Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard doesnt obfuscate Class name, only methods are obfuscated

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

like image 497
Qing Avatar asked Mar 18 '16 06:03

Qing


People also ask

Does ProGuard obfuscate package name?

By default, ProGuard obfuscates the code: it assigns new short random names to classes and class members. It removes internal attributes that are only useful for debugging, such as source files names, variable names, and line numbers.

How does ProGuard obfuscation work?

In the obfuscation step, ProGuard renames classes and class members that are not entry points. In this entire process, keeping the entry points ensures that they can still be accessed by their original names. The preverification step is the only step that doesn't have to know the entry points.

How do you keep a class in ProGuard?

-keepclassmembernames. This is the most permissive keep directive; it lets ProGuard do almost all of its work. Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names.


2 Answers

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>(...);
}
like image 195
Tomik Avatar answered Oct 17 '22 18:10

Tomik


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.** { *; }
like image 31
shanraisshan Avatar answered Oct 17 '22 18:10

shanraisshan