How to use Data Binding and Kotlin in Android Studio 3.0.0

31,604

Solution 1

It seems that you need 3 gradle entries in the app .gradle at module level to add data binding

  1. apply plugin: 'kotlin-kapt'
  2. android { ... dataBinding { enabled = true } }
  3. dependencies { ...... kapt "com.android.databinding:compiler:$compiler_version" }

Notice that I made compiler version a variable in the project level build gradle so it can be managed from a single place

default was: ext.kotlin_version = '1.1.3-2'

I added with bracket syntax:

ext{
    kotlin_version = '1.1.3-2'
    compiler_version = '3.0.0-beta6'
}

Solution 2

UPD: This was fixed for Android Gradle plugin 3.0.0-alpha3, in yout project root build.gradle, change the buildscript dependencies to use

classpath 'com.android.tools.build:gradle:3.0.0-alpha3'

This is actually a bug in the Kotlin Gradle plugin 1.1.2-4 inter-operation with the Android Gradle plugin 3.0.0-alpha1, caused by how the inputs and outputs of the tasks are set (and thus how the tasks are connected with the depends-on relation).

Thanks @VyacheslavGerasimov for creating the issue KT-17936.


As a temporary workaround, you can try to revert to Kotlin Gradle plugin 1.1.2-2 and disable incremental compilation:

In your project's root build.gradle, change the version of the Kotlin Gradle plugin:

buildscript {
    ...
    dependencies {
        ...
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-2'
    }
}

Add local.properties to the project root, with the following line:

kotlin.incremental=false

It is a known issue that the Kotlin Gradle plugin 1.1.2-2 and below crashes with the newest AGP versions, and disabling incremental compilation seems to fix that crash.

Solution 3

For those still looking for a proper solution, Google has already fixed this issue in Android Studio 3.0 Canary 3 build.

Friday, June 2, 2017

We have just released Android Studio 3.0 Canary 3 to the Canary and Dev Channels. The Android Gradle Plugin 3.0.0-alpha3 was also released through maven.google.com. This release has fixes to Gradle, Kotlin, and many other fixes. We continue to fix bugs in all areas of Studio 3.0 as we stabilize our features, so please continue to pass on feedback.

Working gradle configuration:

build.gradle (project)

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

build.gradle (module)

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


android {
    dataBinding.enabled = true
}
dependencies {
    kapt "com.android.databinding:compiler:3.0.0-alpha3"
}

Solution 4

If you use Kotlin Gradle plugin 1.3 and higher, you do not need to specify kapt "com.android.databinding:compiler:$plugin_version"

https://youtrack.jetbrains.com/issue/KT-32057

It is enough to specify dataBinding in your build.gradle file:

android {
    ...
    dataBinding {
        enabled = true
    }
}

or

android {
    ...
    buildFeatures {
        dataBinding true
    }
}
Share:
31,604

Related videos on Youtube

Leandro Borges Ferreira
Author by

Leandro Borges Ferreira

I’m a 30 years old electrical engineer from Vila Velha (Brazil), currently based in Berlin, Germany. I love software development, engineering, mobile design and pretty much everything related to technology.

Updated on July 08, 2022

Comments

  • Leandro Borges Ferreira
    Leandro Borges Ferreira almost 2 years

    I just started to use Android Studio 3.0.0, but every time I try to build my project I get this error:

    Error:Circular dependency between the following tasks:
    :app:compileDebugKotlin
    +--- :app:dataBindingExportBuildInfoDebug
    |    \--- :app:compileDebugKotlin (*)
    \--- :app:kaptDebugKotlin
         \--- :app:dataBindingExportBuildInfoDebug (*)
    (*) - details omitted (listed previously)
    

    I am using

    kapt "com.android.databinding:compiler:2.2.0"
    

    Before I was using

    androidProcessor "com.android.databinding:compiler:2.2.0"
    

    And it was working just fine... What I am doing wrong??

    Thanks!

    • Vyacheslav Gerasimov
      Vyacheslav Gerasimov almost 7 years
      Seems like a bug, created issue: youtrack.jetbrains.com/issue/KT-17936
    • Arsenius
      Arsenius almost 7 years
      This bug appear even without any data binding. You just use 1.1.2-4 version of kotlin and apply plugin: 'kotlin-kapt'. Then you will get this error Error:Circular dependency between the following tasks: :app:compileDebugKotlin \--- :app:kaptDebugKotlin \--- :app:compileDebugKotlin () () - details omitted (listed previously)
    • BoD
      BoD almost 7 years
  • Leandro Borges Ferreira
    Leandro Borges Ferreira almost 7 years
    I believe you meant gradle.properties
  • hotkey
    hotkey almost 7 years
    @LeandroBorgesFerreira, you use can any of them, because Gradle detects and interprets the local.properties file in your projects as well. Well, maybe local.properties is more suitable for local machine properties.
  • gderaco
    gderaco almost 7 years
    This leads to this error: stackoverflow.com/questions/44056104/…
  • hotkey
    hotkey almost 7 years
    @gderaco, make sure you disable Kotlin incremental compilation. The error you mentioned is caused by the incompatibility of the incremental compilation code in 1.1.2 with the newer Android Gradle plugin.
  • Arsenius
    Arsenius almost 7 years
    Actually to revert to 1.1.2-3 also works fine. Same issue with Android Studio 2.3.2 and gradle 2.3.2 when using Kotlin 1.1.2-4 together with kotlin-kapt plugin
  • Nimrod Dayan
    Nimrod Dayan almost 7 years
    @hotkey based on issuetracker.google.com/issues/38447344 this is an issue in the Android plugin, not kotlin gradle plugin. A fix was made and is planned to be released in the next 3.0.0 alpha release.
  • DarkLeafyGreen
    DarkLeafyGreen almost 7 years
    @Arsenius how you reverted? Did you switch to stable release of android studio?
  • Arsenius
    Arsenius almost 7 years
    How to fix this issue in Android Studio 2.3.2?
  • Arsenius
    Arsenius almost 7 years
    @LostinBielefeld just use classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" where kotlin_version 1.1.2-3 Yes I'm using stable release 2.3.2
  • Prokash Sarkar
    Prokash Sarkar almost 7 years
    You can use an older version of Kotlin plugin with incremental false. Check the previous answer by "hotkey"
  • 0leg
    0leg almost 7 years
    No, 1.1.2-4 still doesn't work. One still has to use 1.1.2-2 and also add kapt "com.android.databinding:compiler:3.0.0-alpha3" to your build.gradle (module) or you will get Unresolved reference: databinding.
  • Daivid
    Daivid almost 7 years
    Not fixed on 1.1.2-5 as well
  • SpaceMonkey
    SpaceMonkey over 6 years
    This is the correct answer, to add kotlin-kapt and that dependency line. The top answer isn't relevant any more
  • Shirane85
    Shirane85 over 6 years
    Liked the use of the version variable
  • Pavel Zaitsev
    Pavel Zaitsev over 4 years
    I am getting: - with all versions [kapt] An exception occurred: java.lang.NullPointerException
  • Rubber Duck
    Rubber Duck over 4 years
    @PavelZaitsev I haven't used android studio in a while