Error when adding buildFeatures in build.gradle file

27,870

Solution 1

You can add this only in Android Studio 4.0+ which is only available Canary build

Solution 2

I caught this error when I tried to add Jetpack to my existing app. I followed Suraj's answer, even used the newest Kotlin gradle plugin, and couldn't exactly figure out what was wrong. I also followed the official setup guide and it didn't work. Everything seemed okay, but nothing helped.

Installing Android Studio 4.0 canary didn't help either.

Turns out, it's not enough to just include certain dependencies – you need specific versions or higher. I was using an older Android Gradle plugin: 3.5.3. Upgrading to 4.0.0-alpha07 fixed the error:

classpath 'com.android.tools.build:gradle:4.0.0-alpha07'

Make sure to check your dependencies if you're adding Jetpack to an existing app

Solution 3

It seems that this

buildFeatures {
    viewBinding true
}

Is replaced with

viewBinding {
    enabled true
}

Reference

Solution 4

For build.gradle.kts I wasn't able to add it with

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

What worked was this:

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

Solution 5

To add jetpack compose to your project, you need to follow below steps:

Note: You should be on 4.1 Canary build of Android Studio

Step 1 : Inside the build.gradle file

android {
    defaultConfig {
        ...
        minSdkVersion 21
    }

    buildFeatures {
        // Enables Jetpack Compose for this module
        compose true
    }
    ...

    // Set both the Java and Kotlin compilers to target Java 8.

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Note: Jetpack Compose currently requires an experimental version of the Kotlin-Gradle plugin. To include this plugin in your app, include the following in your project’s build.gradle file

buildscript {
    repositories {
        google()
        jcenter()
        // To download the required version of the Kotlin-Gradle plugin,
        // add the following repository.
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0-alpha01'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.60-eap-25'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    }
}

Step 2: Add Jetpack Compose toolkit dependencies in your build.gradle file

dependencies {
    // You also need to include the following Compose toolkit dependencies.
    implementation 'androidx.ui:ui-tooling:0.1.0-dev02'
    implementation 'androidx.ui:ui-layout:0.1.0-dev02'
    implementation 'androidx.ui:ui-material:0.1.0-dev02'
    ...
}
Share:
27,870
Sreedev
Author by

Sreedev

I am a Digital Nomad who loves to code and a free thinker who brainstorm to make disruptive innovations for the human race to live better. Delivering work with quality is a personal constrain which I follow and I push myself to keep my skills in demand. I live each day to pass on the knowledge which I acquire and also to create a positive impact on the future generations. I am expertised in Java, Android, HTML, CSS and java script, pretty used to Unity 3d and Aurdino. Hybrid mobile development tools are newbies for me and I am gobsmacked. Am not a geek, I just have respect for my passions. Personal Site Technical Blog Reach out to my public repository in github to learn and explore. GITHUB

Updated on January 26, 2022

Comments

  • Sreedev
    Sreedev over 2 years

    I am trying out compose which is a new feature in Andorid jetpack. Below is my code. I am adding buildfeatures in build.gradle file of app, not in the root folder.

    android {
        compileSdkVersion compileSDKVer
        buildToolsVersion buildToolsVer
        defaultConfig {
            applicationId "com.sample.slothyhacker.jetpackcompose"
            minSdkVersion minSdkVer
            targetSdkVersion targetSdkVer
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    
        buildFeatures {
            // Enables Jetpack Compose for this module
            //compose true
        }
    
        compileOptions {
            // Set both the Java and Kotlin compilers to target Java 8.
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    

    But my project is giving me a compile error. I would really appreciate if someone can put some light on what am doing wrong.

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