Prevent ProGuard class member name confusion

I have a ClassMultiPoint class with subclasses.

 public class ClassMultiPoints { public String message; public List<ClassPoints> data; public class ClassPoints { public String id; public List<ClassPoint> points; public class ClassPoint { public String speed; public String bearing; } } } 

I will get the value of the oPoints object from the parse GSON :

oPoints = gson.fromJson( jsonString, ClassMultiPoints.class);

I am trying to use oPoints.message .

When I launch the application without proguard success of the application. When I run my application with proguard application crashes.

I think the problem is this: proguard rename the attribute 'oPoints.message' my class to short 'a' .

I try to keep method and attribute names constant, but proguard rename it:

proguard.cfg:

 -injars bin/classes -injars libs -outjars bin/classes-processed.jar -dontpreverify -repackageclasses '' -allowaccessmodification -optimizations !code/simplification/arithmetic -keepattributes *Annotation* -dontskipnonpubliclibraryclasses -optimizationpasses 5 -printmapping map.txt -flattenpackagehierarchy -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.MapActivity -keep public class * extends android.app.Service -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -libraryjars libs/commons-io-2.2.jar -libraryjars libs/ftp4j-1.7.1.jar -libraryjars libs/gson-2.2.2.jar -keep public class org.apache.commons.io.** -keep public class it.sauronsoftware.ftp4j.** -keep public class com.google.gson.** -keep public class com.mypackagename.ActivityMonitor$* -keep public class * extends android.view.View { public <init>(android.content.Context); public <init>(android.content.Context, android.util.AttributeSet); public <init>(android.content.Context, android.util.AttributeSet, int); public void set*(...); } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet, int); } -keepclassmembers class * extends android.content.Context { public void *(android.view.View); public void *(android.view.MenuItem); } -keepclassmembers class * implements android.os.Parcelable { static android.os.Parcelable$Creator CREATOR; } -keepclassmembers class **.R$* { public static <fields>; } 

What is the correct way to store the method names and attributes of my (static) class?

+66
java android obfuscation proguard
Mar 18 '13 at 11:59
source share
7 answers

Thanks Waqas!

I find a solution for my case:

 -optimizationpasses 5 -dump class_files.txt -printseeds seeds.txt -printusage unused.txt -printmapping mapping.txt -optimizations !code/simplification/arithmetic,!field/*,!class/merging*/ -allowaccessmodification -repackageclasses '' -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.MapActivity -keep public class * extends android.app.Service -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -libraryjars libs/commons-io-2.2.jar -libraryjars libs/gson-2.2.2.jar -keep public class org.apache.commons.io.** -keep public class com.google.gson.** -keep public class com.google.gson.** {public private protected *;} ##---------------Begin: proguard configuration for Gson ---------- -keepattributes *Annotation*,Signature -keep class com.mypackage.ActivityMonitor.ClassMultiPoints.** { *; } -keep public class com.mypackage.ActivityMonitor$ClassMultiPoints { public protected *; } -keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints { public protected *; } -keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints$ClassPoint { public protected *; } # To support Enum type of class members -keepclassmembers enum * { *; } ##---------------End: proguard configuration for Gson ---------- 

Also, I don't use @SerializedName("message") in my class, and the config command works fine without serialization.

+27
Mar 18 '13 at 13:21
source share

If you don't want your class members to be confused, use the SerializedName annotation provided by Gson. For example:

 public class ClassMultiPoints { @SerializedName("message") public String message; @SerializedName("data") public List<ClassPoints> data; ... } 

Also, make sure you also add the correct proguard configuration for the Gson library. For example:

 ##---------------Begin: proguard configuration for Gson ---------- # Gson uses generic type information stored in a class file when working with #fields. Proguard removes such information by default, so configure it to keep #all of it. -keepattributes Signature # For using GSON @Expose annotation -keepattributes *Annotation* # Gson specific classes -keep class sun.misc.Unsafe { *; } #-keep class com.google.gson.stream.** { *; } # Application classes that will be serialized/deserialized over Gson -keep class com.google.gson.examples.android.model.** { *; } ##---------------End: proguard configuration for Gson ---------- 

For more information read this .

+66
Mar 18 '13 at 12:03
source share

I also found that I need -keepclassmembers when using the Dexguard optimization option. Without this, some of my model objects could not deserialize

 ##---------------Begin: proguard configuration for Gson ---------- # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. -keepattributes Signature # For using GSON @Expose annotation -keepattributes *Annotation* # Application classes that will be serialized/deserialized over Gson, keepclassmembers -keep class com.myapp.model.** { *; } -keepclassmembers class com.myapp.model.** { *; } 
+23
Feb 20 '14 at 16:47
source share

If you use @Expose annotation like me, you can tell ProGuard to save any field annotated with it:

 # keep anything annotated with @Expose -keepclassmembers public class * { @com.google.gson.annotations.Expose *; } # Also keep classes that @Expose everything -keep @com.google.gson.annotations.Expose public class * 
+7
Aug 19 '15 at 23:12
source share

To exclude your class from obfuscation, keep the InnerClasses attributes, save your class, and save the class class of the class, for example.

 -keepattributes InnerClasses -keep class com.yourproject.YourClass** -keepclassmembers class com.yourproject.YourClass** { *; } 

For more information http://proguard.sourceforge.net/manual/examples.html

+4
Oct 07 '15 at 12:51 on
source share

There is a built-in annotation containing @Keep , please refer to this link

+4
Aug 01 '17 at 5:00
source share

Proguard 6.1 cannot process interfaces with generics in the library. See the bug report here: https://sourceforge.net/p/proguard/bugs/765/

0
Sep 05 '19 at 1:28
source share



All Articles