Kotlin 1.6 和 Android Studio 更新后,数据绑定显示错误但应用程序编译并运行良好
After Kotlin 1.6 and Android Studio update, databinding shows errors but app compiles and runs fine
我的 android 工作室版本是
Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021
将 IDE 和 kotlin 更新到 1.6 后,我打开了一个正在处理的项目,Android Stidio 将数据绑定字段标记为红色,显示以下错误:
正文:
Cannot access 'androidx.databinding.Observable' which is a supertype of
'com.cioccarellia.wordbucket.databinding.ActivityMainBinding'.
Check your module classpath for missing or conflicting dependencies
奇怪的是,一切都编译好了,应用程序运行正常,这些错误就在 IDE 中,每次我通过数据绑定访问视图时它们都会弹出。
自更新 android studio 以来,我没有更改我的依赖项。对于这种情况,还有其他人有同样的问题/解决方案吗?
主要Activityxml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.cioccarellia.wordbucket.activity.MainActivity">
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
一般build.gradle:
buildscript {
ext {
kotlin_version = "1.6.0"
}
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.10'
classpath "com.google.dagger:hilt-android-gradle-plugin:2.40.3"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
classpath "com.github.ben-manes:gradle-versions-plugin:0.39.0"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
申请build.gradle:
plugins {
id "com.android.application"
id "kotlin-android"
id "kotlin-kapt"
id "dagger.hilt.android.plugin"
id "com.google.gms.google-services"
id "androidx.navigation.safeargs.kotlin"
id "com.github.ben-manes.versions"
id "kotlin-parcelize"
id "com.google.firebase.crashlytics"
}
android {
compileSdk 31
defaultConfig {
applicationId "com.cioccarellia.wordbucket"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}
buildTypes {
debug {
versionNameSuffix ".dev"
}
release {
debuggable false
// Enables code shrinking, obfuscation, and optimization
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
dataBinding true
}
sourceSets {
main.res.srcDirs = [
"src/main/res",
"src/main/res-public"
]
android.sourceSets.all {
java.srcDir("src/$name/kotlin")
}
}
lintOptions {
// if set to true (default), stops the build if errors are found.
abortOnError false
// if true, only report errors.
ignoreWarnings false
}
}
dependencies {
// AndroidX
implementation "androidx.core:core-ktx:1.7.0"
implementation "androidx.appcompat:appcompat:1.4.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.2"
implementation "com.google.android.material:material:1.6.0-alpha01"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
// Lifecycle
def lifecycle_version = "2.4.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
// DI -> Hilt
implementation "com.google.dagger:hilt-android:2.40.3"
kapt "com.google.dagger:hilt-compiler:2.40.3"
// Import the Firebase BoM
implementation platform("com.google.firebase:firebase-bom:28.4.2")
// When using the BoM, you don"t specify versions in Firebase library dependencies
implementation "com.google.firebase:firebase-crashlytics-ktx"
implementation "com.google.firebase:firebase-analytics-ktx"
implementation "com.google.firebase:firebase-auth-ktx"
implementation "com.google.firebase:firebase-firestore-ktx"
// LeakCanary
debugImplementation "com.squareup.leakcanary:leakcanary-android:2.7"
// Logging
implementation "com.jakewharton.timber:timber:5.0.1"
// Styles
implementation 'com.airbnb.android:paris:2.0.1'
kapt 'com.airbnb.android:paris-processor:2.0.1'
// JUnit Default Dependencies
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
}
尝试重新启动 IDE。如果这不起作用,请在 window 顶部的“构建”>“清理项目”中清理项目,然后通过“构建”>“重建项目”完全重建它。
编辑:
另一个原因是 Android Studio 将不再支持绑定到直接元素,因此您必须自己指定绑定。
为此,请在应用程序 gradle 文件中添加 BuildFeatures:
android {
compileSdk 31
//This right here
buildFeatures{
viewBinding true
}
defaultConfig {
applicationId "com.cioccarellia.wordbucket"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}
然后通过将此添加到您的 activity 文件来创建新绑定:
//Declare the binding
private lateinit val binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
//Initialize the binding
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
//Change the content view to the root of the binding
setContentView(binding.root)
}
然后在使用您的视图时引用绑定:
binding.toolbar.title = bucket.name
更新 android studio 解决了问题
我的 android 工作室版本是
Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021
将 IDE 和 kotlin 更新到 1.6 后,我打开了一个正在处理的项目,Android Stidio 将数据绑定字段标记为红色,显示以下错误:
正文:
Cannot access 'androidx.databinding.Observable' which is a supertype of
'com.cioccarellia.wordbucket.databinding.ActivityMainBinding'.
Check your module classpath for missing or conflicting dependencies
奇怪的是,一切都编译好了,应用程序运行正常,这些错误就在 IDE 中,每次我通过数据绑定访问视图时它们都会弹出。
自更新 android studio 以来,我没有更改我的依赖项。对于这种情况,还有其他人有同样的问题/解决方案吗?
主要Activityxml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.cioccarellia.wordbucket.activity.MainActivity">
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
一般build.gradle:
buildscript {
ext {
kotlin_version = "1.6.0"
}
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.10'
classpath "com.google.dagger:hilt-android-gradle-plugin:2.40.3"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
classpath "com.github.ben-manes:gradle-versions-plugin:0.39.0"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
申请build.gradle:
plugins {
id "com.android.application"
id "kotlin-android"
id "kotlin-kapt"
id "dagger.hilt.android.plugin"
id "com.google.gms.google-services"
id "androidx.navigation.safeargs.kotlin"
id "com.github.ben-manes.versions"
id "kotlin-parcelize"
id "com.google.firebase.crashlytics"
}
android {
compileSdk 31
defaultConfig {
applicationId "com.cioccarellia.wordbucket"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}
buildTypes {
debug {
versionNameSuffix ".dev"
}
release {
debuggable false
// Enables code shrinking, obfuscation, and optimization
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
dataBinding true
}
sourceSets {
main.res.srcDirs = [
"src/main/res",
"src/main/res-public"
]
android.sourceSets.all {
java.srcDir("src/$name/kotlin")
}
}
lintOptions {
// if set to true (default), stops the build if errors are found.
abortOnError false
// if true, only report errors.
ignoreWarnings false
}
}
dependencies {
// AndroidX
implementation "androidx.core:core-ktx:1.7.0"
implementation "androidx.appcompat:appcompat:1.4.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.2"
implementation "com.google.android.material:material:1.6.0-alpha01"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
// Lifecycle
def lifecycle_version = "2.4.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
// DI -> Hilt
implementation "com.google.dagger:hilt-android:2.40.3"
kapt "com.google.dagger:hilt-compiler:2.40.3"
// Import the Firebase BoM
implementation platform("com.google.firebase:firebase-bom:28.4.2")
// When using the BoM, you don"t specify versions in Firebase library dependencies
implementation "com.google.firebase:firebase-crashlytics-ktx"
implementation "com.google.firebase:firebase-analytics-ktx"
implementation "com.google.firebase:firebase-auth-ktx"
implementation "com.google.firebase:firebase-firestore-ktx"
// LeakCanary
debugImplementation "com.squareup.leakcanary:leakcanary-android:2.7"
// Logging
implementation "com.jakewharton.timber:timber:5.0.1"
// Styles
implementation 'com.airbnb.android:paris:2.0.1'
kapt 'com.airbnb.android:paris-processor:2.0.1'
// JUnit Default Dependencies
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
}
尝试重新启动 IDE。如果这不起作用,请在 window 顶部的“构建”>“清理项目”中清理项目,然后通过“构建”>“重建项目”完全重建它。
编辑: 另一个原因是 Android Studio 将不再支持绑定到直接元素,因此您必须自己指定绑定。
为此,请在应用程序 gradle 文件中添加 BuildFeatures:
android {
compileSdk 31
//This right here
buildFeatures{
viewBinding true
}
defaultConfig {
applicationId "com.cioccarellia.wordbucket"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}
然后通过将此添加到您的 activity 文件来创建新绑定:
//Declare the binding
private lateinit val binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
//Initialize the binding
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
//Change the content view to the root of the binding
setContentView(binding.root)
}
然后在使用您的视图时引用绑定:
binding.toolbar.title = bucket.name
更新 android studio 解决了问题