我在我的项目中使用 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/