Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Data Binding and Kotlin in Android Studio 3.0.0

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!

like image 367
Leandro Borges Ferreira Avatar asked May 17 '17 22:05

Leandro Borges Ferreira


People also ask

How do you use kotlin binding in activity?

To use View Binding in Activity, create an instance of the binding class, get the root view, and pass it to setContentView(). class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.

Can we use DataBinding and ViewBinding together?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.


4 Answers

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.

like image 81
hotkey Avatar answered Oct 07 '22 18:10

hotkey


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'
}
like image 38
Rubber Duck Avatar answered Oct 07 '22 19:10

Rubber Duck


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"
}
like image 37
Prokash Sarkar Avatar answered Oct 07 '22 17:10

Prokash Sarkar


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
    }
}
like image 35
Kirill Beloglazov Avatar answered Oct 07 '22 17:10

Kirill Beloglazov