37

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.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sreedev
  • 6,563
  • 5
  • 43
  • 66

16 Answers16

43

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

Morozzzko
  • 580
  • 5
  • 12
  • I'm told to never upgrade the Gradle and every time I do I have to re-install everything again! – Dave Jun 12 '21 at 14:56
41

It seems that this

buildFeatures {
    viewBinding true
}

Is replaced with

viewBinding {
    enabled true
}

Reference

Moaz Rashad
  • 1,035
  • 1
  • 10
  • 16
Kresimir Plese
  • 3,497
  • 1
  • 20
  • 15
13

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
}
Alex Burdusel
  • 3,015
  • 5
  • 38
  • 49
8

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'
    ...
}
Suraj Bahadur
  • 3,730
  • 3
  • 27
  • 55
  • Yeah! It's works. Thank you and I want to know where did you find that code? I didn't find that in [documentation](https://developer.android.com/jetpack/compose/setup#add-compose) – Thaw De Zin Mar 22 '20 at 09:31
6

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

Ranjeet Chouhan
  • 686
  • 5
  • 13
6

it depends on which version of Android Studio (and gradle) you're using. The above syntax is used for the newer versions of gradle (Android Studio 4.0 canary and above)

If you're using 3.5.3, please use the following:

dataBinding {
   enabled = true
 }

source: https://github.com/stripe/stripe-terminal-android/issues/90

sgtcadet
  • 159
  • 2
  • 4
2

Try to up you dependencies yourproject/build.gradle

buildscript {
    ext.kotlin_version = '1.4.10'

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.1'
    }
}

And yourproject/gradle/wrapper/gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
Leonid Ivankin
  • 579
  • 4
  • 9
1

This problem was solved for me by upgrading to Android Studio 4.1.2 and Gradle to the 4.1.2.

Overture
  • 83
  • 1
  • 10
1

this will resolve using Android 4.0 and above version.

if still not work then please try to set data binding

replace

android {
       buildFeatures {
           dataBinding = true
       }
   }

by this

    android {
            buildFeatures.dataBinding = true
   }

it is worked for me.

1

I experienced this problem when replacing Kotlin synthetics with viewBinding. Turned out I had placed the buildFeatures in the wrong part of the gradle file. I accidentally put it outside the android section, which is where I previously had the experimental flag set for androidExtensions.

So I had to take this:

android {
  ...
}

buildFeatures {
  viewBinding = true
}

And cange it to this:

android {
  ...

  buildFeatures {
    viewBinding = true
  }
}
Stonz2
  • 6,306
  • 4
  • 44
  • 64
1

I upgraded the Top level build.gradle file Gradle version to classpath 'com.android.tools.build:gradle:7.0.0' and distributionUrl in Gradle-wrapper.properties as distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip . This helped me get rid of the error.

0

It must be wrong build.gradle file in which you adding this. If you see, there are two files, we have to select build.gradle (Module: <project_name.app>), which is named as build.gradle (:app) when you open it.

Nick
  • 23
  • 1
  • 9
0

My Android Studio is of 3.2.1 version , and below code worked for me. Gradle Scripts -> build.gradle file, in the android section, add the following lines:

   dataBinding {
      enabled = true
    }
0

upgrade to the latest build at https://developer.android.com/studio/preview

Dave
  • 1,964
  • 1
  • 10
  • 11
0

Latest, Just change version of gradle in top-level(project-level) build.gradle file to latest,

     buildscript {
        repositories {
            // Gradle 4.1 and higher include support for Google's Maven repo using
            // the google() method. And you need to include this repo to download
            // Android Gradle plugin 3.0.0 or higher.
            google()
            ...
        }
        dependencies {
//Update This Line with latest Gradle version
            classpath 'com.android.tools.build:gradle:4.2.0'
        }
    }

check latest gradle version here

Kamau Mbûgua
  • 777
  • 1
  • 10
  • 19
0

Changing gradle version worked for me

classpath "com.android.tools.build:gradle:3.3.3"

to

classpath "com.android.tools.build:gradle:4.1.3"
Tarun Anchala
  • 2,232
  • 16
  • 15