Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

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.

  • I want to keep all package names, which I'm using "-keeppackagenames" to achieve.
  • I want to keep all class names.
  • I want to keep method names and method argument names for all public methods.

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!

question from:https://stackoverflow.com/questions/65837432/how-to-keep-method-names-in-android-proguard-r8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...