85

我的班级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;
       }
    }
 }

oPoints将从 parse中获取对象的值GSON

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

我尝试使用oPoints.message.

当我在没有proguard应用程序运行成功的情况下运行我的应用程序时。proguard当我在应用程序崩溃时运行我的应用程序时。

我认为问题是:将我的班级的proguard属性重命名'oPoints.message'为 short '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>;
}

保留我的一个(静态)类的方法和属性名称的正确方法是什么?

4

9 回答 9

72

如果您不希望您的班级成员被混淆,请使用SerializedNameGson 提供的注释。例如:

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

   ...

}

此外,请确保您也为 Gson 库添加了正确的 proguard 配置。例如:

##---------------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 ----------

有关更多信息,请阅读

于 2013-03-18T12:03:35.940 回答
30

谢谢瓦卡斯!

我为我的情况找到了解决方案:

-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")课堂上使用,上面的配置无需序列化即可正常工作。

于 2013-03-18T13:21:15.660 回答
27

我还发现-keepclassmembers在使用 Dexguard 的优化选项时我需要这样做。没有这个,我的几个模型对象无法反序列化

##---------------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.** { *; }
于 2014-02-20T16:47:31.910 回答
11

如果你@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 *
于 2015-08-19T23:12:41.043 回答
6

要从混淆中排除您的类,请保留 InnerClasses 的属性,保留您的类并保留该类的类成员,例如。

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

更多信息http://proguard.sourceforge.net/manual/examples.html

于 2015-10-07T12:51:13.603 回答
6

您使用的是内置@Keep注释,请参考此链接

于 2017-08-01T05:00:19.907 回答
1

Proguard 6.1 无法处理库中带有泛型的接口。在此处查看错误报告: https ://sourceforge.net/p/proguard/bugs/765/

于 2019-09-05T01:28:50.340 回答
1

您可以在所有请求类型类中跳过混淆

open class Request
data class Example(val name: String) : Request()

proguard-rules.pro

-keepclassmembers class * extends <your-package-name>.util.ws.Request { *; }

总帐

于 2020-01-14T15:21:13.363 回答
1

该线程已经有惊人的答案,但是不要忘记添加

-keep public class * extends java.lang.Exception

否则,当您在 firebase 上看到它们时,所有的崩溃都会被混淆。

在此处查看官方文档:https ://firebase.google.com/docs/crashlytics/get-deobfuscated-reports?platform=android

于 2020-11-17T00:28:31.297 回答