android - ProGuard for Android 和 Retrofit2 Converter Gson?

标签 android gson proguard retrofit2 android-proguard

我在我的项目中使用 ProGuard,但它在 new Gson().toJson(Request) 中提供了错误的数据;

我要出去了

{"a":"manage","b":"689184d4418b6d975d9a8e53105d3382","c":"10","d":"76"}

代替

{"username":"manage","password":"689184d4418b6d975d9a8e53105d3382","value":"10","store":"76"}

我的 ProGuard 规则

-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
-dontwarn sun.misc.Unsafe
-dontwarn org.w3c.dom.bootstrap.DOMImplementationRegistry
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclassmembers class rx.internal.util.unsafe.** {
    long producerIndex;
    long consumerIndex;
}

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
-keep class com.google.gson.** { *; }
-keep class com.google.inject.** { *; }

我正在使用

 compile 'com.squareup.retrofit2:converter-gson:2.0.0'

在 Android 中是否有新的推荐 ProGuard 配置用于 retrofit2:converter-gson?

最佳答案

你要么想用 gson 保留你正在使用的类,要么使用 @SerializedName注释。

选项 1(继续上课)

// all classes in a package
-keep class com.example.app.json.** { *; }
// or a specific class
-keep class com.example.app.json.SpecificClass { *; }

选项 2(使用 @SerializedName ):

public class YourJsonClass{
   @SerializedName("name") String username;

   public MyClass(String username) {
     this.username = username;
   }
 }

使用第二个选项,proguard 仍然混淆了类名和字段名,但 gosn 可以使用注释为每个字段获取正确的名称

关于android - ProGuard for Android 和 Retrofit2 Converter Gson?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38631754/

相关文章:

android - fragment 菜单项在滑动时未正确显示,但会显示在选项卡选择上 - 无菜单 xml

android - 如何在使用 Apache Ant 编译的项目中包含 ActionBar

kotlin - java.lang.IllegalStateException :JSON OBJECT and JSON ARRAY when parsing Json using GSON

gradle - 如何禁用外部依赖 jar 的 Proguard 设置?

java - 我怎样才能对 ProGuard 进行良好的混淆处理?

android - 如何将原始文件夹中的自定义声音添加到 RingtonePreference 中以显示?

java - 我应该为 POST HTTP 请求的 Content-Length 使用什么值?

java - 将 JSON 类反序列化为不同名称的 Java 类

java - 无法使用 GSON 对 JSON 数组内的 JSON 数组进行序列化

android - 使用 Android 库项目设置 ProGuard