June 23, 2020

解決 Rejecting re-init on previously-failed class <androidx.core.view.ViewCompat$2> 警告

Tags: androiderror | (1 min read)
解決 Rejecting re-init on previously-failed class <androidx.core.view.ViewCompat$2> 警告

新建一個安卓項目, compileSdkVersion 29,然後啟動MainActivity就看到 Logcat中有警告信息:

Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;

雖然不會拋出錯誤或異常,但是看到也很噁心。

解決方法,在build.gradle文件中添加以下配置

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == "androidx.appcompat") {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "1.+"
            }
        }
    }
}
build.gradle (app)

延伸閱讀: