首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Moshi Json注释不适用于pro卫士。

Moshi Json注释不适用于pro卫士。
EN

Stack Overflow用户
提问于 2020-02-11 09:58:41
回答 2查看 3.4K关注 0票数 4

我按照https://github.com/square/moshi添加了对moshi和pro卫士规则的gradle依赖,然后编写代码进行验证。

代码语言:javascript
复制
data class Car(
    @Json(name = "low_speed")  val lowSpeed: Int,
    @Json(name = "high_speed") val highSpeed: Int
)
代码语言:javascript
复制
val moshi = Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()
val jsonAdapter = moshi.adapter(Car::class.java)

val json = "{\"low_speed\": 10, \"high_speed\": 20}"
val car = jsonAdapter.fromJson(json)

if (car != null) {
    Toast.makeText(this, "${car.lowSpeed}+${car.highSpeed}", Toast.LENGTH_LONG).show()
}

当我在调试模式下运行它时,它显示了"10+20",这是预期的,但是当我在发布模式下运行时(已经启用了0+0),我看到了"0+0“。

我的卫兵-规则.专业档案:

代码语言:javascript
复制
# Retain generic type information for use by reflection by converters     and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

# Common Stuff to Keep
-keepattributes *Annotation*
-keepattributes JavascriptInterface

# OkHttp
-dontwarn javax.annotation.**
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-dontwarn org.codehaus.mojo.animal_sniffer.*
-dontwarn okhttp3.internal.platform.ConscryptPlatform

# okIo
-dontwarn okio.**

#moshi
# JSR 305 annotations are for embedding nullability information.
-keepclasseswithmembers class * {
    @com.squareup.moshi.* <methods>;
}

-keep @com.squareup.moshi.JsonQualifier interface *

# Enum field names are used by the integrated EnumJsonAdapter.
# values() is synthesized by the Kotlin compiler and is used by EnumJsonAdapter indirectly
# Annotate enums with @JsonClass(generateAdapter = false) to use them with Moshi.
-keepclassmembers @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
    <fields>;
    **[] values();
}

#moshi-kotlin
-keep class kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl

-keepclassmembers class kotlin.Metadata {
    public <methods>;
}

我的build.gradle依赖性:

代码语言:javascript
复制
// moshi
implementation("com.squareup.moshi:moshi:1.9.2")
implementation("com.squareup.moshi:moshi-kotlin:1.9.2")

// okhttp
implementation "com.squareup.okhttp3:okhttp:3.10.0"

我想我错过了莫希和保安的一些东西,但我真的不知道这是什么

EN

回答 2

Stack Overflow用户

发布于 2022-06-02 00:09:27

使用@Keep注释来防止目标类的小型化。

我的案子跟OP描述的有点不同。我的应用程序在尝试用moshi jsonAdapter解析JSON响应时崩溃了。

代码语言:javascript
复制
val jsonAdapter = Moshi.Builder().add(KotlinJsonAdapterFactory()).build().let { moshi ->
    moshi.adapter(WeatherResponse::class.java)
}

..。

代码语言:javascript
复制
val weatherResponse = response.body?.source()?.let { 
    jsonAdapter.nullSafe().fromJson(it) 
}

我通过将@Keep注释应用于WeatherResponse类定义和由WeatherResponse实例化的所有类来解决这个问题。

代码语言:javascript
复制
@Keep
data class Weather(
...

这似乎解决了我的问题。

票数 1
EN

Stack Overflow用户

发布于 2021-02-02 16:14:40

试着添加:

代码语言:javascript
复制
@JsonClass(generateAdapter = false)

将这些行添加到proguard:

代码语言:javascript
复制
-keepclassmembernames @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
    <fields>;
}

信贷银行( Credit -> https://github.com/square/moshi/issues/689 )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60166217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档