androidx 数据绑定(bind)错误 : package android. support.constraint 不存在

标签 android gradle android-jetpack androidx

我刚刚学习数据绑定(bind),并尝试使用新的库结构作为androidx。 当我尝试构建项目时,我遇到了这个错误。

error: package android.support.constraint does not exist

这个错误在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.support.constraint 表示您正在使用支持库而不是 AndroidX。

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

关于androidx 数据绑定(bind)错误 : package android. support.constraint 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53592636/

相关文章:

android - 如何在 Android Jetpack Compose 的 TabRow 中更改按下的 Tab 颜色?

android - Kotlin Android Jetpack 在后台 fragment 之间导航

android - 当我在 gradle 中导入 Android-ktx 时,kotlin 扩展的合成不起作用

java - Android:JsonWriter 无法写入文件

android - 像抽屉导航一样实现 Gmail 平板电脑

android - 如果另一个任务依赖于第一个任务运行,Gradle任务不会删除文件

android - 更新 google play 服务(8.1 至 8.3)后应用程序无法安装

java - DataOutputStream:循环中的dos.write(),Receiver只接收到一个数据包

android - 未在Android Studio中加载Codenameone插件

android - Android Studio-构建Android示例项目(无构建文件)?