我试图在我的项目中启用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条答案
按热度按时间wgeznvg71#
无论您是否遇到此错误,请检查您是否:
1 -将@SerializedName添加到模型的属性中。(这有助于GSON找到真实的的名称)
2 -将此配置添加到proguard文件中:
3-如果您一直收到此错误,请检查您的所有模型是否都在使用@SerializedName,并且都在上述规则中包含的包中
h79rfbju2#
您可能需要为following link中的gson添加proguard,它包含以下内容:
更新
您可以尝试使用以下方法将字段保留在类中:
pgccezyw3#
Proguard会混淆类、字段和方法名,使其无法反序列化。
首先尝试在模型类的所有字段中使用
@SerializedName
注解。例如
此外,请使用适当的proguard设置,如@所述。
如果这些都不能解决您的问题,最后的手段将是禁用proguard。
在文件
build.gradle
中设置minifyEnabled false