Proguard obfuscating does not work

2019-04-11 12:54发布

问题:

I'm trying to obfuscate my package names including that one of my used libraries.

I use this build config in my gradle file:

buildTypes {
    debug {
        versionNameSuffix "-Development"
        debuggable true
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    #...

This is my proguard file:

# Butterknife
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepnames class * { @butterknife.InjectView *;}

# OrmLite uses reflection
-keepclassmembers class com.j256.** { *; }
-keep class my.package.name.database.** { *; }
-keep class com.j256.**

#test
-repackageclasses 'qqq1'
-flattenpackagehierarchy 'qqq2'
-allowaccessmodification
-forceprocessing

I'm using this command for dumping all the dexed classes:

7z x -aoa my.apk classes.dex && dexdump classes.dex | grep "Class desc" | less

And I still see all full package names if I just grep for "qqq" I get no results so it seems that both rules repackageclasses and flattenpackagehierarchy seems to be ignored (I also tested to use only one of that lines). Any idea what I missed?

回答1:

For library modules, it seems that the build system add "-keeppackagenames" by default which will lead to the package names not be obfuscated.

You can try using this WORKAROUND:

Add "-keeppackagenames !**" to disable -keeppackagenames being injected by the build system.

Via: https://code.google.com/p/android/issues/detail?id=67587



回答2:

Wow this took long to fix. The butterknife rules broke everything. My solution was to grep that one from the homepage and how everything works as expected.

Here are the fixed rules:

# Butterknife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

# OrmLite uses reflection
-keepclassmembers class com.j256.** { *; }
-keep class my.package.name.database.** { *; }
-keep class com.j256.**

#test
-repackageclasses 'qqq1'
-flattenpackagehierarchy 'qqq2'
-allowaccessmodification
-forceprocessing