【问题标题】:After Kotlin 1.6 and Android Studio update, databinding shows errors but app compiles and runs fineKotlin 1.6 和 Android Studio 更新后,数据绑定显示错误,但应用程序编译并运行良好
【发布时间】:2022-01-10 17:42:16
【问题描述】:

我的android studio版本是

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 以来,我没有更改我的依赖项。其他人对这种情况有同样的问题/解决方案吗?

主要活动 xml:

<?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"
}

【问题讨论】:

  • 可能重复 - 只需查看此link。您也可以尝试使缓存无效并重新启动 IDE,然后重建和同步项目。
  • 我试过了,但不能解决问题

标签: android android-studio kotlin data-binding


【解决方案1】:

尝试重新启动 IDE。如果这不起作用,清理项目将转到窗口顶部的 Build > Clean Project,然后通过转到 Build > Rebuild Project 完全重建它。

编辑: 另一个原因是 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
}

然后通过将其添加到您的活动文件来创建一个新的绑定:

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

【讨论】:

  • 将 buildFeatures 放在 defaultConfig 之外不起作用,这在 kotlin 1.6 之前有效。我尝试使缓存无效,但仍然无法正常工作
  • 您是否收到来自 Android Studio 的消息说您的项目可以升级?此外,请确保您的依赖项是最新的。
【解决方案2】:

更新 android studio 解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 2017-06-30
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多