I want upgrade kotlin gradle plugin from 1.4.32
to 1.5.20
,but some code occurred error in lower android version device,such as Xiaomi 5.1.1
& Oppo 6.0.1
& Pixel2 6.0
, but it normal in Android 10 devices.
The error info:
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)
My some code(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()
}
I just call it in MainActivity
:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showProgress()
}
}
For the complete code, see demo
Take a look to AnnotationAccess.java.
Especially lines
try {
return name.equals("<init>")
? declaringClass.getDeclaredConstructor(parametersArray)
: declaringClass.getDeclaredMethod(name, parametersArray);
} catch (NoSuchMethodException e) {
throw new IncompatibleClassChangeError("Couldn't find " + declaringClass.getName()
+ "." + name + Arrays.toString(parametersArray));
}
Then look to Class::getDeclaredMethod documentation.
Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object. ... If the name is "<init>"or "<clinit>" a NoSuchMethodException is raised.
Kotlin global property and companion object property initialization compiles to Java static initialization block aka <clinit>.
Puzzle complete. Move LifecycleObserver
declaration from static property somewhere else and problem will be solved.
Why Android 5 and 6 only? I think it's just getDeclaredMethod
implementation differences. We see crashes on Android 5 and 6 only too.