6

I've searched for an answer to my problem, as shown in the title of this question, and found Error inflating class androidx.constraintlayout.ConstraintLayout after migration to androidx, which aligns with the official migration instructions presented in Migrating to AndroidX. I took the information as requiring me to change android.support.constraint.ConstraintLayoutto androidx.constraintlayout.widget.ConstraintLayout, but this doesn't work for me.

I haven't found anything about what dependency to put in my gradle file, so I tried this (this is a shot in the dark):

    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

which is accepted but when I tried:

    implementation 'androidx.constraintlayout.widget:constraintlayout:1.1.3'

I get ERROR: Failed to resolve: androidx.constraintlayout.widget:constraintlayout:1.1.3, so I used the previous implementation directive.

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jbiss.petminder.activities.MainActivity">

<!--<android.support.v7.widget.Toolbar-->
<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/colorPrimaryDark"
    android:elevation="4dp"
    app:layout_constraintTop_toTopOf="parent"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

<TextView
    android:id="@+id/tvMsg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:accessibilityLiveRegion="assertive"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
    tools:text="error message goes here"/>

<TextView
    android:id="@+id/tvEmVet"
    android:layout_width="wrap_content"
    android:layout_height="44dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="8dp"
    android:text="@string/emergency_vet"
    android:textColor="@android:color/holo_red_dark"
    android:textSize="24sp"
    app:layout_constraintStart_toEndOf="@+id/phoneCallEmVet"
    app:layout_constraintTop_toBottomOf="@+id/recyclerview1"
    tools:textSize="24sp"/>

<ImageView
    android:id="@+id/phoneCallEmVet"
    android:layout_width="37dp"
    android:layout_height="44dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="8dp"
    android:onClick="callEmVet"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recyclerview1"
    app:srcCompat="@android:drawable/sym_action_call"/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview1"
    android:layout_width="368dp"
    android:layout_height="365dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/pet_name"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/my_toolbar"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Running my app produces the following error:

2019-02-18 19:43:36.031 458-458/com.example.jbiss.petminder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jbiss.petminder, PID: 458
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.jbiss.petminder-i-ubC_oLu_y7-aFpiqALqg==/base.apk", zip file "/data/app/com.example.jbiss

Following the information doesn't work, for some reason. The migration has been tough but I've cleaned up a lot of issues and am now unable to find a reason why this is happening when it appears that I have done what is intended.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jeff
  • 431
  • 4
  • 16
  • Are you confused between `build artifact name` and `class name` when migrating to AndroidX? – ecle Feb 19 '19 at 06:31
  • No, because I have no idea what either is. When I used Migrating to AndroidX, I found the Class Mappings table because it listed the classes that were being complained about in error messages, I now see that there is a table for Artifact Mappings. I do NOT understand what an artifact is or what to do with them as there is no information about it on that page. I will search for information as I assume that your question is a hint. – Jeff Feb 19 '19 at 14:17
  • When I launched AS this morning, I am greeted with "Calculate task graph" errors, the blue ones are about ConstraintLayout, so I assume that there is some "artifact" issue. I have found and am reading a [The Reality of Migrating to AndroidX](https://blog.danlew.net/2018/11/14/the-reality-of-migrating-to-androidx/), so maybe I'll find something helpful. – Jeff Feb 19 '19 at 14:24
  • From the little reading I've been able to do, it looks like "build artifacts" have to do with what is presented in the gradle file? For example, the Old is: android.arch.lifecycle:extensions and the "Androidx" is: androidx.lifecycle:lifecycle-extensions:2.0.0-rc01, which I think means that I must make that change in my gradle file? This seems to be implied in **Migration Preparation** in [The Reality of Migrating to AndroidX](https://blog.danlew.net/2018/11/14/the-reality-of-migrating-to-androidx/). Am I on the right track? – Jeff Feb 19 '19 at 15:24

3 Answers3

11

I solved my problem. I found that Androidx migration is a bit more involved than I had at first thought. Knowing that I had updated the labels of the ConstraintLayout elements in the XML files used in the initial screen, I used Windows GREP to search for "android.support.constraint.ConstraintLayout" throughout my project. Lo and behold I found instances that I had missed and so I updated the XML file reference to "androidx.constraintlayout.widget.ConstraintLayout".

This allowed my project to be successfully built and launch on the emulator. However, after trying to access another screen I got the same error as shown in my initial post, but for the AppBarLayout. So, I must proceed for this one as I did for ConstraintLayout and then any others.

If you are migrating, be prepared for a little more work than you had anticipated!

Jeff
  • 431
  • 4
  • 16
3

I was facing the same problem after migrating to androidX. Tried above solution. But, it didn't work. I added mavenCentral() in project level app gradle.

 repositories {
            jcenter()
            maven { url 'https://maven.google.com' }
            mavenCentral()
     }

with following changes

dependencies  {
    implementation 'com.google.android.material:material:1.0.0'
    implementation "androidx.constraintlayout:constraintlayout:1.1.3"
}

updated "android.support.constraint.ConstraintLayout" xml tag with "androidx.constraintlayout.widget.ConstraintLayout"

finally it worked.

  • 1
    If you want people to upvote your solution, I recommend explaining at least how to add this repository. – Alex Jan 13 '20 at 10:25
  • Thanks, it worked for me. Android Studio suggested to replace the jcenter() line to mavenCentral() so I think that line is not needed. It is working without it for me. – Hyung Tae Carapeto Figur Apr 08 '22 at 17:43
3

If you experience this issue on Android Studio 3.6.3 after using the Migrate to Androidx tool like I did, Here is what worked for me:

I was using androidx.constraintlayout.ConstraintLayout in my layout XML file instead of androidx.constraintlayout.widget.ConstraintLayout

The difference is one has widget before ConstraintLayout. The dependency in my app-level build.gradle was:

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

The latest at that time.

Eaweb
  • 811
  • 1
  • 12
  • 16