How to tell Proguard to obfuscate class names

14,148

Solution 1

There are several class that appear in your code that must retain the same fully qualified class name in order for 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 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

Solution 2

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?

Share:
14,148
Code Droid
Author by

Code Droid

Updated on June 17, 2022

Comments

  • Code Droid
    Code Droid almost 2 years

    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.

  • Code Droid
    Code Droid about 12 years
    Well I am seeing the that all class names are being kept. I have since removed the -keepclasseswithmembers but still the same result?
  • Code Droid
    Code Droid about 12 years
    For example I have a utility class and it is being kept. So perhaps there is some implicit rule about keeping class names that reference other classes or are referenced by other classes
  • Code Droid
    Code Droid about 12 years
    I've removed that line. I'm now seeing a Utility class being kept that does not extend from anything being kept. It is simply being used by classes that are kept and importing them etc.
  • David O'Meara
    David O'Meara about 12 years
    (just checking first principles) in your Android project, have you uncommented the definition for proguard.config in project.properties, and what is the value?
  • Code Droid
    Code Droid about 12 years
    proguard.config=proguard.cfg and in manifest I have set debuggable="false" which is also necessary. It is obfuscating to some degree all classes, but for too many it does not obfuscate class names.
  • alexc
    alexc about 12 years
    Hmm, can you put up your full .cfg file?