如何让我的课程不被混淆
我有我的课ClassMultiPoint
与子类。
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; } } }
我将从parsingGSON
获得对象oPoints
值:
oPoints = gson.fromJson( jsonString, ClassMultiPoints.class);
我尝试使用oPoints.message
。
当我运行我的应用程序没有proguard
应用程序运行成功。 当我运行我的应用程序与proguard
我的应用程序崩溃。
我认为问题是: proguard
重命名属性'oPoints.message'
我的类短'a'
。
我尝试保持的方法和属性的名称是不变的,但proguard
重命名:
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>; }
什么是正确的方法来保持我的一个(静态)类的方法和属性的名称?
如果你不想让你的类成员被混淆,那么使用由Gson提供的SerializedName
注解。 例如:
public class ClassMultiPoints { @SerializedName("message") public String message; @SerializedName("data") public List<ClassPoints> data; ... }
此外,请确保您也为Gson库添加适当的proguardconfiguration。 例如:
##---------------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 ----------
欲了解更多信息,请阅读。
谢谢Waqas!
我为我的情况find解决办法:
-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 ----------
此外,我不使用@SerializedName("message")
在我的课,上面的configuration工作正常,无需序列化。
当我使用Dexguard的优化选项时,我也发现我需要-keepclassmembers
成员。 没有这几个我的模型对象未能反序列化
##---------------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.** { *; }
如果您像我一样使用@Expose
注释,则可以告诉ProGuard保留用它注释的任何字段:
# 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 *
要排除你的类混淆,保持InnerClasses的属性,保持你的类,并保持类的类成员例如。
-keepattributes InnerClasses -keep class com.yourproject.YourClass** -keepclassmembers class com.yourproject.YourClass** { *; }
有一个你使用的内置@Keep
注释,请参考这个链接