我想把kotlin gradle插件从升级1.4.32
到1.5.20
,但是在较低的android版本设备上出现了一些代码错误,例如Xiaomi 5.1.1
& Oppo 6.0.1
& Pixel2 6.0
,但在Android 10设备上是正常的。
错误信息:
java.lang.IncompatibleClassChangeError: Couldn't find com.example.kotlinupgradedemo.ProgressDialogKt.<clinit>[]
at libcore.reflect.AnnotationAccess.indexToMethod(AnnotationAccess.java:608)
at libcore.reflect.AnnotationAccess.getEnclosingMethodOrConstructor(AnnotationAccess.java:405)
at java.lang.Class.isLocalClass(Class.java:1334)
at java.lang.Class.getCanonicalName(Class.java:378)
at androidx.lifecycle.Lifecycling.resolveObserverCallbackType(Lifecycling.java:153)
at androidx.lifecycle.Lifecycling.getObserverConstructorType(Lifecycling.java:146)
at androidx.lifecycle.Lifecycling.lifecycleEventObserver(Lifecycling.java:83)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.<init>(LifecycleRegistry.java:347)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:174)
at com.example.kotlinupgradedemo.ProgressDialogKt.showProgress(ProgressDialog.kt:36)
at com.example.kotlinupgradedemo.MainActivity.onCreate(MainActivity.kt:12)
我的一些代码(ProgressDialog.kt
):
private val ownerToProgressMap = mutableMapOf<LifecycleOwner, Dialog>()
private val progressCleaner = object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy(owner : LifecycleOwner) {
ownerToProgressMap.remove(owner)?.dismiss()
owner.lifecycle.removeObserver(this)
}
}
fun LifecycleOwner.showProgress() {
val context = when (this) {
is Activity -> this
is Fragment -> this.context
else -> null
} ?: return
ownerToProgressMap[this]
?.apply { show() }
?: Dialog(context).also {
it.setTitle("Tips")
it.show()
}.let {
ownerToProgressMap[this] = it
this.lifecycle.addObserver(progressCleaner)
}
}
fun LifecycleOwner.dismissProgress() {
ownerToProgressMap[this]?.dismiss()
}
我只是调用它MainActivity
:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showProgress()
}
}
完整代码见demo