51

After migrating to Androidx packages using Android Studio menu option Refactor -> Refactor to Androidx...

I'm getting the following error:

Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Zoe
  • 27,060
  • 21
  • 118
  • 148
Arturo Mejia
  • 1,862
  • 1
  • 17
  • 25

14 Answers14

112

Make sure constraint layout is updated to latest version:

implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

And replace your xml tag names

<androidx.constraintlayout.ConstraintLayout>

with

<androidx.constraintlayout.widget.ConstraintLayout>

in every place where it appears using this shortcut:

Windows: CTRL + SHIFT + R

Mac: COMMAND + SHIFT + R

hannes ach
  • 16,247
  • 7
  • 61
  • 84
  • 2
    The same error may also occur with android.constraintlayout.Guideline, so keep an eye out for that as well. – lbenedetto Oct 24 '18 at 09:41
  • It is required to use androidx.coordinatorlayout.widget.CoordinatorLayout for BottomAppBar. Any solution for it. – Akshay Nov 13 '18 at 09:43
  • I had the same problem with androidx.constraintlayout.motion.MotionLayout. Renaming it to androidx.constraintlayout.motion.widget.MotionLayout compiled and worked for me. –  Nov 13 '18 at 20:14
  • In my case: constraintlayout:1.1.2 -> constraintlayout:1.1.3. – Naveen Kumawat Feb 02 '19 at 12:06
35

In AndroidStudio 3.3 RC2 I got it resolved by upgrading -

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

to

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

in build.gradle (Module: app)

Note: This solution is also valid for the release version 3.3 (as per below comment by FireZenk), 3.5.2 (as per below comment by user2350644)

Devarshi
  • 16,440
  • 13
  • 72
  • 125
25

Just replace

<androidx.constraintlayout.ConstraintLayout>

with

<androidx.constraintlayout.widget.ConstraintLayout>

In your project's xml files.

You can use Replace in Path shortcut in mac (⇧⌘F or ⇧⌘R) and windows (Ctrl+Shift+R)

enter image description here

Ely Dantas
  • 705
  • 11
  • 23
Arturo Mejia
  • 1,862
  • 1
  • 17
  • 25
10

Well in my case I've tried Solution 1 which still was not working. Then I used Solution 2 along with Solution 1.


Solution 1

Updating the current version ConstraintLayout in app level build.gradle file.

From

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

Into

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

Solution 2

Invalidate Caches and Restart Android Studio (If still not working after applying Solution 1).

FileInvalidate Caches / RestartInvalidate and Restart

I used both of them and It works fine for me.

Note: Solution 1 is important.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
dipakbari4
  • 840
  • 1
  • 12
  • 16
4

I've also face this issue. This issue occur only for constraintlayout dependecy version. Use 1.1.3 version & hope your issue will be solved:

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Sheikh Hasib
  • 7,423
  • 2
  • 25
  • 37
3

There are two ways to add ConstraintLayout:

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

then you need to use it like this:

<android.support.constraint.ConstraintLayout
        ...>

</android.support.constraint.ConstraintLayout>

or using androidx:

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'

then you need to use

<androidx.constraintlayout.widget.ConstraintLayout
        ...
        >

</androidx.constraintlayout.widget.ConstraintLayout>

Do not mix those two.

Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55
2

Since the Android Studio is updated to 3.3 it will pop this error. I resolved it by upgrading the version for constraint layout in the build.gradle of the app level:

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

TO

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
1

I use an Android Studio 4.1 and this library for constraint layout:

implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

which is updated on 09-Nov-20 and had the same error with inflating a Constraint Layout.

In my case on my Samsung a40 app worked but on my ZTE nubia n1 app crashed instantly. I figured that a problem was in my layout background image because my png image was too big for an old device with a different screen resolution than the Samsung a40. I imported a background png image through the file explorer (drawable - right-click- Show in Explorer) and there was actually the problem.

I tried to import images with Android Drawable Importer but I notice this plugin does not work anymore on Android Studio.

SOLUTION RELATED TO ANDROID DRAWABLE IMPORTER:

If you have already installed the Android Drawable Importer on your Android Studio please uninstall it before you start the process of repairing. To repair the Android Drawable Importer you need ADI-hack-plugin which you can get by following this youtube video from Thomas Samoul.

After you repair the Android Drawable Importer just import your image with it and everything will work fine.

Dezo
  • 835
  • 9
  • 16
  • The same for SVG image. I changed `android:background="@drawable/bg_image"` of `ConstraintLayout` to SVG image and got this exception. – CoolMind Dec 19 '20 at 21:19
  • Yeah, the biggest problem is that on some devices everything works fine and on some an app crashes. I did not try on emulator but i suppose there would not be any crashes. – Dezo Dec 19 '20 at 22:13
  • 1
    Agree with you. But I got this exception on emulator with SVG. – CoolMind Dec 20 '20 at 17:00
1

I ran into the same problem none of the solutions above worked. The problem, in fact , was caused by a null pointer exception thrown after I switch to NoActionBar theme while the app was using the ActionBar to setTitle(). The linked stack overflow answer helped me figure it out

0

Android Studio is updated version 3.3. Please remove the previous version constraintLayout

my case used this

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'

Thank you

Sonu Kumar
  • 969
  • 1
  • 11
  • 36
0

In my case i just had to update the androidx dependencies to the latest version.

EDIT In build.gradle (app) you have to look for the dependencies which contains androidx. Android studio will show you information about the latest versions of these dependencies. Using that information just the change the dependency version.

Anand Sharma
  • 53
  • 10
0

In AndroidStudio 3.4 I had to downgrade to

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

to get it working again.

Kurt Huwig
  • 983
  • 11
  • 11
0

Strangely, it worked for when I upgraded androidx.appcompat:...' library to

'androidx.appcompat:appcompat:1.1.0-alpha05' with constraint layout version 'androidx.constraintlayout:constraintlayout:1.1.3' on Android Studio 3.3

Jose
  • 2,479
  • 1
  • 20
  • 17
0

For those who have tried all of the above to no vain, downgrading constraintLayout worked for me (after I wrecked my head for 3 days trying everything under the sun).

implementation 'androidx.constraintlayout:constraintlayout:2.1.0'

to

implementation 'androidx.constraintlayout:constraintlayout:2.0.0'