Android Moshi KotlinJsonAdapterFactory在启用缩小后无法解析Json

Android Moshi KotlinJsonAdapterFactory在启用缩小后无法解析Json,android,kotlin,minify,moshi,Android,Kotlin,Minify,Moshi,我开发了一个Android应用程序,使用Moshi作为其依赖项之一 今天我想为这个项目启用minify。因此,我在我的build.gradle中设置了minifyEnabled true 之后,我发现来自服务器的所有响应都变为空 首先,我使用Reformation2调用API。Response.body()中的JSON主体不为null,并且具有正确的值 响应主体如下(简化): 我正在使用下面的代码将其转换为我自己的对象: val someResponse = Moshi.Builder().ad

我开发了一个Android应用程序,使用Moshi作为其依赖项之一

今天我想为这个项目启用minify。因此,我在我的
build.gradle
中设置了
minifyEnabled true

之后,我发现来自服务器的所有响应都变为空

首先,我使用Reformation2调用API。
Response.body()
中的JSON主体不为null,并且具有正确的值

响应主体如下(简化):

我正在使用下面的代码将其转换为我自己的对象:

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自述部分中指定的proguard规则,即和

为什么以及如何解决这个问题

以下是我完整的
proguard规则。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://stackoverflow.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.**
#在此处添加特定于项目的程序规则。
#您可以使用
#在build.gradle中设置proguardFiles。
#
#有关详细信息,请参阅
#   http://developer.android.com/guide/developing/tools/proguard.html
#如果您的项目使用带JS的WebView,请取消注释以下内容
#并为JavaScript接口指定完全限定的类名
#类别:
-keepclassmembers类fqcn.of.javascript.interface.for.webview{
公众*;
}
#取消对此的注释以保留行号信息
#调试堆栈跟踪。
-keepattributes源文件,LineNumberTable,*注释*
#如果保留行号信息,请将其取消注释为
#隐藏原始源文件名。
#-重命名SourceFileAttribute源文件
#碰撞:https://docs.fabric.io/android/crashlytics/dex-and-proguard.html
-keepattributes*注释*
-keep public class*扩展java.lang.Exception
#https://stackoverflow.com/questions/36816521/is-the-format-of-the-data-held-in-kotlin-metadata-documented-anywhere
-唐特旺·科特林**
-dontwarn kotlin.reflect.jvm.internal**
-保持类kotlin.reflect.jvm.internal.*{*;}
-保持类kotlin.Metadata{*;}
-keepclassmembers公共类com.example.app.*{
公共合成;
}
-keepclassmembers类kotlin.Metadata{
公众;
}
-keepclassmembers类**$WhenMappings{
;
}
-保留类kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl
#所有型号
-保留类com.example.app.models**
#######改造#######
#https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro
#改型不会对泛型参数进行反射。InnerClass需要使用签名和
#使用InnerClass需要EnclosingMethod。
-keepattributes签名、内部类、封闭方法
#改型会对方法和参数注释进行反思。
-keepattributes RuntimeVisibleAnnotations,RuntimeVisibleParameterAnnotations
#优化时保留服务方法参数。
-keepclassmembers、allowshrinking、AllowFusion接口*{
@2.http.*;
}
#忽略用于构建工具的注释。
-dontwarn org.codehaus.mojo.animal_sniffer.ignorejrrequirement
#忽略JSR305注释以嵌入可空性信息。
-dontwarn javax.annotation**
#由NoClassDefFoundError try/catch保护,仅在类路径上使用。
-dontwarn kotlin.单位
#只能由Kotlin使用的顶级函数。
-dontwarn改装2.Kotlin
#在R8完全模式下,它看不到改型接口的子类型,因为它们是用代理创建的
#并将所有潜在值替换为null。显式保留接口可防止出现这种情况。
-if接口*{@reformation2.http.*;}
-保持,AllowFusion接口
#######OkHttp3######
#https://github.com/square/okhttp/blob/master/okhttp/src/main/resources/META-INF/proguard/okhttp3.pro
#JSR305注释用于嵌入可空性信息。
-dontwarn javax.annotation**
#资源使用相对路径加载,因此必须保留此类的包。
-keepnames类okhttp3.internal.publicsuffix.PublicSuffixDatabase
#动物嗅探器编译依赖项,以确保API与旧版本的Java兼容。
-dontwarn org.codehaus.mojo.animal_sniffer*
#OkHttp平台仅在JVM上和Conscrypt依赖项可用时使用。
-dontwarn okhttp3.internal.platform.ConscryptPlatform
######奥基奥######
#https://github.com/square/okio/blob/master/okio/src/jvmMain/resources/META-INF/proguard/okio.pro
#动物嗅探器编译依赖项,以确保API与旧版本的Java兼容。
-dontwarn org.codehaus.mojo.animal_sniffer*
####摩希####
#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
#JSR305注释用于嵌入可空性信息。
-dontwarn javax.annotation**
-keepclassswithmembers类*{
@com.squareup.moshi.*;
}
-keep@com.squareup.moshi.JsonQualifier接口*
#Enum字段名由集成EnumJsonAdapter使用。
#用@JsonClass(generateAdapter=false)注释枚举以将其用于Moshi。
-keepclassmembers@com.squareup.moshi.JsonClass*扩展了java.lang.Enum{
;
}
#@JsonClass类型的名称用于查找生成的适配器。
-keepnames@com.squareup.moshi.JsonClass类*
#如果保留注释类型,则保留生成的JSONAdapter。
-if@com.squareup.moshi.JsonClass类*
-保持类JsonAdapter{
(...);
;
}
-if@com.squareup.moshi.JsonClass类**$*
-保持课堂适应性{
(...);
;
}
-if@com.squareup.moshi.JsonClass类**$*$*
-保持类的适应性{
(...);
;
}
-if@com.squareup.moshi.JsonClass类**$*$*$*
-保持类的自适应性{
(...);
;
}
-if@com.squareup.moshi.JsonClass类**$*$*$*$*
-保持
# 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://stackoverflow.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.**
# Moshi
-keepclassmembers class ** {
  @com.squareup.moshi.FromJson *;
  @com.squareup.moshi.ToJson *;
}
-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.** { *; }
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.2"
@JsonClass(generateAdapter = true)
data class MyModel(...)
@JsonClass(generateAdapter = false)
enum class MyEnumClass { ... }