Proguard混淆后,data class 空指针

现象

开启混淆后shrinkResources true后,访问如下代码的textColor触发了空指针异常。

data class LyricStyleConfig(

    /** 通用配置 */
    @SerializedName("font")              val font: String?,
    @SerializedName("fontID")            val fontId: Int?,
    @SerializedName("textSize")          val textSize: Int,
    @SerializedName("textSpacing")       val textSpacing: Int,

    /** 第一行字相关配置 */
    @SerializedName("textColor")         val textColor: String?,
    @SerializedName("shadowOffset")      val shadowOffset: Float,
    @SerializedName("shadowColor")       val shadowColor: String?,
    @SerializedName("shadowBlurRadius")  val shadowBlurRadius: Float,
    @SerializedName("strokeWidth")       val strokeWidth: Float,
    @SerializedName("strokeColor")       val strokeColor: String?
)

异常信息

E/AndroidRuntime: FATAL EXCEPTION: GLThread 1777
    Process: com.tencent.karaoke, PID: 13043
    java.lang.NullPointerException: throw with null exception
        at com.tencent.intoo.effect.lyric.ext.intoo.parse.config.g.b(Unknown Source:3)
        at com.tencent.intoo.effect.lyric.a.a.f.(SourceFile:25)
        at com.tencent.intoo.effect.lyric.a.a.h.a(SourceFile:120)
        at com.tencent.intoo.effect.lyric.a.a.a.a(SourceFile:89)
        at com.tencent.intoo.effect.lyric.a.a.a.a(SourceFile:41)
        at com.tencent.intoo.effect.lyric.e.a(SourceFile:137)
        at com.tencent.intoo.effect.lyric.a.a.c.a(SourceFile:57)
        at com.tencent.karaoke.l.a.b.c.a(SourceFile:38)
        at com.tencent.karaoke.l.a.b.c.a(SourceFile:14)
        at com.tencent.intoo.effect.kit.f.e(SourceFile:117)
        at com.tencent.intoo.effect.kit.e$a.a(SourceFile:40)
        at com.tencent.intoo.effect.kit.b.onDrawFrame(SourceFile:99)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1575)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270)

依据:https://stackoverflow.com/questions/56142150/fatal-exception-java-lang-nullpointerexception-in-release-build

R8 和 Gson 之间不兼容,导致经过R8处理的class,字段值为空。

解决方案一
禁用R8

# gradle.properties
android.enableR8=false

解决方案二
添加混淆规则,Gson 数据类不做混淆

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName ;
}

你可能感兴趣的:(Proguard混淆后,data class 空指针)