【问题标题】:androidx with databinding error: package android.support.constraint does not exist带有数据绑定错误的 androidx:包 android.support.constraint 不存在
【发布时间】:2019-05-04 16:20:51
【问题描述】:

我刚刚了解databinding 并尝试使用新的库结构作为androidx。 当我尝试构建项目时,我遇到了这个错误。

错误:包 android.support.constraint 不存在

这个错误在Binding生成的文件中为

public class ActivityMainBindingImpl extends ActivityMainBinding  {
    @Nullable
    private static final androidx.databinding.ViewDataBinding.IncludedLayouts sIncludes;
    @Nullable
    private static final android.util.SparseIntArray sViewsWithIds;
    static {
        sIncludes = null;
        sViewsWithIds = null;
    }
    // views
    @NonNull
    private final android.support.constraint.ConstraintLayout mboundView0;
    @NonNull
    private final android.widget.TextView mboundView1;

这是我的build.gradle 应用模块。

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.climesoft.learn.livedatabinding"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

这里是build.gradle 项目模块。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.10'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

最后的gradle.properties文件是这个。

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

android.useAndroidX=true

android.enableJetifier=true

那么为什么android studio 不使用androidx.constraintlayout 包生成Binding 类呢?

【问题讨论】:

    标签: android gradle android-jetpack androidx


    【解决方案1】:

    如果你使用

    com.heinrichreimersoftware:material-intro:x.x.x
    

    尝试改变:

    implementation 'com.heinrichreimersoftware:material-intro:master-SNAPSHOT'
    

    【讨论】:

      【解决方案2】:

      使用 androidx,包名称已更改。原来的 android.support 现在是 androidx。通常,当您将项目转换为 androidx/jetpack 时,AndroidStudio 会自动更改。在某处的 Build... 菜单中有一个选项。

      【讨论】:

        【解决方案3】:

        android.support.constraint 表示您使用的是支持库而不是 AndroidX。

        您可能应该将activity_main.xml 中的android.support.constraint.ConstraintLayout 更改为androidx.constraintlayout.widget.ConstraintLayout

        【讨论】:

        • 同一个库出现同样的错误,但现在适用于我的情况
        • 这解决了我在 Android Studio 更新 3.2.x -> 3.6.1 和随后的 Gradle 更新到 5.6.4 之后的问题。我还必须用来自androidx 的相应元素替换所有其他android.support.constraint.xxx 元素。
        猜你喜欢
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 2018-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-03
        • 2018-11-24
        相关资源
        最近更新 更多