启用proguard时出现Gson错误
Gson error when enabling proguard
我正在尝试在我的项目中启用混淆器,但我的 class 一直被混淆器混淆,我收到了这条消息:
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.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.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.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.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.** { *; }
您可能需要为 following link 中的 gson 添加混淆器。它包含以下内容:
##---------------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 ----------
更新
您可以尝试将 class 中的字段保留为:
-keep class com.myapp.myapp.data.remote.request.** { <fields>; }
-keep class com.myapp.myapp.data.remote.response.** { <fields>; }
Proguard 混淆了您的 class、字段和方法名称,因此无法反序列化。
首先尝试在模型的所有字段中使用 @SerializedName
注释 classes。
例如
@SerializedName("name")
private String name;
还使用@əשו אוהב אותך 提到的适当的混淆器设置。
如果这些都不能解决您的问题,最后的办法就是禁用混淆器。
在文件中设置minifyEnabled false
build.gradle
如果您遇到此错误,请检查您是否:
1 - 将@SerializedName 添加到模型的属性中。 (这有助于GSON找到真实姓名)
2 - 将此配置添加到您的混淆器文件中:
###GSON
##---------------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.** { *; }
# 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.data.remote.request.** { *; } # <--- Add your models package here
3- 如果您不断收到此错误,请检查您的所有模型是否都在使用 @SerializedName 并且是否在上述规则中包含的包中
我正在尝试在我的项目中启用混淆器,但我的 class 一直被混淆器混淆,我收到了这条消息:
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.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.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.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.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.** { *; }
您可能需要为 following link 中的 gson 添加混淆器。它包含以下内容:
##---------------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 ----------
更新
您可以尝试将 class 中的字段保留为:
-keep class com.myapp.myapp.data.remote.request.** { <fields>; }
-keep class com.myapp.myapp.data.remote.response.** { <fields>; }
Proguard 混淆了您的 class、字段和方法名称,因此无法反序列化。
首先尝试在模型的所有字段中使用 @SerializedName
注释 classes。
例如
@SerializedName("name")
private String name;
还使用@əשו אוהב אותך 提到的适当的混淆器设置。
如果这些都不能解决您的问题,最后的办法就是禁用混淆器。
在文件中设置minifyEnabled false
build.gradle
如果您遇到此错误,请检查您是否:
1 - 将@SerializedName 添加到模型的属性中。 (这有助于GSON找到真实姓名)
2 - 将此配置添加到您的混淆器文件中:
###GSON
##---------------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.** { *; }
# 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.data.remote.request.** { *; } # <--- Add your models package here
3- 如果您不断收到此错误,请检查您的所有模型是否都在使用 @SerializedName 并且是否在上述规则中包含的包中