简体   繁体   English

在启用 proguard 后调用 retrofit 时在 json 中获取错误的密钥

[英]Getting wrong keys in json while calling retrofit after enabling proguard

when i call retrofit without proguard i got json like following: D/OkHttp: {"UserPin":"123456","password":"123456"} and after adding proguard it gives following json: D/OkHttp: {"a":"123456","b":"123456"} when i call retrofit without proguard i got json like following: D/OkHttp: {"UserPin":"123456","password":"123456"} and after adding proguard it gives following json: D/OkHttp: {"a":"123456","b":"123456"}

Proguard changes the keys in json Proguard 更改 json 中的键

This is my code:-这是我的代码:-

private void doLogin(String userPin, String password) {
        startProgress();
        LoginUser loginUser = new LoginUser(userPin, password);
        Call<Login> call = MyApplication.apiInterface.doLogin(loginUser);
        call.enqueue(new Callback<Login>() {
            @Override
            public void onResponse(Call<Login> call, Response<Login> response) {
                finishProgress();
                try {
                    setLoginResponse(response.body());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<Login> call, Throwable t) {
                finishProgress();
                try {
                    setLoginResponse(null);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

    }

Please help me请帮我

You should add proguard rule.您应该添加proguard规则。

-ignorewarnings
-dontwarn okio.**

-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions

-keepattributes *Annotation*

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

And Login class并登录class

-keep class package_name.Login** { *; }
-keepclassmembers class package_name.Login** { *; }

For GSON Proguard , Try this对于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  ----------

Check to make sure are have proguard configured for gson, too.检查以确保也为 gson 配置了 proguard。 You need to make sure POJO used with gson are not obfuscated and annotations are not stripped.您需要确保与 gson 一起使用的 POJO 未被混淆并且注释未被剥离。

Note: You should replace com.google.gson.examples.android.model.** { *; }注意:您应该替换com.google.gson.examples.android.model.** { *; } com.google.gson.examples.android.model.** { *; } with your model classes in the example below.在下面的示例中使用您的com.google.gson.examples.android.model.** { *; }类。

From the gson example proguard config --来自 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
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启用Proguard后收到警告和错误 - Getting warnings and errors after enabling proguard 启用Proguard后获取com.loopj.android.http.AsyncHttpClient - Getting com.loopj.android.http.AsyncHttpClient after enabling Proguard 在Android Studio中启用Proguard后制作签名的APK时出错 - Error while making signed apk after enabling the Proguard in Android Studio 启用 Proguard 时 Retrofit2 不起作用 - Retrofit2 not working when enabling Proguard 在Android中使用翻新时使用带有动态键的JSON - JSON with Dynamic Keys while using Retrofit in Android 启用proguard时,我在“ getActivity()。getPackageName()”上崩溃,但在未启用的情况下工作正常 - I am getting crash on “getActivity().getPackageName() ” while enabling proguard but working fine without enabling 启用 proguard 后出现 StackOverflow 错误 - StackOverflow error after enabling proguard 启用proguard后,Realm中的ClassNotFoundException - ClassNotFoundException in Realm after enabling proguard Android:启用“创建版本apk时无法计算哈希类-proguard”后 - Android: after enabling geting Unable to compute hash classes-proguard while creating Release apk 启用Proguard后,“应用程序停止工作” - “Application stopped working” after enabling Proguard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM