java.lang.IncompatibleClassChangeError,将kotlin gradle插件升级到1.5.20后
java.lang.IncompatibleClassChangeError,After Upgrade kotlin gradle plugin to 1.5.20
我想从1.4.32
to 1.5.20
升级kotlin gradle插件,但是一些代码在低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
特别是线条
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));
}
然后查看 Class::getDeclaredMethod 文档。
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 全局 属性 和伴随对象 属性 初始化编译为 Java 静态初始化块又名 .
拼图完成。将 LifecycleObserver
声明从 static 属性 移到其他地方,问题将得到解决。
为什么 Android 只有 5 和 6?我认为这只是 getDeclaredMethod
实施差异。我们也仅在 Android 5 和 6 上看到崩溃。
我想从1.4.32
to 1.5.20
升级kotlin gradle插件,但是一些代码在低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
特别是线条
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));
}
然后查看 Class::getDeclaredMethod 文档。
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 全局 属性 和伴随对象 属性 初始化编译为 Java 静态初始化块又名
拼图完成。将 LifecycleObserver
声明从 static 属性 移到其他地方,问题将得到解决。
为什么 Android 只有 5 和 6?我认为这只是 getDeclaredMethod
实施差异。我们也仅在 Android 5 和 6 上看到崩溃。