5

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.

2
  • 2
    It's a known issue youtrack.jetbrains.com/issue/KT-12402 Commented Jun 14, 2016 at 12:13
  • Yeah, thanks. Very useful tracker for those using Kotlin. Seems like something related to Kotlin Lint, I think Commented Jun 16, 2016 at 10:01

1 Answer 1

1

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.