Search code examples
androidandroid-gradle-pluginandroid-viewbinding

When I add viewBinding in gradle in Android Studio, it comes an error


I am using Android Studio version 4.0.1.

When I add viewBinding, it comes an error .

Error when I add viewBinding in gradle.

buildFeatures {
    viewBinding = true
}

build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 5
        versionName "1.2.0"
        resValue("string", "growingio_project_id", "8979dc98cc52320f")
        resValue("string", "growingio_url_scheme", "growing.1f3e3791e1d6aee5")
    }
    compileOptions {
        sourceCompatibility rootProject.ext.sourceCompatibilityVersion
        targetCompatibility rootProject.ext.targetCompatibilityVersion
    }

    buildFeatures {
        viewBinding = true
    }

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

dependencies {
    implementation fileTree(dir: 'libs', include: "*.jar")
    implementation deps.swipeRevealLayout
    implementation deps.glide
    implementation deps.growingio
    implementation(deps.rxbus) {
        exclude group: 'com.jakewharton.timber', module: 'timber'
    }
    implementation deps.androidasync
    implementation deps.timber
}

Error :

Could not find method buildFeatures() for arguments [build_6zjavhoqnf2k7dfs2qrq542f3$_run_closure1$_closure5@6cd00094] on object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.

Why this error comes?

How can I solve this error?


Solution

  • To use buildFeatures in your build.gradle you have to use the android gradle plugin 4.0.x

    buildscript {
        //..
        dependencies {
            classpath "com.android.tools.build:gradle:4.0.0"
            //....
        }
    }
    

    Then you can use:

    android {
        //...
        buildFeatures {
            viewBinding true
        }
    }
    

    If you are using the android gradle plugin 3.6 you can use:

    android{
        //....
        viewBinding {
           enabled = true
        }
    
    }