RChugunov
RChugunov

Reputation: 806

Can't inflate ConstraintLayout

Each time my app crashes because it can't find Landroidx/constraintlayout/widget/R$styleable in classpath. I tried to rebuild, invalidate cache but it always gives me the same error in runtime.
I tried both versions 1.1.2 and 1.1.3. And interesting: when using 1.1.3 androidX Jetifier just forces using 1.1.2 as you can see from gradle report

Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
    at android.view.LayoutInflater.createView(LayoutInflater.java:651)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:141)
    at com.github.sharetaxi.LoginActivity.onCreate(LoginActivity.kt:24)
    at android.app.Activity.performCreate(Activity.java:7050)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6701)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:246)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/constraintlayout/widget/R$styleable;
    at androidx.constraintlayout.widget.ConstraintLayout.init(ConstraintLayout.java:590)
    at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:567)
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:651) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:496) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:141) 
    at com.github.sharetaxi.LoginActivity.onCreate(LoginActivity.kt:24) 
    at android.app.Activity.performCreate(Activity.java:7050) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:6701) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:246) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.constraintlayout.widget.R$styleable" on path: DexPathList

I'm using Android Studio 3.3 with fresh android gradle plugin. And In my gradle.properties I have enabled jetifier:

android.useAndroidX=true
android.enableJetifier=true

The dependency resolved like following:

> Task :app:dependencyInsight
androidx.constraintlayout:constraintlayout:1.1.2
   variant "default" [
      org.gradle.status                                            = release (not requested)
      Requested attributes not found in the selected variant:
         com.android.build.api.attributes.BuildTypeAttr               = debug
         org.gradle.usage                                             = java-api
         com.android.build.gradle.internal.dependency.AndroidTypeAttr = Aar
         org.jetbrains.kotlin.platform.type                           = androidJvm
   ]
   Selection reasons:
      - Was requested
      - Selected by rule : ENABLE_JETIFIER is enabled
      - By constraint : debugRuntimeClasspath uses version 1.1.2

com.android.support.constraint:constraint-layout:1.1.3 -> androidx.constraintlayout:constraintlayout:1.1.2
+--- debugCompileClasspath
\--- project :general
     \--- debugCompileClasspath

(*) - dependencies omitted (listed previously)

A web-based, searchable dependency report is available by adding the --scan option.

In xml it looks like following:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bg"
    android:orientation="vertical">
...
</androidx.constraintlayout.widget.ConstraintLayout>

UPDATE:

Finally I had to use ConstraintLayout 2.0 alpha version since no other solution found

Upvotes: 12

Views: 13128

Answers (5)

Abdelalim Hassouna
Abdelalim Hassouna

Reputation: 768

TL;DR

The issue can be totally different from what the error message shows


Although my case could be particularly unique I'll share it for reference.

I have created a library to be used in another project through Titanium Hyperloop, link, where you have to list all libraries used (aar or jar).

The issue was not related at all to the error message shown. It took me ages to figure out that a library (gson-2.8.6jar) was missing.

Upvotes: 0

h0lgi
h0lgi

Reputation: 31

apart from using

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

I also had to rebuild the project in Andriod Studio to make it work

Upvotes: 0

Antek
Antek

Reputation: 739

what helped in my case was just following IDE's recommendations - these were not only to update constraint dependency, but also all the other androidx ones.

As a side note - I stumbled this upon just when starting a sample app from Android Studio generator on Android Studio 3.3.

Upvotes: 0

goldenreign
goldenreign

Reputation: 11

It seems you have com.android.support.constraint as the dependency and not androidx.constraintlayout.

For me changing

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

to

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

fixed the issue.

Upvotes: 1

Steve
Steve

Reputation: 252

Make sure you put implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' inside dependencies in build.gradle (app):

dependencies {
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
}

And inside gradle.properties add:

android.enableJetifier=true
android.useAndroidX=true

It works for me in Android Studio 3.3.

EDIT: I've just noticed you don't want to use alpha version. Anyways, I hope the other parts of my answer can help.

Upvotes: 16

Related Questions