1

Am fail to fully understand significance of { ---- } after -keep, Can someone please take below example and explain the difference ?

Statement 1

-keep public class * extends android.view.View ; 

Statement 2

-keep public class * extends android.view.View { 
  public <init>(android.content.Context); 
  public <init>(android.content.Context, android.util.AttributeSet); 
  public <init>(android.content.Context, android.util.AttributeSet, int); 
  public void set*(...); 
} 

Statement 3

-keepclassmembers public class * extends android.view.View { 
  public <init>(android.content.Context); 
  public <init>(android.content.Context, android.util.AttributeSet); 
  public <init>(android.content.Context, android.util.AttributeSet, int); 
  public void set*(...); 
} 
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

4

This line:

-keep public class * extends android.view.View ; 

prevents a single class from being obfuscated, while this:

-keep public class * extends android.view.View { 
  public <init>(android.content.Context); 
  public <init>(android.content.Context, android.util.AttributeSet); 
  public <init>(android.content.Context, android.util.AttributeSet, int); 
  public void set*(...); 
} 

prevents (in this case) specific methods from being obfuscated. When there's brackets involved, it means the statement applies to multiple items. It can also be used to keep multiple classes, or something else where you'd keep multiple of something else. You can also use it for packages or and through that specific classes in packages. The entire system is flexible because of the wildcard. Basically:

-keep something;

means keep a single one, while:

- keep something {
    somethingElse
}

specifies what to keep based on the parameter. Meaning it can specify multiple to keep. (commented version of that one:)

- keep something {//When something
    somethingElse//keep somethingElse
    //... and more as needed
}

It's to a certain degree comparable to an if-statement. You have two different ones:

if(something)
     //single line for action

and:

if(something){
    //Do multiple things
}

essentially:

-keep //keep if
     public class * extends Something //the class extends something

and

-keep //if
    public class * extends Something //the class extends something
    {
        field1//keep field1
        //and whatever else is supplied
    }

And your third example:

-keep public keepclassmembers * extends android.view.View { 
  public <init>(android.content.Context); 
  public <init>(android.content.Context, android.util.AttributeSet); 
  public <init>(android.content.Context, android.util.AttributeSet, int); 
  public void set*(...); 
} 

is wrong. keepclassmembers is something you use instead of -keep. I.e.:

-keepclassmembers public class * extends android.view.View {
  public <init>(android.content.Context); 
  public <init>(android.content.Context, android.util.AttributeSet); 
  public <init>(android.content.Context, android.util.AttributeSet, int); 
  public void set*(...); 
}

And -keepclassmembers keeps the class members, while -keep keeps the class members and classes themselves.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • in this case, how keep {} is different than keepclassmembers {}, because both prevent members only. I have updated above as statement 3 ? –  Dec 22 '17 at 14:59
  • statement 3 was typo, thanks for correcting it. So my understanding is Statement 1 - prevent class View from being obfuscated Statement 2 - prevent certain methods i.e. public (android.content.Context); which are coming between {} from being obfuscated Statement 3 - prevent certain methods i.e. public (android.content.Context); which are coming between {} from being obfuscated Though I understand difference between keep and keepclassmembers , in this specific case result of statement 2 and statement 3 will be same, please correct if I am wrong. –  Dec 22 '17 at 17:11
  • @Girish yes, it'll be the same. But with `-keepclassmembers` (AFAIK and IIRC) you can't keep nested classes. So if you have a nested class, that will still be obfuscated and can't be kept with `-keepclassmembers` – Zoe Dec 22 '17 at 19:43
  • does the -keep class * also preserve the members of the lass? saw some config has these two rules together: -keep public class com.google.gson.** -keep public class com.google.gson.** {public private protected *;} why? see https://stackoverflow.com/questions/65613612/why-it-needs-two-proguard-rule-keep-for-same-class, Thanks! – lannyf Jan 07 '21 at 17:50