Android Data Binding and Kotlin

泄露秘密 提交于 2019-12-10 12:39:52

问题


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?


回答1:


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
    }
    



回答2:


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

kapt {
    generateStubs = true
}



回答3:


I have recenly write Blog for Data Binding android with Kotlin here

Use Classpath

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

Dependency

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

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

dependencies {
    ......
    kapt 'com.android.databinding:compiler:2.3.1'
}

for more detail check out this post



来源:https://stackoverflow.com/questions/35814837/android-data-binding-and-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!