Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 958
LackOfKnowledge Avatar asked Jan 16 '19 07:01

LackOfKnowledge


Video Answer


1 Answers

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.

like image 68
Steve Avatar answered Sep 26 '22 18:09

Steve