启用proguard时发生Gson错误

mrfwxfqh  于 2022-11-06  发布在  其他
关注(0)|答案(3)|浏览(140)

我试图在我的项目中启用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.**{ *; }
wgeznvg7

wgeznvg71#

无论您是否遇到此错误,请检查您是否:
1 -将@SerializedName添加到模型的属性中。(这有助于GSON找到真实的的名称)
2 -将此配置添加到proguard文件中:


### 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,并且都在上述规则中包含的包中

h79rfbju

h79rfbju2#

您可能需要为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>; }
pgccezyw

pgccezyw3#

Proguard会混淆类、字段和方法名,使其无法反序列化。
首先尝试在模型类的所有字段中使用@SerializedName注解。
例如

@SerializedName("name")
private String name;

此外,请使用适当的proguard设置,如@所述。
如果这些都不能解决您的问题,最后的手段将是禁用proguard。
在文件build.gradle中设置minifyEnabled false

相关问题