16

I'm trying to compile and run a demo app that was written for an old Android version.

I have updated all the files to use the new androidx libraries. That included the gradle.build the manifest and layout files. It compiles properly but crashes on the main activity on setContentView(R.layout.activity_main);

From looking at the stack trace I can deduce that its related to the layout.xml file as the app crash when it set for the first time. But I could not find any problem with the layout file after migrating the FloatingActionButton from the old to the new deisgn librarey.

Any ideas?

Stack:

I/zygote: 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;
        at void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2423)
        at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:750)
        at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:630)
        at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:529)
        at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:161)
        at void com.example.android.emojify.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:75)
        at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6975)
        at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1213)
        at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2770)
        at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2892)
I/zygote:     at void android.app.ActivityThread.-wrap11(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:1593)
I/zygote:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:105)
        at void android.os.Looper.loop() (Looper.java:164)
        at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6541)
        at java.lang.Object 

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.android.emojify"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.google.android.material:material:1.1.0-alpha07'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.gms:play-services-vision:17.0.2'
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    implementation 'com.jakewharton.timber:timber:4.7.1'
    testImplementation 'junit:junit:4.12'
}

and layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.emojify.MainActivity">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/view_margin"
        android:contentDescription="@string/imageview_description"
        android:scaleType="fitStart" />

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/emojify_button"
        android:layout_centerHorizontal="true"
        android:layout_margin="@dimen/view_margin"
        android:text="@string/emojify_me"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

    <Button
        android:id="@+id/emojify_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/go"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/clear_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_clear"
        android:visibility="gone"
        app:backgroundTint="@android:color/white"
        app:fabSize="mini" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="@dimen/fab_margins"
        android:layout_marginEnd="@dimen/fab_margins"
        android:layout_marginRight="@dimen/fab_margins"
        android:src="@drawable/ic_save"
        android:visibility="gone"
        app:backgroundTint="@android:color/white" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/share_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="@dimen/fab_margins"
        android:layout_marginLeft="@dimen/fab_margins"
        android:layout_marginStart="@dimen/fab_margins"
        android:src="@drawable/ic_share"
        android:visibility="gone"
        app:backgroundTint="@android:color/white" />

</RelativeLayout>
Itamar Kerbel
  • 2,508
  • 1
  • 22
  • 29
  • 1
    in your `gradle.properties` file: add `android.useAndroidX=true` `android.enableJetifier=true` – Basi Jun 04 '19 at 12:18
  • also for `espresso` https://developer.android.com/training/testing/espresso/setup – Basi Jun 04 '19 at 12:19
  • for more androidx https://stackoverflow.com/a/55849025/4649110 – Basi Jun 04 '19 at 12:20
  • 1
    @Basi - I forgot to mention that I already used these parameters in gradle.properties and read the article about migrating to AndroidX but didn't find anything that I did not do. – Itamar Kerbel Jun 04 '19 at 16:01

3 Answers3

2

I had the same problem, after update dependency into build.grade file to androidx.appcompat:appcompat:1.3.0-alpha02 problem was solved.

elvirt
  • 111
  • 1
  • 5
0

Your test implementation is support one yet, change for

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Lenin
  • 500
  • 3
  • 11
  • 1
    You are right that I missed this one but its not related to the error. It is still crashing. – Itamar Kerbel Jun 04 '19 at 16:00
  • So try to redo the layout. Some components change imports as ConstraintsLayouts. Let Android Studio autocomplete them and you will se the change. You can copy/paste margins and those common things but put again the layouts – Lenin Jun 04 '19 at 16:24
0

You are using material component

com.google.android.material.floatingactionbutton.FloatingActionButton

try to use material theme in you app theme

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

Hope this helps.

Nidheesh MT
  • 1,074
  • 11
  • 18
Islam Assem
  • 1,376
  • 13
  • 21
  • I have ` – Csaba Toth May 16 '20 at 23:53
  • can you comment with crash report – Islam Assem May 18 '20 at 09:28
  • basically the same call stack as the opening question – Csaba Toth May 21 '20 at 16:40
  • Do you have permission to upload it to GitHub, I can run it and debug it? – Islam Assem May 21 '20 at 17:24
  • This will be one of the apps: https://github.com/DIYGPSTracker/DIYGPSManager (+ the companion app the DIYGPSTracker). I wouldn't spend time on it since I'm almost certain that there are 3 possible solutions non of which I want to apply for different reasons 1. `@RequiresApi(28)` https://stackoverflow.com/a/52954286/292502 2. Fiddling with dex https://stackoverflow.com/a/53161064/292502 3. Or fiddling with the lib with AspectJ https://stackoverflow.com/a/60664338/292502 – Csaba Toth May 21 '20 at 17:33