Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Proguard to obfuscate class names

I would like proguard to obfuscate classnames. I have this line in Proguard.cfg

-keepclasseswithmembers class * {
public static <fields>;
}

-keepnames class * implements java.io.Serializable
-keep public class com.google.**

And I notice that what is not obfuscated is the class names. So running jdgui i see com/test/abcd/ActualClass.java public class ActualClassName extends Activity etc

moreover I see methods returning real classnames. like

 ActualClassname aa();

and imports statements like

 import com.abcd.ActualClassName

How do I get Proguard to obfuscate the classname itself. Its not just for Activities that I see this my Adapters are not being obfuscated. Well there is obfuscation going on but not class names.

Is the rules above what prevents the class names from being obfuscated?

Update: i have since removed the rules above and a Utility class that does not extend anything from Android is not being obfuscated. I'm now wondering if there is some implicit rule about keeping class names of classes that are referenced from classes that are being kept like Activity derivied classes? The classes whose names are not being obfuscated have a few things in common:

1) Static methods 2) Import of other types which are kept like those deriving from activity or serializable. 3) They have methods with parameters of other classes (Some of which might need to be kept).

However there is no where that I am specifically requesting that these utility classes should be kept.

like image 736
Code Droid Avatar asked May 08 '12 02:05

Code Droid


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 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.

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.


2 Answers

There are several class that appear in your code that must retain the same fully qualified class name in order for android to be able to find them. One example as above are all Activity classes, as they are defined in the manifest by their full name as a String. If proguard were to rename the class then Android would no longer be able to find them.

The typical Proguard config will refer to the Proguard config in the Android SDK which will include several important lines like this one:

-keep public class * extends android.app.Activity
like image 192
David O'Meara Avatar answered Sep 28 '22 04:09

David O'Meara


I think your problem is coming from your first line: '-keepclasseswithmembers' includes the class name, so in your case any class with a static field will keep its name. Changing it to simply '-keepclassmembers' will obfuscate the class names while leaving the static fields intact(which I'm assuming you want to do).

That said, I'm not sure why you want to do this, though. If you're trying to preserve the static variable names, shouldn't you want to preserve the class names as well, seeing as how that's how you'll be accessing the static fields? Why are you trying to preserve all of your static variable names?

like image 24
alexc Avatar answered Sep 28 '22 04:09

alexc