Rewriting Android Library with Kotlin

After Google announced the official support for Kotlin, I see a lot of people started to learn about it. As for me, I decided to start learning from converting my Java library into Kotlin. Gradle’s Kotlin plugin offers a great converting feature which worked well for my project. However, there were some adjustments I had to make to actually release AAR file. I will leave those as a note to share other developers.

Before I move on, here are PRs I made for my debugging library Debot.

Tweaks to maintain the interface and logics

My library had a couple Singletons and Factory methods. When those Java files are converted to Kotlin, Singletons are converted to something like below.

From a code simplicity’s point of view, this is perfect. However, when these methods are accessed from Java, you need to call through INSTANCE.

I wanted to keep the interface same as before. Luckily, there is a convenient annotation called @JvmStatic. Also, companion keyword is usful for Factory method. So the final code became:

You can find useful documents from here:

Also, I found many nullable variables’ methods are called with unsafe !!operator.

I changed them to use ? or let instead. Document here helped me a lot.

Converting Test code

Converting Test code to Kotlin was same as other production code. The only change I made was introducing mockito-kotlin to create mock with Kotlin. To use mockito-kotlin, I had to change dexmaker-mockito as well.

See the links below and you will find a good explanation about Testing with Kotlin.

Building Artifacts from Kotlin

When building artifacts(usually AAR, javadocs, and source files) for my library, I had trouble creating javadocs. Since Kotlin is not Java, looks like currently there is no way to create javadocs for the Kotlin project. (I would appreciate if anyone let me know how to solve this issue)

:debot:mavenAndroidJavadocsjavadoc: error - Illegal package name: "/Users/tomoaki/Workspace/debot/debot/src/main/java/com/tomoima/debot/adapter/DebotMenuListAdapter.kt"javadoc: error - Illegal package name: "/Users/tomoaki/Workspace/debot/debot/src/main/java/com/tomoima/debot/annotation/DebotAnnotation.kt”
...

Work around was to disable creating javadocs.

ref: https://github.com/novoda/bintray-release/issues/71

Setting for Gradle to use Kotlin library

I released a snapshot build and imported to another project that already has Kotlin code. Then I got this error:

Error:Conflict with dependency 'org.jetbrains.kotlin:kotlin-stdlib' in project ':app'. Resolved versions for app (1.1.2-4) and test app (1.1.2-3) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

Unfortunately, the conflict could not be solved by excluding kotlin-stdlibfile from my library. Instead, I added the following code on build.gradle and forced using a specific version of Kotlin.

This might cause some issue if the version is significantly different, but should work in most of the cases.

Wrap up

I got lots of findings and learns through converting Java code to Kotlin. Moreover, the code written in Kotlin increased readability. Along with refactoring the code, I got rid of redundant codes from the original code base. I think converting is a good way to taste the world of Kotlin.

--

--

--

CTO at Knot, inc. Love to share ideas about React Native, AWS, and GraphQL. https://github.com/tomoima525

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tomoaki Imai

Tomoaki Imai

CTO at Knot, inc. Love to share ideas about React Native, AWS, and GraphQL. https://github.com/tomoima525

More from Medium

Why Kotlin will replace Java for Android App Development

Android Kotlin RecyclerView with DataBinding

Bloody (Android) Handlers

Android Retrofit 2.0 using kotlin