114

In a newly created project with Androidx:appcompat:appcompat:1.0.0-rc01, I'm getting the

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

I had also added 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.+"
            }
        }
    }
}

This one is not affecting the app functionality or crashes. But it always have this error when the app is running. Kindly help me to solve the error. The entire stack trace is as follows.

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)
Mohan Rex
  • 1,674
  • 2
  • 17
  • 22
  • 1
    I have the same error. Have you found any workarounds? – c0nst Sep 13 '18 at 08:51
  • No I haven't found any workaround. – Mohan Rex Sep 25 '18 at 10:43
  • 1
    I'm getting a similar but non AndroidX error `Rejecting re-init on previously-failed class java.lang.Class: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;`. Is this a different cause? – Dale Apr 19 '19 at 00:53
  • @Mohan Rex Please help me how to fix this issue? – Garg Aug 19 '19 at 08:29
  • 1
    The only thing what helped me is to replace extends AppCompatActivity with just Activity in all my activities. E.g. public class MainActivity extends Activity. Nothing else worked for me. After this the warning is gone as it is related to AppCompat in android X. However, not the best solution for backward compatibility – Darksymphony Nov 20 '19 at 12:09
  • I have similar error and app not showing UI after updated Android Studio Canary. I solved this with: run app(or try other app first) in other phone is working, then back to previous phone(uninstalled app manually, not sure this is the real solution or not) is working again. – 林果皞 Mar 25 '20 at 07:32
  • Jump to see the answer works for me: https://stackoverflow.com/a/62991930/3260008 – Amos Dec 09 '20 at 13:16

6 Answers6

97

As A.L.Flanagan mentioned in a comment the problem is that android.support.v4.view.ViewCompat does not implement View.OnUnhandledKeyEventListener in the new androidx package structure and only implements it starting on API 28 in the support lib structure (at least in version 28.0.0). Therefore the warning appears on devices with API <28 and does not appear on those >=28.

This is the related code in the ViewCompat.class class from the support package structure:

@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);
    }
}

I can not think about any easy fix to solve this warning.

AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62
  • 20
    The issue has been marked as **won't fix** and nobody cared to give an explanation or a workaround. – Odys Dec 17 '18 at 16:24
  • 5
    It seems like the bug has been resubmitted here https://issuetracker.google.com/issues/120750246 – aaronmarino Dec 19 '18 at 11:30
  • 2
    Well, mine app is just not working at all and I just see this error on my debug (Logcat). The app don't crash, but the "contentView" is also not updated, leaving a blank screen. – Iúri dos Anjos Jan 22 '19 at 20:35
  • @AlvaroSantisteban How to use this class and where to call this? – Sulabh Gajjar Jan 31 '19 at 11:30
  • 1
    I am having same problem.. It's not crashing but view is having some unexpected behaviour. It's started to happen as soon as I upgraded the compile sdk to 28 from 27. – Amit Kumar Mar 01 '19 at 18:14
  • @AmitKumar, and what if upgrade to 29? In my case on API 27 emulator I get warning "Rejecting re-init on previously-failed class java.lang.Class: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;". – CoolMind Jul 12 '19 at 08:54
  • My app crashes with this error, only in mobile moto G6, api 28. I am not able to find any resources on this, how to fix the crash if @Odys is tellling it wont fix based on the issue tracker? – Aalap Patel Sep 13 '19 at 17:25
  • Do we have any fix for this problem? – Yerram Naveen Sep 24 '19 at 07:26
  • 4
    This is a runtime crash on every device with API <26 – John Sardinha Nov 02 '19 at 12:20
  • 1
    @AalapPatel. If your app is crashing, there is some other cause - this message itself does not cause a crash, AFAIK. This can be tested by running debug build of any working app on the same device or emulator - does the message still occur, but the app not crash? That is what most people are reporting; the message itself is harmless (assuming you've updated to latest version of the tools - an issue report comment suggests it might have been a crash prior to Feb 2019)... – ToolmakerSteve Mar 18 '20 at 22:31
  • 1
    Issue has been resolved in androidx.appcompat:appcompat:1.3.0-beta01 – Mubarak Feb 25 '21 at 06:19
17

I am facing same issue but just tried to upgrade dependency for appcompat and it worked no more error logs. I hope there will be new stable release soon. https://developer.android.com/jetpack/androidx/releases/appcompat#1.3.0-alpha02

implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'

Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
0

This is a bug in androidx. I've found the error causes lag in debug builds but doesn't show or slow down production builds. You can just ignore it for now.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Bob bobbington
  • 1,225
  • 13
  • 12
0

you can use aspectJ to void this issue by modify androidx byte code.

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();
        }
    }
}
包奇锋
  • 11
  • 2
  • Can you please explain how to get AspectJ working with Android Studio 3.6.3. I'm getting this error having built the "Basic Activity" template using compileSdkVersion 28, buildToolsVersion 29.0.3, minSdkVersion 16, targetSdkVersion 28. – kbro May 15 '20 at 16:56
-2

The answers above are correct. The only way to avoid this is to remove references to AppCompat - e.g. I changed to FragmentActivity. The bad news is the new material libraries all reference and experience the problem. I didn't want this occurring on my startup - the hit is not so bad later on. It's only a warning - but does have a performance impact and it's pretty bogus and poorly handled by the big G.

  • 2
    Re *"I changed to FragmentActivity"*. Not a good idea - that means your app won't run on older devices. Stay with AppCompat. Re *"It's only a warning - but does have a performance impact"* - to clarify, any performance impact is negligible on release build; this is mainly an annoyance, adding clutter near beginning of debug log. – ToolmakerSteve Mar 18 '20 at 22:23
-6

You can avoid it because that specific class only in Android 9.

  • 1
    Could you please give more information. It will be very helpful. – Mohan Rex Aug 19 '18 at 14:22
  • 3
    See https://developer.android.com/reference/android/view/View.OnUnhandledKeyEventListener. It looks like this is a bug where android.support.v4.view.ViewCompat doesn't implement an expected interface. – A. L. Flanagan Sep 18 '18 at 16:32
  • 4
    (Above class is ==> androidx.core.view.ViewCompat) There's a bug report https://issuetracker.google.com/issues/110162198 which is marked "unable to duplicate". You might want to consider filing a bug report with all the information needed to reproduce the issue. – A. L. Flanagan Sep 18 '18 at 16:38
  • Thx for the reference, I extended the issue in the tracker with my code in the hope they can reproduce it. – findusl Nov 09 '18 at 22:54