Proguard (R8) obfuscate custom view names

坚强是说给别人听的谎言 提交于 2021-01-27 21:36:38

问题


I am using R8 in my app and have several custom views (which are referenced in xml layouts) tho their names are not obfuscated at all. Is there any way to achieve this? I am using the standard Gradle rules:

release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }

And also tried with android.enableR8.fullMode=true but it's the same.


回答1:


I am using R8 in my app and have several custom views (which are referenced in xml layouts) tho their names are not obfuscated at all.

This is because the proguard-android-optimize.txt has the following rule:

# keep setters in Views so that animations can still work.
-keepclassmembers public class * extends android.view.View {
    void set*(***);
    *** get*();
}

So your custom views, or any views, will not have their names obfuscated by default.

Now the question is can you still have R8 rename the custom views in your app? And the answer is not really.

You could add an -applymapping myCustomMapping.txt by copying the contents of <root_dir>/app/build/outputs/mapping/<build_variant>/mapping.txt and replacing all references to your custom views that are NOT obfuscated with obfuscated names. Like this:

  1. Copy contents of <root_dir>/app/build/outputs/mapping/<build_variant>/mapping.txt into a new file <root_dir>/app/myCustomMapping.txt
  2. Before changing anything, it will look like this:
my.app.package.CustomView -> my.app.package.CustomView :
    13:34:void <init>(android.content.Context,android.util.AttributeSet,int) -> <init>
    15:16:void <init>(android.content.Context,android.util.AttributeSet,int,int,kotlin.jvm.internal.DefaultConstructorMarker) -> <init>
    43:46:void customMethod() -> c

  1. You need to change only this line, that has the top level class mapping. Notice that it is unchanged because of the android proguard rules. Change it to whatever obfuscated name you want, like this:
my.app.package.CustomView -> my.app.package.youcantseemeatall :
    13:34:void <init>(android.content.Context,android.util.AttributeSet,int) -> <init>
    15:16:void <init>(android.content.Context,android.util.AttributeSet,int,int,kotlin.jvm.internal.DefaultConstructorMarker) -> <init>
    43:46:void customMethod() -> c
  1. Finally, add these lines to your proguard-rules.pro file
-applymapping myCustomMapping.txt
-printmapping mapping.txt

Those above steps will change your .class files to obfuscate CustomView to youcantseemeatall, BUT your resource files will still reference the original CustomView name and your app will crash at runtime.

Conclusion:

Unfortunately there really isn't a way to do what your asking with proguard or any tooling that comes with Android Studio. There may be a custom Gradle Plugin that changes all custom view names before the app is assembled, but I couldn't find one just googling it now.



来源:https://stackoverflow.com/questions/62469583/proguard-r8-obfuscate-custom-view-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!