启用proguard时出现Gson错误

时间:2017-10-31 01:29:34

标签: android gson proguard android-proguard

我正在尝试在我的项目中启用proguard,但我的课程一直被proguard混淆,我收到了这条消息:

 Process: com.myapp.myapp, PID: 4313
                                                                 java.lang.IllegalArgumentException: Unable to create converter for class com.myapp.myapp.data.remote.response.LoginResponse
                                                                     for method AccountsEndPoint.login
                                                                     at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:752)
                                                                     at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:738)
                                                                     at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:169)
                                                                     at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
                                                                     at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
                                                                     at java.lang.reflect.Proxy.invoke(Proxy.java:813)
                                                                     at $Proxy1.login(Unknown Source)
                                                                     at com.myapp.myapp.presentation.activities.LoginActivity.a(LoginActivity.java:211)
                                                                     at com.myapp.myapp.presentation.activities.LoginActivity$1.onClick(LoginActivity.java:97)
                                                                     at android.view.View.performClick(View.java:5637)
                                                                     at android.view.View$PerformClick.run(View.java:22429)
                                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                  Caused by: java.lang.IllegalArgumentException: class com.myapp.myapp.data.b.a.a declares multiple JSON fields named a
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
                                                                     at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
                                                                     at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
                                                                     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
                                                                     at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                     at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:64)
                                                                     at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:330)
                                                                     at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:313)
                                                                     at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:736)
                                                                     at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:169) 
                                                                     at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170) 
                                                                     at retrofit2.Retrofit$1.invoke(Retrofit.java:147) 
                                                                     at java.lang.reflect.Proxy.invoke(Proxy.java:813) 
                                                                     at $Proxy1.login(Unknown Source) 
                                                                     at com.myapp.myapp.presentation.activities.LoginActivity.a(LoginActivity.java:211) 
                                                                     at com.myapp.myapp.presentation.activities.LoginActivity$1.onClick(LoginActivity.java:97) 
                                                                     at android.view.View.performClick(View.java:5637) 
                                                                     at android.view.View$PerformClick.run(View.java:22429) 
                                                                     at android.os.Handler.handleCallback(Handler.java:751) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我在我的模型中使用@SerializedName注释,我也在添加proguard-rules.pro

-keep class com.myapp.myapp.data.remote.request.** { *;}
-keep class com.myapp.myapp.data.remote.response.** { *; }

3 个答案:

答案 0 :(得分:0)

您可能需要从following link添加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
-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

##---------------End: proguard configuration for Gson  ----------

<强>更新

您可以尝试使用以下字段保留班级中的字段:

-keep class com.myapp.myapp.data.remote.request.** { <fields>; }
-keep class com.myapp.myapp.data.remote.response.** { <fields>; }

答案 1 :(得分:0)

Proguard模糊了你的类,字段和方法名称,因此无法反序列化。

首先尝试在模型类的所有字段中使用@SerializedName注释。

例如

@SerializedName("name")
private String name;

也可以使用@ישואוהבאותך提到的正确的proguard设置。

如果其中任何一个无法解决您的问题,最后的办法就是禁用该程序。

在文件minifyEnabled false

中设置build.gradle

答案 2 :(得分:0)

您是否遇到此错误,请检查您是否:

1 - 将@SerializedName添加到模型的属性中。 (这有助于GSON找到真实姓名)

2 - 将此配置添加到您的proguard文件中:

environment.etc

3-如果您不断收到此错误,请检查您的所有型号是否都在使用@SerializedName并且是否在上面规则中包含的包中