我在我的项目中使用ProGuard,但是它在新的Gson().toJson(请求)中提供了错误的数据;
我要出去了
{"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'是否有一种新的建议的ProGuard配置2:转换器-gson在安卓?
发布于 2016-07-28 10:09:18
用@Keep注释JSON模型类。
发布于 2018-11-20 11:14:52
在您想要的类(如authToken )上使用android @连续不断的注释
@Keep
data class AuthToken(
var access_token: String,
var token_type: String,
var expires_in: String,
var userName: String,
var issued: String,
var expires: String) {}然后在ProGuard中添加以下一行:
如果使用雄激素
-keep @androidx.annotation.Keep public class *否则
-keep @android.support.annotation.Keep public class *发布于 2017-05-12 15:10:47
在我的例子中,我使用Moshi作为JSON的Retrofit,但问题是一样的。它在调试中工作,但是在使用ProGuard构建之后,使用该模型的RecyclerView与一个NullPointerException一起崩溃,因为列表中满是空的模型对象,因为Moshi不识别任何字段。我认为同样的事情也发生在Gson身上。
一种解决方案是用相应的序列化名称对每个字段进行注释:
@Json(name="username") String username;这样,ProGuard就可以在不中断转换的情况下混淆变量名。
另一个解决方案是像道奇建议的那样,在proguard-rules.pro文件中添加一个“保持”选项。
-keep public class com.example.model.Userhttps://stackoverflow.com/questions/38631754
复制相似问题