MODEL类中的变量名称重复,在ANDROID STUDIO中进行混淆处理时出错

时间:2018-10-15 11:53:12

标签: android gson obfuscation

我正在通过在清单文件中使minifyEnabled = true来混淆我的Android代码。

但是GSON的混淆代码存在一些问题。 我尝试过:

  1. 迷惑不应该重命名我的Model类和Model类变量。
  2. 在Model类的每个变量上添加@SerializedName。

但是错误提示,我的Model类中有重复的变量,名为a。 但是我的Object类中没有任何重复的变量。

  

build.gradle

release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }

以下是我的保护文件:

  

proguard-rules.pro

-ignorewarnings
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable
-keep class com.crashlytics.** { *; }

-dontwarn com.crashlytics.**

# Gson specific classes
-dontwarn sun.misc.**

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.hgs.ruralhealth_new.model.** { *; }
-keep class com.hgs.ruralhealth_new.model.* { *; }
-keep class com.hgs.ruralhealth_new.model.masterdata.* { *; }

-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

-keep class com.hgs.ruralhealth_new.model.** { <fields>; }
-keep class com.hgs.ruralhealth_new.model.masterdata.** { <fields>; }
-keep class com.hgs.ruralhealth_new.model.register.** { <fields>; }

-keep class com.hgs.ruralhealth_new.model.** { *; }
-keep class com.hgs.ruralhealth_new.model.masterdata.** { *; }
-keep class com.hgs.ruralhealth_new.model.register.RegistrationInputData

-keepclassmembers class com.hgs.ruralhealth_new.model.** { *; }
-keepclassmembers class com.hgs.ruralhealth_new.model.masterdata.** { *; }
-keepclassmembers class com.hgs.ruralhealth_new.model.register.** { *; }
-keepclassmembers class com.hgs.ruralhealth_new.model.register.RegistrationInputData

-keepclassmembers enum * { *; }

错误如下:

FATAL EXCEPTION: main
    Process: com.hgs.ruralhealth_new, PID: 6577
    java.lang.IllegalArgumentException: class d.h$b declares multiple JSON fields named a
        at com.a.a.b.a.i.a(ReflectiveTypeAdapterFactory.java:170)
        at com.a.a.b.a.i.a(ReflectiveTypeAdapterFactory.java:100)
        at com.a.a.f.a(Gson.java:423)
        at com.a.a.b.a.m.a(TypeAdapterRuntimeTypeWrapper.java:56)
        at com.a.a.b.a.i$1.a(ReflectiveTypeAdapterFactory.java:125)
        at com.a.a.b.a.i$a.a(ReflectiveTypeAdapterFactory.java:243)
        at com.a.a.b.a.m.a(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.a.a.b.a.i$1.a(ReflectiveTypeAdapterFactory.java:125)
        at com.a.a.b.a.i$a.a(ReflectiveTypeAdapterFactory.java:243)
        at com.a.a.f.a(Gson.java:669)
        at com.a.a.f.a(Gson.java:648)
        at com.a.a.f.a(Gson.java:603)
        at com.a.a.f.a(Gson.java:583)
        at com.hgs.ruralhealth_new.activity.LoginActivity$2.a(LoginActivity.java:457)
        at d.g$a$1$1.run(ExecutorCallAdapterFactory.java:68)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

当我在Proguard中复制此错误时,在下面的输出中:

FATAL EXCEPTION: main
    Process: com.hgs.ruralhealth_new, PID: 6577
    java.lang.IllegalArgumentException: class d.h$b declares multiple JSON fields named a
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.java.util.Map getBoundFields(com.google.gson.Gson,com.google.gson.reflect.TypeToken,java.lang.Class)(ReflectiveTypeAdapterFactory.java:170)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.com.google.gson.TypeAdapter create(com.google.gson.Gson,com.google.gson.reflect.TypeToken)(ReflectiveTypeAdapterFactory.java:100)
        at com.google.gson.Gson.com.google.gson.TypeAdapter getAdapter(com.google.gson.reflect.TypeToken)(Gson.java:423)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.void write(com.google.gson.stream.JsonWriter,java.lang.Object)(TypeAdapterRuntimeTypeWrapper.java:56)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.void write(com.google.gson.stream.JsonWriter,java.lang.Object)(ReflectiveTypeAdapterFactory.java:125)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.void write(com.google.gson.stream.JsonWriter,java.lang.Object)(ReflectiveTypeAdapterFactory.java:243)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.void write(com.google.gson.stream.JsonWriter,java.lang.Object)(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.void write(com.google.gson.stream.JsonWriter,java.lang.Object)(ReflectiveTypeAdapterFactory.java:125)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.void write(com.google.gson.stream.JsonWriter,java.lang.Object)(ReflectiveTypeAdapterFactory.java:243)
        at com.google.gson.Gson.void toJson(java.lang.Object,java.lang.reflect.Type,com.google.gson.stream.JsonWriter)(Gson.java:669)
        at com.google.gson.Gson.void toJson(java.lang.Object,java.lang.reflect.Type,java.lang.Appendable)(Gson.java:648)
        at com.google.gson.Gson.java.lang.String toJson(java.lang.Object,java.lang.reflect.Type)(Gson.java:603)
        at com.google.gson.Gson.java.lang.String toJson(java.lang.Object)(Gson.java:583)
        at com.hgs.ruralhealth_new.activity.LoginActivity$11.void onResponse(retrofit2.Call,retrofit2.Response)(LoginActivity.java:457)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.void run()(ExecutorCallAdapterFactory.java:68)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

现在,LoginActivity.java:457中的代码是什么

public void onResponse(Call<PhysiotherapistResponse> call, Response<PhysiotherapistResponse> response) {
            Log.i("Physio Backup==>", new Gson().toJson(response));//This is line 457

以下是我的模特课程:

  

PhysiotherapistResponse.java

    public class PhysiotherapistResponse implements Parcelable {

        @SerializedName("status")
        private String status;

        @SerializedName("message")
        private String message;

        @SerializedName("syncFromId")
        private String syncFromId;

        @SerializedName("syncToId")
        private String syncToId;

        @SerializedName("data")
        private List<PhysiotherapistInputData> data;
//////////................GETTERS SETTERS GOES BELOW...........
  

PhysiotherapistInputData.java

    public class PhysiotherapistInputData implements Parcelable {

        @SerializedName("createdDate")
        private String createdDate;

        @SerializedName("swpNo")
        private String swpNo;

        @SerializedName("patientName")
        private String patientName;

        @SerializedName("age")
        private String age;

        @SerializedName("gender")
        private String gender;

        @SerializedName("namePhysio")
        private String namePhysio;

        @SerializedName("painScore")
        private String painScore;

        @SerializedName("exercieAdvice")
        private String exercieAdvice;

        @SerializedName("remark")
        private String remark;

        @SerializedName("isOld")
        private String isOld;

        @SerializedName("doctorId")
        private int doctorId;
//////////................GETTERS SETTERS GOES BELOW...........

1 个答案:

答案 0 :(得分:0)

将遵循proguard规则添加到您的proguard文件中,它来自here

   -keepattributes Signature

    # For using GSON @Expose annotation
    -keepattributes *Annotation*

    # Gson specific classes
    -dontwarn sun.misc.**
    #-keep class com.google.gson.stream.** { *; }

    # Application classes that will be serialized/deserialized over Gson
    -keep class com.google.gson.examples.android.model.** { *; }

    # Prevent proguard from stripping interface information from TypeAdapterFactory,
    # JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
    -keep class * implements com.google.gson.TypeAdapterFactory
    -keep class * implements com.google.gson.JsonSerializer
    -keep class * implements com.google.gson.JsonDeserializer