現(xiàn)象
開啟混淆后shrinkResources true
后,訪問如下代碼的textColor
觸發(fā)了空指針異常辰企。
data class LyricStyleConfig(
/** 通用配置 */
@SerializedName("font") val font: String?,
@SerializedName("fontID") val fontId: Int?,
@SerializedName("textSize") val textSize: Int,
@SerializedName("textSpacing") val textSpacing: Int,
/** 第一行字相關(guān)配置 */
@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.<init>(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)
R8 和 Gson 之間不兼容,導(dǎo)致經(jīng)過R8處理的class况鸣,字段值為空牢贸。
解決方案一
禁用R8
# gradle.properties
android.enableR8=false
解決方案二
添加混淆規(guī)則,Gson 數(shù)據(jù)類不做混淆
# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}