Replacing ProGuard keep-rule with @Keep annotation obfuscates members names

Viewed 164

Using ProGuard to keep an entry point in a library, and also not allow it to be obfuscated, I use the proguard keep rule:

-keep,includedescriptorclasses public class com.demo.api.** { *; }

I would like to replace this rule with a @Keep annotation like this:

@Keep
public class SomeClass {

   public void someMethod() { /*..*/ }
}

If I analyze the aar library containing this class, the SomeClass was kept (not shrunk) and not obfuscated along with method someMethod, as expected.

If I build an app using this library, however, the class SomeClass is kept, but its members are still obfuscated. The app only has the default rules and an empty local proguard_rules.txt:

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

How can I get the @Keep annotation behave in a similar way as the ProGuard -keep rule?

1 Answers

The @keep annotation is added to the class and it will not automatically inherit the keep behavior to all its methods. All the Keep annotation does is ensure the class is not minified (removed) and not obfuscated. So to ensure the methods are not obfuscated or minified the @Keep annotation has to be added to all the methods you want retained as well.

Related