Proguard keep class names?

49,790

Solution 1

Use the -keepnames option in your proguard.cfg

Refer to the manual https://www.guardsquare.com/manual/configuration/usage#keepoptions

-keepnames class_specification

Short for -keep,allowshrinking class_specification

Specifies classes and class members whose names are to be preserved, if they aren't removed in the shrinking phase. For example, you may want to keep all class names of classes that implement the Serializable interface, so that the processed code remains compatible with any originally serialized classes. Classes that aren't used at all can still be removed. Only applicable when obfuscating.

Solution 2

This keeps classnames intact:

-keepnames class com.somepackage.* 

Solution 3

Handy tip for everyone who does not want ProGuard to change any class name:

# please KEEP ALL THE NAMES
-keepnames class ** { *; }

This way you will get readable stack traces while still throwing out things you don't need. :-)

Solution 4

If anyone is interested how to specify multiple class names to keep, then these classes can be separated by a comma. Example:

-keepnames class com.foo.**,com.bar.** { *; }

It is also possible to use negation with this because usually only own classes would be obfuscated and 3rd party libraries can be kept:

-keepnames class !com.foo.**,!com.bar.** { *; }

See the Proguard Documentation for this.

Share:
49,790
Nik
Author by

Nik

Updated on June 25, 2020

Comments

  • Nik
    Nik almost 4 years

    Hello I am writing an Android app and I have set up Proguard to obfuscate my application. I however use a classloader to dynamically load different extensions to my application. The problem is that these don't load correctly if their names are changed. How do I keep Proguard from obfuscating specific class names?

  • Nik
    Nik almost 12 years
    Ok is there a way to do it with a whole package?
  • Zaid Daghestani
    Zaid Daghestani almost 12 years
    Yes, you use the * wildcard. ie, -keepnames com.randompackage.lol.* Will keep all classes in lol
  • Nik
    Nik almost 12 years
    I have tried that and eclipse returns error 1. Are you sure i dont have to do -keepnames class com.randompackage.lol.ClassName ? I have tried this before posting here but it didn't work :S
  • Zaid Daghestani
    Zaid Daghestani almost 12 years
    Try just -keep instead of -keepnames
  • Nik
    Nik almost 12 years
    Doesn't -keep NOT obfuscate the class?
  • Zaid Daghestani
    Zaid Daghestani almost 12 years
    Yes it does. -keepnames is a better option if you only want to not obfuscate class names, but keep will do the same. obfuscate = rename.
  • Jessie A. Morris
    Jessie A. Morris almost 9 years
    -dontobfuscate is the right way to disable obfuscation.
  • oxied
    oxied almost 9 years
    Wrong way. It's better to turn off obfuscation with this configuration: "-dontobfuscate /n -optimizations !code/allocation/variable"
  • android developer
    android developer over 5 years
    Does "-keepnames" just keep the name of the class ? Or does it do more? I want to just keep the name.
  • Brill Pappin
    Brill Pappin almost 5 years
    Better yet, upload your symbols file and allow the capture properly.
  • Aymeric S
    Aymeric S over 4 years
    @androiddeveloper -keep and -keepnames do the same as far as obfuscation is concerned (class names specified in class_specification are kept). As stated above, the difference is in the shrinking phase (with -keepnames, an unused class name is removed instead of kept). Both of them will however obfuscate class members, except if you add a specification to prevent this: adding e.g. {*;} at the end of class_specification (to keep all class members). Note that adding {} is the same as adding no curly brackets at all.
  • android developer
    android developer over 4 years
    @AymericS So it prevents renaming just the class, and if it seems unused, it will remove it. What would happen if you reach the class via reflection? Or even here it depends how you do it, because the shrinker might not understand that the class is really being used?
  • Steven Jeuris
    Steven Jeuris over 2 years
    I was. :) Now that I know to google for "comma-separated class names" I found this. :) Would you happen to know whether a space after the comma is allowed?
  • k_o_
    k_o_ over 2 years
    Not sure, try it, but I guess not, It might be considered as a new option.
  • Steven Jeuris
    Steven Jeuris over 2 years
    Space, and even newline, is allowed.
  • Jawad El Fou
    Jawad El Fou over 2 years
    Link in this answer is not working @ZaidDaghestani