204

Gets following warning when building the project

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

I am using Android Studio Canary 6

user158
  • 12,852
  • 7
  • 62
  • 94

9 Answers9

379

Starting from Android Gradle Plugin 4.0.0-alpha05 there is a new block called buildFeatures to enable build features.

So in order to enable databinding with new AGP plugin you have do like following in module (ex: app) level gradle file

build.gradle ( Groovy DSL )

// shorter version
// android.buildFeatures.dataBinding true


// longer version

android {

    buildFeatures {

         dataBinding true

         // for view binding:
         // viewBinding true
    }
}

build.gradle.kts ( Kotlin DSL )

// shorter version
// android.buildFeatures.dataBinding = true


// longer version

android {

  buildFeatures {

         dataBinding = true

         // for view binding:
         // viewBinding = true
    }
}

Reference: https://developer.android.com/studio/releases/gradle-plugin#buildFeatures

user158
  • 12,852
  • 7
  • 62
  • 94
49

Put it in build.gradle(app level).It will work with android studio version greater than or equal to 4.0.0.

android {
    buildFeatures{ 
        dataBinding true // for data binding 
        viewBinding true // for view binding
    }
}
Community
  • 1
  • 1
Dinesh Jangir
  • 531
  • 4
  • 7
38

This warning occurs because


    dataBinding {
        enabled=true
    }

    viewBinding {
        enabled=true
    }

This code style is deprecated and it will remove from the gradle version 5 now if you still want to use this then you can use androidx legacy support dependencies

in app lavel build.gradle

implementation 'androidx.legacy:legacy-support-v4:1.0.0'

otherwise you can use new code style to enable data binding and view binding

like this

android {

  buildFeatures {

         dataBinding = true

         // for view binding:
         // viewBinding = true
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Arun Aditya
  • 828
  • 8
  • 10
13

Put this code in Gradle Scripts >> build.gradle(Module: appName.app)

after the buildTypes, include the data biding code

buildTypes {
       release {
           .......
          ........
       }
   }
 //here is the code...
   buildFeatures {
       dataBinding = true
   } 

That's all :)

Dayo Jaiye
  • 948
  • 1
  • 10
  • 17
6

If you're looking for the new feature viewBinding, try this for Groovy

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

and this for Kotlin

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

But, to use the default android data binding

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

also, be aware to use

kapt "com.android.databinding:compiler:4.0.0"
Amjad Alwareh
  • 2,926
  • 22
  • 27
4

1- add dataBinding under buildFeatures like this:

android {
...
buildFeatures {
        dataBinding true
    }
...
}

2- Change dagger version to 2.31.2:

annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.google.dagger:dagger:$daggerVersion"

3- Change also butterKnife version to 10.2.3:

implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
Houssin Boulla
  • 2,687
  • 1
  • 16
  • 22
4
buildFeatures {

        //just for dataBinding ,It has nothing to do with viewBinding 
        dataBinding = true

        //just for viewBinding ,It has nothing to do with dataBinding
        viewBinding = true
    }

Look at the notes above,so it should be very clear

Basi
  • 3,009
  • 23
  • 28
Chris.he
  • 41
  • 2
3

The following works:

android { compileSdkVersion 30 buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "com.poet.navviewmodeljave"
    minSdkVersion 19
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"
    //dataBinding.enabled true
    buildFeatures.dataBinding
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Jin
  • 156
  • 1
  • 4
0

You have to add buildFeatures in your gradle module

android {
    buildFeatures{
        dataBinding true
    }
}

example:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.demo"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    buildFeatures{
        dataBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'org.jetbrains:annotations:15.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}