Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding and Kotlin

I am converting my Android application from Java to Kotlin. It is working fine, except when I try to convert a file that is using Android Data Binding Library. In that case, Android Studio complains at compile time about unresolved reference:

Error:(10, 44) Unresolved reference: AdapterHistoriesListBinding

Where AdapterHistoriesListBinding is the name of a file that should be generated by the Data Binding Library. It was working correctly in Java, so I guess it is an issue with Kotlin.

I am using Android Studio 2.0.0-beta6, Android Gradle Plugin 2.0.0-beta6 and Kotlin 1.0. Is there something to do to make the Data Binding Library work with Kotlin?

like image 519
Gaëtan Avatar asked Mar 05 '16 13:03

Gaëtan


People also ask

What is data binding in Android Kotlin?

In Android, the Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.

How do I add data binding to Kotlin?

Launch Android studio and create a new project. Once the project is ready, go to the Gradle scripts folder and open build. gradle (module: app) . Add buildFeatures and set databinding to true .

Should we use data binding in Android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.

Is data binding deprecated in Android?

Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.


2 Answers

Few steps to setup databinding in your Kotlin project.

  1. Tell kapt to use databinding compiler in module dependencies:

    dependencies {
      kapt 'com.android.databinding:compiler:2.0.0-beta6'
    }
    
  2. As Shintaro Katafuchi mentioned, you should tell kapt to generate stubs.

    kapt {
      generateStubs = true
    }
    
like image 188
Artem Ufimtcev Avatar answered Sep 28 '22 07:09

Artem Ufimtcev


Have you tried adding following setting in your build.gradle?

kapt {
    generateStubs = true
}
like image 20
Shintaro Katafuchi Avatar answered Sep 28 '22 09:09

Shintaro Katafuchi