Error inflating class android.support.constraint.ConstraintLayout

I was working on an Android Studio application when I ran into this error.

    Process: com.example.visualizercopy, PID: 28098
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.visualizercopy/io.esense.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6317)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
     Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.ConstraintLayout" on path: DexPathList[[zip file "/data/app/com.example.visualizercopy-1/base.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.visualizercopy-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.visualizercopy-1/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:609)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:424)
        at android.app.Activity.setContentView(Activity.java:2471)
        at io.esense.MainActivity.onCreate(MainActivity.java:41)
        at android.app.Activity.performCreate(Activity.java:6757)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2704)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6317)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
2019-06-23 16:25:42.234 28098-28098/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

This is my gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.example.visualizercopy"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
    implementation 'com.jjoe64:graphview:4.2.2'
    implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

This is the constraint layout tag in my xml file

<android.support.constraint.ConstraintLayout
    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/PLAY_PARENT"
    android:padding="0dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

I have looked into my error on this site and google, and the common answer is to add compile 'com.android.support.constraint:constraint-layout:1.0.2', however compile is now obsolete, and when I replace compile with implementation and changed it to the newest version ('com.android.support.constraint:constraint-layout:2.0.0-beta1') I still run into the same error. How can I fix this?


I figured it out, I had to update the android.support.constraint.ConstraintLayout tags to androidx.constraintlayout.widget.ConstraintLayout in my layout XML.


You are using androidX packages. So you have to migrate all android.support library with androidX.

So replace below line in gradle,

implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'

With this,

implementation 'androidx.constraintlayout:constraintlayout:1.1.2' 

Reference here.


If you are using androidX packages or you want to use them for your project, then you have to migrate your project to androidX. For that you need to follow below steps:

1) In Android Studio, just goto Refactor-> Migrate to AndroidX to migrate your project and there you can backup your project if some problem occurs migrating project.

After that your dependendencies will have androidX packages automatically.

Further reading: Here