user7154703
user7154703

Reputation:

proguard - what is difference between - keep and keep { } statement?

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*(...); 
} 

Upvotes: 1

Views: 1146

Answers (1)

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28268

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.

Upvotes: 4

Related Questions