【问题标题】:Getting wrong keys in json while calling retrofit after enabling proguard在启用 proguard 后调用改造时在 json 中获取错误的键
【发布时间】:2020-08-20 02:26:27
【问题描述】:

当我调用改造 without proguard 我得到如下 json : D/OkHttp: {"UserPin":"123456","password":"123456"}添加 proguard 之后,它会给出以下 json : D/OkHttp: {"a":"123456","b":"123456"}

Proguard 更改 json 中的键

这是我的代码:-

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();
                }

            }
        });

    }

请帮帮我

【问题讨论】:

  • 解决了吗?
  • 我已经将所有 POJO 类放入 Proguard 中,但错过了在 proguard 中添加模型的基本包。我只是简单地添加 ` -keep class com.example.test.** { ; }` 在 proguard 中包含所有 POJO 类,例如 `-keep class com.assettagging.test.LoginUser* { *; }` 并且成功了。
  • 请查看我的回答。

标签: android json gson retrofit okhttp


【解决方案1】:

检查以确保也为 gson 配置了 proguard。您需要确保与 gson 一起使用的 POJO 没有被混淆,并且注释没有被剥离。

注意:您应该在下面的示例中将 com.google.gson.examples.android.model.** { *; } 替换为您的模型类。

来自 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  ----------

【讨论】:

    【解决方案2】:

    您应该添加 proguard 规则。

    -ignorewarnings
    -dontwarn okio.**
    
    -dontwarn retrofit2.**
    -keep class retrofit2.** { *; }
    -keepattributes Signature
    -keepattributes Exceptions
    
    -keepattributes *Annotation*
    
    -keepclasseswithmembers class * {
        @retrofit2.http.* <methods>;
    }
    

    和登录类

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

    对于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  ----------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-01
      • 2015-12-25
      • 2019-05-04
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 2018-12-17
      • 2016-06-01
      相关资源
      最近更新 更多