androidkotlin

How to Fix error buildFeatures { viewBinding = true } that dont occur in viewBinding { enabled true }


I have just started using android development and trying to use Room library. Since yesterday I am facing this warning message that occurs when I import databinding on my MainActivity.kt

unresolved reference: databinding

It just runs fine when I input the ActivityMainBinding class

I was using android Gradle plugin version 4.1.2, so at the first I use buildFeatures { viewBinding = true } to migrate from kotlin synthetics and then when I try to import databinding to my MainActivity.kt the unresolved problem occur

so I try to use the older version of Binding viewBinding { enabled true } and it fix this issue

The problem is the older version of viewbinding will no longer support to android studio version 5

I want to know how to fix this issue, any clue?

Project Build Gradle Project Build Gradle Picture

MainActivity.kt MainActivity Picture

App Build Gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.testingapp.login_mysql"
        minSdkVersion 22
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    dataBinding {
        enabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Update

when I use the older version of viewbinding, indeed it fixes the issue above But, when I try to run the Application this error occur

DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.


Solution

  • add this your build.gradle

    android {
    buildFeatures {
         dataBinding = true
         viewBinding = true
    }
    }