37

After using Android Studio to migrate my project to AndroidX (and manually fixing a lot of import errors), I'm getting no compile errors but when the app starts I get a crash with:

Error inflating class android.support.design.widget.AppBarLayout.

The offending line in the layout file is: <android.support.design.widget.AppBarLayout

My dependencies in build.gradle are:

dependencies {
    def lifecycle_version = '2.1.0-alpha02'
    // used below--will be different for androidx (migrated 2019-02-04)
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'

    implementation 'com.google.android.material:material:1.1.0-alpha03'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.squareup.okio:okio:1.15.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.5'
    implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.0'
    // Relay class
    implementation 'com.jakewharton.rx2:replaying-share:2.1.0'
    // ReplayingShare
    implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
    // RxBinding
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"  // see def above
    // includes ViewModel and LiveData
    implementation 'org.apache.commons:commons-lang3:3.8.1'
    // for tuples like Triple
    implementation 'com.androidplot:androidplot-core:1.5.6'
    // AndroidPlot
}

I'm guessing that I'm missing something but I can't find what it is.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Robert Lewis
  • 1,847
  • 2
  • 18
  • 43
  • 3
    Find `android.support.design.widget.AppBarLayout` and replace it with `com.google.android.material.appbar.AppBarLayout` at app level (including resource layout files.) And then clean build! – makata Feb 05 '19 at 03:57

5 Answers5

76

You need to use com.google.android.material.appbar.AppBarLayout.

Version 1.0.0 is already out So you can use implementation 'androidx.appcompat:appcompat:1.0.0'

Add dependency implementation 'com.google.android.material:material:1.0.0'

See Material Component integration for latest release version. And use

<com.google.android.material.appbar.AppBarLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

</com.google.android.material.appbar.AppBarLayout>

For other artifact and Class Mapping see the AndroidX migration Doc.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
ADM
  • 20,406
  • 11
  • 52
  • 83
  • Well, that error went away, now I get this one: `Error inflating class android.support.v7.widget.Toolbar` – Robert Lewis Feb 05 '19 at 04:01
  • 5
    Replace it with `androidx.appcompat.widget.Toolbar`. – ADM Feb 05 '19 at 04:09
  • Now I'm getting `Error inflating class androidx.constraintlayout.ConstraintLayout` even though I have this in dependencies: `implementation 'androidx.constraintlayout:constraintlayout:1.1.3'` – Robert Lewis Feb 05 '19 at 05:20
  • `ConstraintLayout` was never part of `v7` its a separate library . Use `implementation 'androidx.constraintlayout:constraintlayout:1.1.2'` .. Read the link above in Ben's answer. – ADM Feb 05 '19 at 05:31
  • As you can see above, that is exactly what I have, except for the minor version bump. – Robert Lewis Feb 05 '19 at 05:49
  • Its `androidx.constraintlayout.widget.ConstraintLayout` not `androidx.constraintlayout.ConstraintLayout` . Just use Auto complete intellisense to use the right package in xml . – ADM Feb 05 '19 at 05:52
35

Please go through this old to new class mappings

eg;- Use com.google.android.material.appbar.AppBarLayout instead of android.support.design.widget.AppBarLayout

For AppBarLayout

enter image description here

For Toolbar

enter image description here

Basi
  • 3,009
  • 23
  • 28
9

Androidx inflating class <android.support.design.widget.TabLayout/> will not work it's not exist so replace it with <com.google.android.material.tabs.TabLayout/> it will work fine and don't forgot to add

implementation 'com.google.android.material:material:1.1.0-alpha07'

to your dependencies

Basi
  • 3,009
  • 23
  • 28
eng mohamed emam
  • 549
  • 6
  • 13
7

According to the AndroidX migration docs, the androidx replacement for AppBarLayout is com.google.android.material.appbar.AppBarLayout. Try replacing your AppBarLayout tag with this instead.

As for why compiling/building works, I assume it's something to do with Jetifier, but I'm not certain.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
3

if you are using Kotlin DSL you have to add this to build.gradle.kts

 implementation("com.google.android.material:material:1.1.0")
 implementation("androidx.appcompat:appcompat:1.1.0")

and in your layout use <com.google.android.material.appbar.AppBarLayout/>

Foroogh Varmazyar
  • 1,057
  • 1
  • 14
  • 18