I am trying to use Data binding library in my project, that is written in Kotlin. I am using Kotlin v.1.0.2 & Gradle plugin v. 2.12 and with binding compiler. My build.gradle configured as follows:
android {
dataBinding {
enabled = true
}
}
kapt {
generateStubs = true
}
dependencies {
kapt "com.google.dagger:dagger-compiler:2.2"
provided "org.glassfish:javax.annotation:10.0-b28"
kapt "com.android.databinding:compiler:2.12"
}
In my Fragment layout I declared <data>
block with some variables:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
<import type="com.xxx.ui.AuthorizationPresenter"/>
<import type="com.xxx.entity.AuthProvider"/>
<variable
name="model"
type="com.xxx.ui.authorization.Model"/>
<variable
name="presenter"
type="com.xxx.ui.authorization.AuthorizationPresenter"/>
</data>
<FrameLayout
... />
</layout>
And then in my AuthorizationFragment.kt
class I am setting these variables:
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentAuthorizationBinding.inflate(inflater)
binding?.presenter = presenter
binding?.model = Model()
binding?.addOnRebindCallback(object : OnRebindCallback<FragmentAuthorizationBinding>() {
override fun onPreBind(binding: FragmentAuthorizationBinding?): Boolean {
val sceneRoot = binding?.root as ViewGroup
TransitionManager.beginDelayedTransition(sceneRoot)
return true
}
})
return binding?.root
}
And the problem is though this code successfully compiles and builds APK, Android studio marks these lines
binding?.presenter = presenter
binding?.model = Model()
as invalid and shows the following error: Cannot access class 'com.xxx.ui.authorization.AuthorizationPresenter'. Check your module classpath for missing or conflicting dependencies
.
I tried to add kapt 'com.google.guava:guava:19.0'
to my build.gradle
, but nothing has changed.
Also worth to mention that problem is only appears when I write my fragment in Kotlin, when it is written in Java everything is ok.
Until the IDE shows the error, you can use this:
binding.setVariable(BR.model, Model())
It is not so concise as kotlin form but the red color will not drive you crazy any more ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With