113

在一个新创建的带有 Androidx:appcompat:appcompat:1.0.0-rc01 的项目中,我得到了

java.lang.ClassNotFoundException: Didn't find class 
"android.view.View$OnUnhandledKeyEventListener" on path: DexPathList

我还添加了 configuration.all

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

这不会影响应用程序功能或崩溃。但是当应用程序运行时它总是有这个错误。请帮助我解决错误。整个堆栈跟踪如下。

I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
I/art:     at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
        at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
        at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
        at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
        at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
        at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
        at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
        at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
        at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
        at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
        at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
        at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
        at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
        at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
        at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
        at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
        at void com.connectdb.truckish.common.base.BaseActivity.onCreate(android.os.Bundle) (BaseActivity.java:44)
        at void com.connectdb.truckish.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:14)
        at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6672)
        at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1140)
        at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2612)
        at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2724)
        at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
        at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1473)
        at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
        at void android.os.Looper.loop() (Looper.java:154)
        at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6123)
        at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
        at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:867)
        at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:757)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.connectdb.truckish-2/base.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.connectdb.truckish-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[
        at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
        at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
        at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
        at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
        at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
        at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
        at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
        at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
        at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
        at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
        at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
        at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
        at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
        at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
        at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
        at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
        at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
        at void com.connectdb.truckish.common.base.BaseActivity.onCreate(android.os.Bundle) (BaseActivity.java:44)
        at void com.connectdb.truckish.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:14)
        at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6672)
        at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1140)
        at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2612)
        at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2724)
        at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
        at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1473)
        at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
        at void android.os.Looper.loop() (Looper.java:154)
        at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6123)
        at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
        at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:867)
        at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:757)
4

7 回答 7

97

正如 ALFlanagan 在评论中提到的那样,问题在于android.support.v4.view.ViewCompat它没有View.OnUnhandledKeyEventListener在新的 androidx 包结构中实现,并且仅在支持 lib 结构中的 API 28 开始实现它(至少在版本 28.0.0 中)。因此,警告出现在 API <28 的设备上,而不出现在那些 >=28 的设备上。

ViewCompat.class这是来自支持包结构的类中的相关代码:

@RequiresApi(28)
private static class OnUnhandledKeyEventListenerWrapper implements OnUnhandledKeyEventListener {
    private ViewCompat.OnUnhandledKeyEventListenerCompat mCompatListener;

    OnUnhandledKeyEventListenerWrapper(ViewCompat.OnUnhandledKeyEventListenerCompat listener) {
        this.mCompatListener = listener;
    }

    public boolean onUnhandledKeyEvent(View v, KeyEvent event) {
        return this.mCompatListener.onUnhandledKeyEvent(v, event);
    }
}

我想不出任何简单的方法来解决这个警告。

于 2018-10-23T16:58:48.740 回答
17

我面临同样的问题,但只是尝试升级 appcompat 的依赖项,它没有更多的错误日志。我希望很快会有新的稳定版本。 https://developer.android.com/jetpack/androidx/releases/appcompat#1.3.0-alpha02

实现 'androidx.appcompat:appcompat:1.3.0-alpha02'

于 2020-07-20T08:57:56.093 回答
0

您可以使用 aspectJ 通过修改 androidx 字节码来消除此问题。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class UnHandleKeyEventAspectJ {
    public static final String TAG="UnHandleKeyEventAspectJ";
    @Pointcut("execution(* androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener))")
public void kotlinClassInit() {

}
    @Around("kotlinClassInit()")
    public void addTransaction(ProceedingJoinPoint joinPoint){
        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
于 2020-03-13T03:02:58.917 回答
0

这是androidx中的一个错误。我发现错误会导致调试版本滞后,但不会显示或减慢生产版本。你现在可以忽略它。

于 2018-09-02T12:13:29.113 回答
-2

上面的答案是正确的。避免这种情况的唯一方法是删除对 AppCompat 的引用 - 例如,我更改为 FragmentActivity。坏消息是新素材库全部参考和体验的问题。我不希望这发生在我的创业公司中——后来的打击还不错。这只是一个警告 - 但确实会对性能产生影响,而且它非常虚假并且大 G 处理不当。

于 2019-05-13T23:33:50.130 回答
-4

该注释@RequiresApi(28)实际上意味着,唯一build.gradle可以摆脱日志垃圾邮件的配置是将minSdkVersion至少提高到28.

android {    
    defaultConfig {
        targetSdkVersion 28
        compileSdkVersion 28
        minSdkVersion 28
    }
}

从错误跟踪器:

此错误已作为 Won't Fix (Intended Behavior) 关闭,这些错误可以忽略。

于 2019-05-24T07:50:17.743 回答
-6

您可以避免它,因为该特定类仅在 Android 9 中。

于 2018-08-15T22:53:09.543 回答