24

I am trying to enable data binding for my Android project using Kotlin. I have Kotlin plugin enabled, but I am not able to enable data binding I keep getting the following error

Error:(66, 0) Could not find method kapt() for arguments [com.android.databinding:compiler:2.0.0-beta6] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

I have the following dependencies for data binding in my gradle file

dependencies {
 ...
 kapt 'com.android.databinding:compiler:2.0.0-beta6'
} 

kapt {
    generateStubs = true
}

3 Answers 3

40

Edit: With Kotlin 1.1 and Kapt3 it works slightly different:

in your project build.gradle:

buildscript {

   ext {
       ...
       plugin_version = "2.3.1"
       kotlin_version = "1.1.2-3"
       ...
   }

   ...

   dependencies {
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
       classpath "com.android.tools.build:gradle:$plugin_version"
       ...
   }
}

and in your app build.gradle:

    apply plugin: "kotlin-android"
    apply plugin: "kotlin-kapt"
    ...
    android {
      ...
      dataBinding {
            enabled = true
        }
      ...
    }

    dependencies {
       ...
       kapt "com.android.databinding:compiler:$plugin_version"
       ...
    }

It's really important that the databinding compiler version and the plugin version are identical.
Also note that with kapt3, you shouldn't use the generateStubs flag anymore.


Old Answer

Having the Android Studio plugin enabled isn't enough, you also need to adjust your gradle files a little bit (add and apply the kotlin-gradle-plugin) Here are excerpts of my gradle files with working Java and Kotlin Databinding

In your project build.gradle:

buildscript {
...
 dependencies {
   classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.5"
   classpath 'com.android.tools.build:gradle:2.2.3'
   ...
 }
}

and in your app build.gradle:

apply plugin: "kotlin-android"
...
android {
  ...
  dataBinding {
        enabled = true
    }
  ...
}
kapt {
    generateStubs = true
}
dependencies {
   ...
   kapt "com.android.databinding:compiler:2.2.0"
   ...
}

(I'm using a newer version of the databinding compiler here, you probably should do this as well)

4
  • yes, I do have the kotlin-gradle-plugin. But I am still getting the error. I am trying to convert a java project to Kotlin. Commented Dec 20, 2016 at 4:10
  • @3xplore If your setup looks like mine, then maybe try to clear the Gradle cache stackoverflow.com/questions/13565082/…
    – Lovis
    Commented Dec 20, 2016 at 7:40
  • 2
    This solved my issue but keep in mind that it only works with kotlin_version = "1.1.2-3" and not with 1.1.2-4 on Android Studio 2.3.1
    – Kayvan N
    Commented Jun 1, 2017 at 20:02
  • @KayvanN yes, but it should be possible to change the versions according to your setup. the important part is, that the gradle plugin and data binding compiler use the same version.
    – Lovis
    Commented Jun 2, 2017 at 6:48
2

Try to include the missing blocks in your gradle files with the help of the following reference gradle file source.

App level Build.Gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.example.adventure.abc"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin/com/dougritter/marvelmovies'
    }
    dataBinding {
        enabled = true
    }
}




kapt {
    generateStubs = true
}

dependencies {
    //Compatibility
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    kapt 'com.android.databinding:compiler:2.3.0'

    //Libraries
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:support-vector-drawable:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile project(':domain')
    compile project(':androidutils')
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.jakewharton.timber:timber:4.5.1'


}

Project level Build.Gradle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.


buildscript {
    ext.kotlin_version = '1.1.2-2'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        mavenCentral()
    }
}


task clean(type: Delete) {
    delete rootProject.buildDir
}
-1

You don’t need to include any extra libraries to enable view binding

remove kapt "androidx.lifecycle:lifecycle-compiler:2.2.0" and add

android {
..
    buildFeatures {
       viewBinding true
    }
}

to build.gradle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.