4

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.

Alex Newman
  • 1,369
  • 12
  • 34

1 Answers1

4

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.

Community
  • 1
  • 1
flopshot
  • 1,153
  • 1
  • 16
  • 23
  • I should also mention that there is no way EXTEND the R8/proguard mapping.txt. You can only REPLACE it with your own. Thus, you would have to repeat steps 1-4 every time you updated your source code. R8/Proguard really makes it a pain to customize your obfuscation names without 3rd party tooling. – flopshot Jun 19 '20 at 14:44
  • Thanks for the info, appreciated. – Alex Newman Jun 19 '20 at 15:20