2

我想把kotlin gradle插件从升级1.4.321.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

4

1 回答 1

2

看看AnnotationAccess.java

尤其是线条

    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文档。

返回一个 Method 对象,该对象反映此 Class 对象表示的类或接口的指定声明方法。...如果名称是“<init>”或“<clinit>”,则会引发 NoSuchMethodException。

Kotlin 全局属性和伴随对象属性初始化编译为 Java 静态初始化块,即<clinit>

拼图完成。将LifecycleObserver声明从静态属性移到其他地方,问题将得到解决。

为什么只有 Android 5 和 6?我认为这只是getDeclaredMethod实施差异。我们也仅在 Android 5 和 6 上看到崩溃。

于 2021-09-27T15:55:51.693 回答