118

I have upgraded my Android Studio to

Android Studio 3.2
Build #AI-181.5540.7.32.5014246, built on September 17, 2018
JRE: 1.8.0_152-release-1136-b06 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.11.6

I create a new Project using Empty Activity template

Sync Gradle
Clean
Build
Run

my application logcat shows this exception on startup

2018-09-27 13:51:41.116 22090-22090/? I/zygote64: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:647)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:790)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:730)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:863)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:824)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void com.manacle.secondmanacle.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:11)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:7009)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7000)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1214)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2731)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2856)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1589)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.os.Looper.loop() (Looper.java:164)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6494)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:438)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:807)
2018-09-27 13:51:41.116 22090-22090/? I/zygote64: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.manacle.secondmanacle-6Ra5peoMwc4HH30iTlEXCg==/base.apk"],nativeLibraryDirectories=[/data/app/com.manacle.secondmanacle-6Ra5peoMwc4HH30iTlEXCg==/lib/arm64, /system/lib64, /vendor/lib64]]

I have made no changes to the template app generated by Android Studio.

Why doesnt the generate template app start cleanly?

I tried migrating to androidX, however the issue remains exactly the same.

Why cant Android Studio generate a "clean" template application?

My gradle files resemble this:-

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

====

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.manacle.secondmanacle"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
Hector
  • 4,016
  • 21
  • 112
  • 211

10 Answers10

66

Sometimes after an upgrade, you need to invalidate and clear cache.

enter image description here

There are some known issues in 3.2 so also ensure you are not on Kotlin tools org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.70

as that causes freeze issues as well. If that doesn't work, remove your google plugin lines and support libraries, sync and add them again and sync. Sometimes the cache directories just get out of whack.

Sam
  • 5,342
  • 1
  • 23
  • 39
  • 9
    Nothing help. I try with Invalidate Caches / Restart. With and without Kotlin. I have Android Studio 3.2.1. – Bobert Oct 18 '18 at 14:30
  • Hi @Bobert, you aren't the one that asked the question, so I have no idea what your situation is. Please post a question with your scenario, versions, toolsets, and error and I'm happy to try and help if I see the problem. – Sam Oct 18 '18 at 19:32
  • 1
    Hi @Sam it's exactly the same problem. I create a new projekt and start this new project and become the same LogCat infos. But i use Android Studio 3.2.1 Build #AI-181.5540.7.32.5056338, built on October 9, 2018 JRE: 1.8.0_152-release-1136-b06 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 7 6.1 – Bobert Oct 19 '18 at 09:22
  • Hi @Bobert without seeing your exact log errors and gradle file snippets, it is just a guessing game. I'm good lol, but not that good. If you post a question with all your snippets and error logs, feel free to send me the link to review. As your error logs won't be identical to his above, even if you have a similar error. There may be one small line in there that tells us exactly what's going on or points us in the right direction – Sam Oct 19 '18 at 19:09
  • 2
    I have found the Issue from me https://issuetracker.google.com/117685087 it make the same error – Bobert Oct 20 '18 at 11:00
  • Nothing help. I try with Invalidate Caches / Restart. – Hamza Abdullah May 31 '19 at 14:40
  • It may be related to targetSDK version 27 or 28 or androidX. – Kirill Karmazin Jul 15 '19 at 17:51
  • hi @rbaleksandar the file- invalidate Cache and Restart is still there in the latest. I simply suggested sometimes you need to do this. So I'm not sure what you mean by "not working with latest versions of SDK and Studio" as I'm using the latest, and invalidating cache and restarting still does exactly what it says it does. – Sam Nov 05 '19 at 01:13
  • 1
    Yes, it is there but when I deploy the app I am still getting the previously build one and none of my changes are there. – rbaleksandar Nov 05 '19 at 10:58
  • @rbaleksandar sounds like you have a bigger problem then just a simple cache clearing then. – Sam Nov 05 '19 at 18:15
  • invalidate and clear cache is most popular solution for may problems in Android Studio – void pointer Feb 19 '20 at 06:25
34

I solved a similar issue

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;
 
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener"

Adding dependency in build.gradle (app level)

implementation 'androidx.core:core:1.5.0-alpha04'
Shomu
  • 2,734
  • 24
  • 32
  • Confirming to fix the issue for me. Using latest AS (2021.03 Arctic Fox Patch 1). Also, updated Google Play dependency to latest version (com.google.gms:google-services:4.3.10) – HX_unbanned Aug 21 '21 at 11:39
14

if you are using android x try this (add this code segment to app level Gradle file)

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

if you are not using android x try this code segment.

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == "com.android.support") {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion "26.+"
        }
    }
}}
Supun Ishara
  • 323
  • 2
  • 8
  • Thanks, but didn't work for me :-( - Android Studio 3.5.3, Build #AI-191.8026.42.35.6010548, built on November 15, 2019, JRE: 1.8.0_202-release-1483-b49-5587405 amd64, JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o, Linux 4.15.0-74-generic – legolas108 Jan 30 '20 at 16:56
  • 1
    @legolas108, before you build the application, Invalidate Caches/Restart IDE then clean project and rebuild the application APK. It worked for me. – Supun Ishara Jan 31 '20 at 10:04
  • 1
    that helped, a new Empty Activity project without modifications now starts without this error after following your additional instructions. Thanks much! – legolas108 Feb 01 '20 at 13:43
  • I'm on Kotlin and my `targetSdk: 29` and it doesnt work for me. – Anbuselvan Rocky Apr 27 '20 at 03:11
  • @Anbuselvan Rocky I think this article will be helpful for you https://discuss.kotlinlang.org/t/kotlin-dependencies-in-gradle-projects/10490/4 – Supun Ishara Apr 27 '20 at 14:09
11

Add this to gradle will resolve the issue, works for me:

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

Please note 1.2.0 even doesn't work

Amos
  • 2,222
  • 1
  • 26
  • 42
  • Did not worked for me. I have submodule in my project. It gives ANR not crashing. Can you put other gradle dependencies you used in the answer. – Nuwan Chamara Mar 12 '21 at 08:53
10

Got similar issue with androidx dependencies as like

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;

Resolved by adding configurations in Gradle files as below

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == "androidx.appcompat") {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "1.+"
            }
        }
    }
}
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • Worked on AS 4.1. For fellow noobs, I have added this to _build.gradle_ of the module (the one inside the app folder). – PeterG Nov 01 '20 at 12:37
  • 1
    I got better solution rather than adding this code snippet, please refer to https://stackoverflow.com/a/65217631/3260008 – Amos Dec 19 '20 at 03:19
4

I solved a similar issue by pasting this at the bottom of my build.gradle file

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == "com.android.support") {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "26.+"
            }
        }
    }
}

https://github.com/facebook/flipper/issues/146

Rag King
  • 65
  • 1
3

This error can be a decoy. In my case, the actual error turned out to be a Resources$NotFoundException somewhere in the stacktrace. When I replaced an <androidx.appcompat.widget.AppCompatImageView with ImageView I got a readable error which then was easy to solve.

Cristan
  • 12,083
  • 7
  • 65
  • 69
3

My problem was missing the androidx import in the build.gradle for one of the view elements in the layout that was being inflated. Adding implementation 'androidx.drawerlayout:drawerlayout:1.0.0 ' fixed my specific problem. I imagine there is some androidx view that is inside your Main Activity's layout that is not imported. All of the different packages and versions are at https://maven.google.com/web/index.html, simply find the item you are missing and add it to the dependencies in your module's build.gradle.

rjferguson
  • 603
  • 3
  • 12
1

I struggled with same exception but only when testing my application on a real device (Samsung Galaxy S9). Same application was running fine on virtual devices. Since build.gradle config change did not solved, I worked around the problem by moving my main activity from AppCompatActivity to Activity. I sounds like downgrading your activity, but if you are not using advanced activity features - See this (https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity) - it's not a real problem.

//import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;

public class MainActivity /*extends AppCompatActivity*/ extends Activity
Pascal Chardon
  • 186
  • 1
  • 4
1

Worked for me , Thanks for Amos in the top

add this in your dependencies in Gradl

    implementation 'androidx.core:core:1.5.0-alpha04'
Omar Essam El-Din
  • 1,415
  • 14
  • 17