如何在添加新的 Activity 的同时在旧的 android 项目中添加数据绑定?

How to add data binding in old android project while adding new Activity?

通过 Android studio 添加 activity 时出现数据绑定错误。如何解决这个错误?

我正在尝试通过 android studio 在现有项目中添加新的 Activity。那一次我用的是targetSdkVersion 30 sdk。但是在更新 targetSdkVersion 32 之后,尝试通过 android studio 添加新的 Activity 时,出现如图所示的错误。

这是我的build.gradle


plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
}
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 32
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.softwares.bannari.whatstouch"
        minSdkVersion 21
        targetSdkVersion 32
        versionCode 47
        versionName "1.1.47"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    bundle {
        language {
            enableSplit = false
        }
    }
    buildFeatures {
        viewBinding true
    }

}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
//    implementation project(path: ':nativetemplates')
    implementation 'com.google.firebase:firebase-analytics:21.0.0'
    implementation 'com.google.firebase:firebase-crashlytics:18.2.10'
    implementation 'androidx.navigation:navigation-fragment:2.4.2'
    implementation 'androidx.navigation:navigation-ui:2.4.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'

    implementation group: 'commons-io', name: 'commons-io', version: '2.6'
    implementation 'the.bot.box:appusagemonitor:2.1.0'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
//    implementation project(path: ':nativetemplates')
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'com.github.ybq:Android-SpinKit:1.4.0'
    implementation 'com.github.natheeshsunway:Android-Permissions:2.0.5'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'com.google.android.exoplayer:exoplayer:2.17.1'
    implementation 'org.jetbrains:annotations:16.0.1'
    implementation 'com.github.antonKozyriatskyi:CircularProgressIndicator:1.3.0'
    implementation 'com.rm:rmswitch:1.2.2'

    implementation "ru.tinkoff.scrollingpagerindicator:scrollingpagerindicator:1.0.6"
    implementation "com.airbnb.android:lottie:3.5.0"

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'com.squareup.retrofit2:retrofit:2.7.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    implementation 'com.squareup.okhttp3:okhttp:3.14.7'
    implementation "androidx.preference:preference:1.2.0"

    implementation 'com.google.android.gms:play-services-ads:19.7.0'
    implementation 'com.suddenh4x.ratingdialog:awesome-app-rating:2.2.1'
    implementation 'org.aviran.cookiebar2:cookiebar2:1.1.4'
    implementation 'eu.dkaratzas:android-inapp-update:1.0.5'
    implementation 'com.akexorcist:localization:1.2.6'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'

    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'


    def lifecycle_version = "2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.4.1"

}
repositories {
    mavenCentral()
}

首先删除ActivityGroupsActivityBinding的第一行(导入库),不要删除Binding class及其对象。然后单击 ActivityGroupsActivityBinding class 名称然后您将从左侧获得导入建议。或者您可以通过单击它然后按 alt+enter

再次导入

查看使用方法Databinding in Android