oldsport76
oldsport76

Reputation: 407

Converting Android project with databinding from Java to Kotlin

I have a large Android project written in Java and am currently converting it to Kotlin. Many of my activites and fragments use databinding.

Initially, when I converted my first activity using databinding to Kotlin, the project was unable to build for this activity. The error said there were missing references to the databinding library. I added:

kapt 'com.android.databinding:compiler:2.0.0-beta6'

to dependencies in app/build.gradle and also

kapt {
    generateStubs = true
}

to the same file.

After building, the Kotlin activity with databinding worked, whereas all the remaining Java databinding activites now report that the databinding package reference does not exist.

This leaves me in a tough spot. I was planning on converting the activities and fragments one by one to Kotlin and building them as I go, but this seems not doable at the moment since I have to make a choice between Java or Kotlin working at every build.

I found Kotterknife: https://github.com/JakeWharton/kotterknife , which is a view injection library for Kotlin, but I still cannot believe that there is no way to slowly convert your activities from Java to Kotlin, even if they do use databinding. It simply does not seem credible to me.

Has anyone done converted a project in such a way in the past and succeeded without having to make a choice one or the other? Is there a way to convert single activities as you go along? The other option is to just convert all the Java to Kotlin in one go and then make one single build after it's all done, but that seems a bit risky if you put in all the work and then realize you overlooked something. Any ideas? Thanks in advance.

Upvotes: 2

Views: 606

Answers (1)

WindRider
WindRider

Reputation: 12308

Beware of the combination of Data Binding + Kotlin! The Data Binding library internally uses Kotlin and some conflicts arise from this. I would advise you to keep your activity/fragment classes that use data binding in Java for now and convert the business logic to Kotlin. I had a lot of problems in a fairly large projects. The more dependencies the project has, the bigger the risks are. Proceed to convert classes gradually, never all at once! If you convert multiple files at once, make sure your local or remote history is intact so that you can recover if needed. Few weeks ago I've into so severe bug that make the project was impossible to be built. Some really strange things can be expected. Also beware of the alpha and beta libraries and always update library dependencies one by one to catch any incompatibility as early as possible. I hope that helped a bit.

EDIT: As of May 20, 2017, I believe most of the aforementioned problems are fixed already. Also Kotlin is now an official language, so full compatibility should be guaranteed and continuously tested by Google.

Upvotes: 3

Related Questions