Harry
Harry

Reputation: 353

How to keep method names in Android Proguard R8?

I'm developing an Android Library and I'd like to have this result for Proguard.

Other than that, I'd like to obfuscate and shrink everything else, including but not limited to all of non-public methods and even the body of the public methods.

How can I achieve that result?

Thank you very much beforehand for your help!

Upvotes: 1

Views: 1161

Answers (1)

sgjesse
sgjesse

Reputation: 4628

R8 itself is distributes as a library which has been run through R8. For that library we want to achieve exactly what you ask to ensure that developers using the library will not by accident use any APIs which are not supposed to be public (this has nothing to do with protecting IP as the project is open source).

The rules can be found here and I will explain the rationale for them below.

We try to be indirect and use annotations, so the main rules are:

-keep @com.android.tools.r8.Keep class * { public *; }
-keep @com.android.tools.r8.KeepForSubclassing class * { public *; protected *; }

All API classes are then annotated with @Keep or @KeepForSubclassing.

Then a number of attrubutes are kept

-keepattributes SourceFile, LineNumberTable, InnerClasses, EnclosingMethod, Exceptions, Signature

SourceFile and LineNumberTable are kept to get retracable stack traces. The rest for javac compilation and IDE integration for library users.

Then

-keepparameternames

is there for IDE integration getting the names of arguments in the API, and

-keeppackagenames com.android.tools.r8
-repackageclasses com.android.tools.r8.internal

to move as many classes as possible into the internal name space so the renamed classes does not show up in IDE's.

There are a few additional rules in the file to handle cases not covered by the annotation based approach.

Upvotes: 1

Related Questions