致命异常:发布版本中的java.lang.NullPointerException

pra*_*kar 9 android gson kotlin retrofit r8

我在应用程序的发布版本中遇到一个奇怪的问题。这是我的例外

Fatal Exception: java.lang.NullPointerException`
throw with null exception
in.hopq.hopq.authentication.models.AppUpdateSourceDO$AppUpdate.getMinAllowedVersion (AppUpdateSourceDO.java:3)
in.hopq.hopq.authentication.activities.SplashActivity$onCreate$1.onChanged (SplashActivity.java:48)
in.hopq.hopq.authentication.activities.SplashActivity$onCreate$1.onChanged (SplashActivity.java:31)
Run Code Online (Sandbox Code Playgroud)

Pojo文件

data class AppUpdateSourceDO(
    @SerializedName("app_update")
    val appUpdate: AppUpdate,
    @SerializedName("message")
    val message: String,
    @SerializedName("success")
    val success: Boolean
) {
data class AppUpdate(
        @SerializedName("excluded_versions")
        val excludedVersions: List<ExcludedVersion>,
        @SerializedName("min_allowed_version")
        val minAllowedVersion: Int,
        @SerializedName("min_allowed_version_ios")
        val minAllowedVersionIos: String,
        @SerializedName("recommended_version")
        val recommendedVersion: Int?
) {
    data class ExcludedVersion(
            @SerializedName("version")
            val version: String
    )
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的保护文件

##OKHTTP3
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-dontnote okhttp3.**
-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
Run Code Online (Sandbox Code Playgroud)

Abh*_*hek 24

R8完全禁用可能不是一个好主意。但是通过在您的Proguard 规则文件中添加以下行可能会解决问题——

-keepclassmembers,allowobfuscation class * {
    @com.google.gson.annotations.SerializedName <fields>;
  }
-keep,allowobfuscation @interface com.google.gson.annotations.SerializedName
Run Code Online (Sandbox Code Playgroud)

谷歌开发人员之一也建议将其作为所需的解决方案。您可以在此处找到他的答案,也可以关注围绕它的整个讨论。


pra*_*kar 19

终于解决了这个问题。这是由于新的R8代码混淆。只需将其添加到gradle.properties文件即可从项目中禁用它

android.enableR8=false

另外,您将此添加到您的proguard规则文件中。

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}
Run Code Online (Sandbox Code Playgroud)

但是,将其添加到proguard并没有真正解决。

  • 似乎R8仍然存在此错误https://issuetracker.google.com/issues/120277396希望他们尽快解决此问题。 (2认同)

and*_*per 9

似乎 GSON 和 R8 不能很好地协同工作,它写在这里

实际上,以下内容也应该有效:

-keepclassmembers,allowobfuscation class * { @com.google.gson.annotations.SerializedName ; -保持,允许混淆@interface com.google.gson.annotations.SerializedName

这将缩小(混淆)字段和属性的名称,以进一步减小最终 APK 的大小。

此外,您可以在Gson 示例中看到以下规则:

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

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

##---------------End: proguard configuration for Gson  ----------
Run Code Online (Sandbox Code Playgroud)