不能使ConstraintLayout膨胀

时间:2019-01-16 07:46:20

标签: android android-constraintlayout androidx

每次我的应用程序崩溃是因为它在类路径中找不到Landroidx / constraintlayout / widget / R $ styleable。我试图重建缓存并使之无效,但是在运行时它总是给我同样的错误。
我尝试了1.1.2和1.1.3版本。有趣的是:使用1.1.3时,androidX Jetifier仅强制使用1.1.2,如从gradle报告中看到的那样

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

我正在将 Android Studio 3.3 与新的android gradle插件一起使用。在我的gradle.properties中,启用了jetifier:

android.useAndroidX=true
android.enableJetifier=true

依赖关系如下解决:

> 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.

在xml中,其外观如下:

<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>

更新:

最后,由于找不到其他解决方案,我不得不使用ConstraintLayout 2.0 alpha版本

5 个答案:

答案 0 :(得分:8)

确保将实现'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'放入build.gradle(app)的依赖项中

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

在gradle.properties内添加:

android.enableJetifier=true
android.useAndroidX=true

它在Android Studio 3.3中对我有效。

编辑:我刚刚注意到您不想使用Alpha版本。无论如何,我希望我的答案的其他部分能有所帮助。

答案 1 :(得分:0)

您需要添加

android.enableJetifier=true
android.useAndroidX=true

在gradle.properties(项目属性)中

答案 2 :(得分:0)

似乎您将com.android.support.constraint作为依赖项,而不是androidx.constraintlayout

为我而改变

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

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

解决了该问题。

答案 3 :(得分:0)

在我的案例中,有什么帮助只是遵循IDE的建议-这些建议不仅用于更新constraint依赖性,而且还用于更新所有其他androidx依赖性。

作为旁注-在从Android Studio 3.3上的Android Studio生成器启动示例应用程序时,我偶然发现了这一点。

答案 4 :(得分:0)

除了使用

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

我还必须在Andriod Studio中重建该项目才能使其正常工作