Android Studio Flamingo 2022.2.1 Caused by java.lang.RuntimeException: Missing type parameter

如果你 Android Studio 升級到 Flamingo 2022.2.1 版本,請注意以下內容:

升級後的 Android Studio 使用 Gradle 8.0.0。這個版本的 Gradle 引入了 R8 完整模式(R8 full mode),該模式預設是開啟的。R8 是一個用於縮減、模糊處理和最佳化應用程式的工具。這表示你的應用程式在編譯時會受到更強大的最佳化處理。

你應該特別注意您的應用程式是否會受到這些變化的影響。一個普遍受到影響的第三方 lib 套件是 GSON。GSON 是一個常用的 JSON 序列化/反序列化庫,它可能會出現以下相關錯誤,例如:

Caused by java.lang.RuntimeException: Missing type parameter

或是因為 SerializedName 導致在獲取資料欄位時呈現 null,目前 KT 加入 Google 官方提供新的 Proguard 規則,已確定 Gson 在混淆時能正常工作,避免出現相關問題。完整設定請參考下方資訊連結內容。

最後,鼓勵大家在遇到問題時彼此分享解決方案,透過共享經驗,省去踩坑的除錯時間,歡迎大家留言,討論您遇到的問題以及您是如何解決的,一起改善開發流程。

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 TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-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>;
}

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

##---------------End: proguard configuration for Gson  ----------

Retrofit

# 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

# Keep annotation default values (e.g., retrofit2.http.Field.encoded).
-keepattributes AnnotationDefault

# 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
-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>

# Keep inherited services.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface * extends <1>

# With R8 full mode generic signatures are stripped for classes that are not
# kept. Suspend functions are wrapped in continuations where the type argument
# is used.
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

# R8 full mode strips generic signatures from return types if not kept.
-if interface * { @retrofit2.http.* public *** *(...); }
-keep,allowoptimization,allowshrinking,allowobfuscation class <3>

參考資料

R8 縮減、模糊處理及最佳化應用程式
https://developer.android.com/studio/build/shrink-code?hl=zh-tw

GSON
https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

Retrofit
https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro

這個網誌中的熱門文章

[轉貼]傳說中的100句英語可以幫你背7000單詞

[推薦] 翻譯機 (無敵 CD-318、CD-222 電子辭典)

[推薦]目前當代具有相當權威的英語辭典整理收集

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

16天記下7000單字