签名 APK 中的正文参数属性名称更改

body params properties name change in signed APK

我创建了一个 android 应用程序,我使用改造作为 REST 客户端。

当我在 phone 上安装应用程序时,请求发送没有任何问题,但是当我构建签名 APK 时,我不知道为什么属性名称在发送请求时发生变化

我有一个class,其中包含一个“phone”字段,那么请求正文必须是

{
    "phone": "123456"
}

但是在日志中,我看到正文变成了

{
    "a": "123456"
}

问题只出现在签名的 APK 中,

有什么帮助吗?

快速修复将 minifyEnabled 禁用为 false。

release {
        minifyEnabled false
        ...
    }

OR

如果您不希望您的 class 成员被混淆,请使用 Gson 提供的 SerializedName 注释。例如:

public class ClassNameHere
{
   @SerializedName("phone")
   public String phone;
     ...

}

此外,请确保您也为 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
-keep class com.mypackage.ClassNameHere.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
# 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 ----------

有关详细信息,请阅读混淆器规则

看起来像是 R8/proguard 问题。

对于调试版本,它通常是禁用的,因此不会进行优化和混淆,但对于发布 apk,它通常是打开的(这是应该的!)。

// this line in build.gradle
minifyEnabled true

这不取决于改装,而是取决于您的 json 转换器、GSON 或 Moshi 或其他任何东西。您需要“保留”(通过调整 proguard 规则)您的模型 类 或正确注释它们,以便转换器按预期工作。

对于 moshi,通常的方法是通过注释处理器生成模型的 json 转换器

@JsonClass(generateAdapter = true) 
class PhoneModel(@Json(name = "phone") val phoneField: String)

但是请查看您转换器在 proguard / R8 上的自述文件页面