Moshi KotlinJsonAdapterFactory 在启用 minify 后无法解析 Json
Posted
技术标签:
【中文标题】Moshi KotlinJsonAdapterFactory 在启用 minify 后无法解析 Json【英文标题】:Moshi KotlinJsonAdapterFactory cannot parse Json after enabled minify 【发布时间】:2019-11-23 20:56:27 【问题描述】:我开发了一个 android 应用,使用 Moshi 作为其依赖项之一。
今天我想为这个项目启用 minify。所以我在我的build.gradle
中设置了minifyEnabled true
。
之后,我发现所有来自服务器的响应都变成了 null。
首先,我使用 Retrofit2 来调用 API。 Response.body()
中的 JSON 正文不为 null 并且具有正确的值。
响应正文如下(简化):
"status":"success","data":"user": "I am a user"
我正在使用下面的代码将其转换为我自己的对象:
val someResponse = Moshi.Builder().add(KotlinJsonAdapterFactory()).build().adapter(SomeResponse::class.java).fromJson(theJsonString)
而SomeResponse
的代码:
class SomeResponse
@Json(name="status")
var status: String? = null
@Json(name="data")
var data: User? = null
然后我简单地用Log.i("Moshi", "$someResponse.status"
查看值,结果是null
。
我已经包含了 Moshi Github 的 README 部分中指定的 proguard 规则,即this one 和this one。
为什么以及如何解决这个问题?
供参考,以下是我的完整proguard-rules.pro
:
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the javascript interface
# class:
-keepclassmembers class fqcn.of.javascript.interface.for.webview
public *;
# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable, *Annotation*
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#Crashlytics: https://docs.fabric.io/android/crashlytics/dex-and-proguard.html
-keepattributes *Annotation*
-keep public class * extends java.lang.Exception
#https://***.com/questions/36816521/is-the-format-of-the-data-held-in-kotlin-metadata-documented-anywhere
-dontwarn kotlin.**
-dontwarn kotlin.reflect.jvm.internal.**
-keep class kotlin.reflect.jvm.internal.** *;
-keep class kotlin.Metadata *;
-keepclassmembers public class com.example.app.**
public synthetic <methods>;
-keepclassmembers class kotlin.Metadata
public <methods>;
-keepclassmembers class **$WhenMappings
<fields>;
-keep class kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl
#All models
-keep class com.example.app.models.**
#######Retrofit#######
#https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro
# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod
# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface *
@retrofit2.http.* <methods>;
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**
# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit
# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.KotlinExtensions
# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * @retrofit2.http.* <methods>;
-keep,allowobfuscation interface <1>
#######OkHttp3######
#https://github.com/square/okhttp/blob/master/okhttp/src/main/resources/META-INF/proguard/okhttp3.pro
# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform
######Okio######
#https://github.com/square/okio/blob/master/okio/src/jvmMain/resources/META-INF/proguard/okio.pro
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
####Moshi####
#https://github.com/square/moshi/blob/master/moshi/src/main/resources/META-INF/proguard/moshi.pro
#https://github.com/square/moshi/blob/master/kotlin/reflect/src/main/resources/META-INF/proguard/moshi-kotlin.pro
# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**
-keepclasseswithmembers class *
@com.squareup.moshi.* <methods>;
-keep @com.squareup.moshi.JsonQualifier interface *
# Enum field names are used by the integrated EnumJsonAdapter.
# Annotate enums with @JsonClass(generateAdapter = false) to use them with Moshi.
-keepclassmembers @com.squareup.moshi.JsonClass class * extends java.lang.Enum
<fields>;
# The name of @JsonClass types is used to look up the generated adapter.
-keepnames @com.squareup.moshi.JsonClass class *
# Retain generated JsonAdapters if annotated type is retained.
-if @com.squareup.moshi.JsonClass class *
-keep class <1>JsonAdapter
<init>(...);
<fields>;
-if @com.squareup.moshi.JsonClass class **$*
-keep class <1>_<2>JsonAdapter
<init>(...);
<fields>;
-if @com.squareup.moshi.JsonClass class **$*$*
-keep class <1>_<2>_<3>JsonAdapter
<init>(...);
<fields>;
-if @com.squareup.moshi.JsonClass class **$*$*$*
-keep class <1>_<2>_<3>_<4>JsonAdapter
<init>(...);
<fields>;
-if @com.squareup.moshi.JsonClass class **$*$*$*$*
-keep class <1>_<2>_<3>_<4>_<5>JsonAdapter
<init>(...);
<fields>;
-if @com.squareup.moshi.JsonClass class **$*$*$*$*$*
-keep class <1>_<2>_<3>_<4>_<5>_<6>JsonAdapter
<init>(...);
<fields>;
-keep class kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl
-keepclassmembers class kotlin.Metadata
public <methods>;
####Mp4 Marser####
-keep class com.coremedia.iso.** *;
-keep class com.googlecode.mp4parser.** *;
-keep class com.mp4parser.** *;
-dontwarn com.coremedia.**
-dontwarn com.googlecode.mp4parser.**
####Picasso#####
#https://github.com/square/picasso/blob/master/picasso/consumer-proguard-rules.txt
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote okhttp3.internal.Platform
# java.nio.file.* usage which cannot be used at runtime. Animal sniffer annotation.
-dontwarn okio.Okio
# JDK 7-only method which is @hide on Android. Animal sniffer annotation.
-dontwarn okio.DeflaterSink
#Ignore all other 3rd party libraries for now as we don't really care about the size but more about code obfuscation.
-keep class de.hdodenhof.**
-keep class io.github.luizgrp.sectionedrecyclerviewadapter.**
-keep class q.rorbin.badgeview.**
-keep class com.theartofdev.edmodo.**
-keep class me.relex
-keep class com.tbruyelle.rxpermissions2.**
-keep class com.github.pwittchen.reactivenetwork.**
-keep class com.minimize.android.rxrecycleradapter.**
-keep class at.blogc.android.**
-keep class com.yarolegovich.**
-keep class cn.trinea.android.view.autoscrollviewpager.**
-keep class com.apkfuns.logutils.**
【问题讨论】:
自述文件在 GitHub 上有一个 proguard 部分(接近底部):github.com/square/moshi/blob/master/README.md 你实现了吗? @MarkKeen 是的,我有,刚刚更新了我的问题。还是谢谢 保留 Kotlin 的元数据注释。这就是所有应该需要的。杰克沃顿 我怀疑你对模型的规则。您可以尝试使用-keep class com.example.app.models.** *;
吗?
我在 kotlin 中使用 moshi 解析 json 时遇到了获取空值的问题。我通过使用@field:Json(name="") 而不是@Json(name="") 解决了这个问题。看看有没有帮助。
【参考方案1】:
您也可以使用 Moshi “codegen” 注释处理器。
添加依赖:
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.2"
然后用@JsonClass
注释你的模型:
@JsonClass(generateAdapter = true)
data class MyModel(...)
您还需要注释模型使用的任何枚举类,但它们不需要适配器:
@JsonClass(generateAdapter = false)
enum class MyEnumClass ...
前面提到的 here 和 here 中的 Moshi Proguard 规则也是必需的。
【讨论】:
【参考方案2】:@JeganBabu 的评论中提到了唯一对我有用的解决方案
即将@Json(name="field_name")
更改为@field:Json(name="field_name")
。
可能的原因是如果未添加field:
,则注释不会应用于转换后的Java 代码。很奇怪。
供您参考:(Moshi in kotlin) @Json vs @field:Json
【讨论】:
【参考方案3】:-keepclasseswithmembers class *
@com.squareup.moshi.* <methods>;
-keep @com.squareup.moshi.JsonQualifier interface *
-dontwarn org.jetbrains.annotations.**
-keep class kotlin.Metadata *;
-keepclassmembers class kotlin.Metadata
public <methods>;
-keepclassmembers class *
@com.squareup.moshi.FromJson <methods>;
@com.squareup.moshi.ToJson <methods>;
-keepnames @kotlin.Metadata class (Change with Yourpackagename) com.myapp.packagename.model.**
-keep class (Change with Yourpackagename) com.myapp.packagnename.model.** *;
-keepclassmembers class (Change with Yourpackagename) com.myapp.packagename.model.** *;
将 com.myapp.packagnename 更改为您的包名
或
将 moshi-kotlin 替换为 kotshi,它可以在没有特殊规则的情况下与 Proguard 配合使用,在最终的 apk 中减少数百 KB。
【讨论】:
【参考方案4】:尝试将此添加到您的 proguard-rules.pro 文件中。希望这会有所帮助。
# Moshi
-keepclassmembers class **
@com.squareup.moshi.FromJson *;
@com.squareup.moshi.ToJson *;
【讨论】:
【参考方案5】:我认为这里的解决方案很简单:在您的模型中添加 @Keep - 注释 (SomeResponse),因此编组名称不应再被混淆。 :-)
【讨论】:
以上是关于Moshi KotlinJsonAdapterFactory 在启用 minify 后无法解析 Json的主要内容,如果未能解决你的问题,请参考以下文章