Android 混淆打包后gson报错Missing type parameter

原文: Android 混淆打包后gson报错Missing type parameter - Stars-One的杂货小窝

记录一个简单的bug

在代码中使用了gson将json转为list:

val json = ""
val planList = gson.fromJson<List<PrayerPlan>>(json, object : TypeToken<List<PrayerPlan>>() {}.type)

然后线上的时候出现了以下错误

Fatal Exception: java.lang.RuntimeException: Missing type parameter.
       at com.google.gson.reflect.TypeToken.getSuperclassTypeParameter(TypeToken.java:3)
       at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:3)
       at com.professional.bibble.ui.activity.PlanActivity$initPlanView$2$planList$1.<init>(PlanActivity.java)
       at com.professional.bibble.ui.view.BottomPlayerLayout$init$2.invoke(BottomPlayerLayout.java:25)
       at com.professional.bibble.ui.view.BottomPlayerLayout$init$2.invoke(BottomPlayerLayout.java:25)
       at com.professional.bibble.ui.view.BottomPlayerLayout$sam$androidx_lifecycle_Observer$0.onChanged(BottomPlayerLayout.java:2)
       at androidx.lifecycle.LiveData.considerNotify(LiveData.java:29)
       at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:56)
       at androidx.lifecycle.LiveData.setValue(LiveData.java:14)
       at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java)
       at com.dylanc.mmkv.MMKVOwnerKt$asLiveData$1$getValue$1.onActive(MMKVOwner.kt:4)
       at androidx.lifecycle.LiveData.changeActiveCounter(LiveData.java:49)
       at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:49)
       at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:28)
       at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:23)
       at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:344)
       at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:344)
       at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:34)
       at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:14)
       at androidx.fragment.app.Fragment.performStart(Fragment.java:57)
       at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:57)
       at androidx.fragment.app.FragmentStateManager.k(FragmentStateManager.java:63)
       at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:62)
       at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:62)
       at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:36)
       at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:58)
       at androidx.fragment.app.FragmentController.dispatchStart(FragmentController.java:58)
       at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:58)
       at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java)
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1455)
       at android.app.Activity.performStart(Activity.java:8079)
       at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3676)
       at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
       at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2221)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:201)
       at android.os.Looper.loop(Looper.java:288)
       at android.app.ActivityThread.main(ActivityThread.java:7952)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1022)

实际上项目因为急着上线,没让测试进行release包的测试,不然这个问题一测就出来了

解决方法其实很简单,对应的bean已经是不会混淆,主要问题就是那个gson的类给混淆了,在proguard-rules.pro文件添加下面的混淆规则即可解决

-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
posted @ 2023-11-23 17:40  Stars-one  阅读(790)  评论(0编辑  收藏  举报