Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep annotated class in Proguard

I have a bunch of classes that use e.g. an @Singleton annotation like so

@Singleton public class ImageCache 

that I would like to keep. How can I configure a proguard -keep statement so it applies to all classes that have that annotation.

Btw in terms of context I need this for an app using Roboguice on Android, which is why I added the tags. Might help others.

like image 660
Manfred Moser Avatar asked Jul 23 '12 23:07

Manfred Moser


People also ask

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.

What does ProGuard keep do?

-keep Specifies classes and class members (fields and methods) to be preserved as entry points to your code. For example, in order to keep an application, you can specify the main class along with its main method. In order to process a library, you should specify all publicly accessible elements.

How does ProGuard obfuscation work?

Simply by enabling Proguard, we at Gradeup reduced our app size by a whooping 40%! It obfuscates the code, which means that it renames classes, fields, and methods with semantically obscure names that, in addition to making the codebase smaller and more efficient, also makes it difficult to reverse engineer the app.

Where is ProGuard Android optimize txt?

The getDefaultProguardFile() refers default file “proguard-android. txt” which gets from the Android SDK tools/proguard/ folder. You can also use “proguard-android-optimize. txt” file for more code shrinking located on the same folder.


2 Answers

ProGuard is based on a java-like configuration with wild-cards. It does require fully qualified class names. This should work:

-keep @com.google.inject.Singleton public class * 
like image 57
Eric Lafortune Avatar answered Sep 26 '22 14:09

Eric Lafortune


First define an annotation

public @interface DoNotStrip {} 

Then put this in proguard.cfg:

-keep,allowobfuscation @interface com.something.DoNotStrip  # Do not strip any method/class that is annotated with @DoNotStrip -keep @com.something.DoNotStrip class * -keepclassmembers class * {     @com.something.DoNotStrip *; } 
like image 42
Sahil Jain Avatar answered Sep 22 '22 14:09

Sahil Jain