Proguard doesnt obfuscate Class name, only methods

2019-06-27 09:59发布

I try using Proguard in android studio, but seems like Proguard is not obfuscating the class name, for example, my app structure, and the config:

enter image description here

and config

enter image description here

but when i try trigger the exception in the app:

enter image description here

the exception is listed in ADB console:

enter image description here

only the methods are obfuscated, the MainActivity.class is not

2条回答
老娘就宠你
2楼-- · 2019-06-27 10:27

I was facing the same problems,

After updating my Android plugin for Gradle, Proguard stop obfuscating my utility and other class files.

After few searching, I found that Android studio gradle now uses newer version of Proguard.

And according to this stack-overflow answer, which stated that: proguard automatically add rules specific for android/google package.

Therefore, After few rule changes in my app, Proguard obfuscated the class names again.

Old proguard-rules.pro:

#support-v4
#@link https://stackoverflow.com/questions/18978706/obfuscate-android-support-v7-widget-gridlayout-issue
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }

#support-v7
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
#https://stackoverflow.com/a/34895791/4754141
-keep class !android.support.v7.view.menu.**
-keep interface android.support.v7.* { *; }

#support design
#@link https://stackoverflow.com/a/31028536
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

#error : Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
#solution : @link https://stackoverflow.com/a/14463528
-dontnote com.google.vending.licensing.ILicensingService
-dontnote **ILicensingService

#updating to Gradle 2.14.1 caused error :         https://stackoverflow.com/q/17141832/4754141
-keepattributes EnclosingMethod

#render script
#@link https://stackoverflow.com/questions/22161832/renderscript-support-library-crashes-on-x86-devices
-keepclasseswithmembernames class * { native <methods>; }
-keep class android.support.v8.renderscript.** { *; }

New proguard-rules.pro:

#https://stackoverflow.com/a/41901653/4754141
#https://stackoverflow.com/a/23840049/4754141
-keep class android.support.** { *; }
-keep interface android.support.** { *; }
查看更多
趁早两清
3楼-- · 2019-06-27 10:51

This is an expected behaviour because the class is an activity!

All classes that are mentioned in AndroidManifest.xml have to keep their names (activities, services, providers, receivers, application, instrumentation). Otherwise the system won't be able to find them.

Gradle build automatically generates some rules for your ProGuard configuration to achieve this. It scans AndroidManifest.xml and adds rules for each class found there.

If you want to see all the rules that are used, add this line to your ProGuard rules:

-printconfiguration "build/outputs/mapping/configuration.txt"

It will create configuration.txt file containing all the rules.

There should be something like this:

# view AndroidManifest.xml #generated:50
-keep class com.github.browep.proguard.MainActivity {
    <init>(...);
}
查看更多
登录 后发表回答