Kotlin is still not top citizen of Android Studio 3.0

Elye
Elye
Oct 30, 2017 · 6 min read

In Google IO this year, Kotlin has been announced as First Class Citizen for writing Android App. The crowd cheered!…. BUT…

Android Studio 3.0 has been released, and everybody is happy they could use Kotlin with it. However, I think even though it may be called First Class Citizen, Android Studio 3.0 still treats Java as top class citizen. My reasons are broken down into 3 parts:

  1. Unchangeable fact: Something Android Studio CAN’T do a thing
  2. Conscious exclusion: Something Android Studio COULD do about
  3. Support failures: Something Android Studio SHOULD do about

Unchangeable fact

Kotlin as of today is run on JVM (Java Virtual Machine). The fact that it could be well adopted so quickly, is mainly because it relies on JVM and also works inter-operably with Java. This enables one to have Kotlin co-exist with Java in a single app development, as well as enable a smoother learning transition (i.e. one could convert Java code to Kotlin, and decompile Kotlin code to view it’s Java source).

In another word, I think it is not too harsh to say, without Java, there would be no Kotlin (or at least won’t get to its popularity that quickly).

From Android Studio 3.0 point of view, further evidence Kotlin is a sub-citizen

Within Android Studio, you will need to add JDK in order to use Kotlin. There’s no KDK that is independent from JDK one could load.

If you every create a new project, you’ll have ensure Include Kotlin support is checked to use it. By default if nothing is checked, Java reigns.

With this checked, Kotlin library and plugin is added as something below

// In root gradle dependencies
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// In module gradle...
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

It needs the extra library and plugins to work. Well, that’s an unchangeable fact given Kotlin lives within Java’s (or more specifically, JVM) domain. We pardon Android Studio for not able to help here.

Conscious exclusion

Nevertheless, there are something that shows Android Studio is still not making Kotlin as exclusive as Java. Let me provide three examples below

The source folder is still JAVA!!

When you create a project in Kotlin, and look at the Project Folder, you’ll notice something as below

What do you see? JAVA!! The source folder by default is Java, even though all the codes underneath are pure Kotlin.

If you want to put into Kotlin folder, you need to explicitly specify in your module build.gradle as below.

android {   ...    
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}

Well, Java‘s flag still flown high… even when there’s only Kotlin within.

Default libraries generated only in JAVA :(

Oh, you may say, well that’s because Kotlin lives within Java, so it’s okay that Kotlin’s code goes under Java source director, as long as Kotlin code is generated. Let me give you another example.

Once you have a project, try create a new module… Maybe a new Android Library module.

This time you realize that there’s no Kotlin checkbox for you to tick. You would think, since you have tick it when you create the project, it should create Android Kotlin library for you.

However to your dismay, when you create that, check out the Example Test code given… Java!!

public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}

Also, check in the new module build.gradle, it doesn’t have Kotlin plugins and library added for you. You’ll have to add Kotlin plugins and library manually. It’s like writing an application form to use Kotlin :(

Where is Kotlin template library?

If the above didn’t convince you Kotlin is not treated as superior as Java yet… check this out.

When you want to create a new plain Java library, Android Studio supply you with that. However, you won’t find that for Kotlin.

Support failures

Well, those things above perhaps is not for Android Studio 3.0. There might be plan in Android Studio 3.1… or later… It will come. So it’s okay not to have them in Android Studio 3.0.

But wait… there are something that clearly I think need to have it in Android Studio 3.0, but not there.

Protobuf Support for Kotlin in Android

Google introduce this great network communication mean, Protocol Buffer (aka Protobuf). It works on Android using Protobuf Gradle.

Both Protobuf and Android are owned by Google. I think it is presumably Kotlin should be able to work with it, as Kotlin is just an extension of Java, and also got the “First Class Citizen” status in Android.

However, to my dismay, in Android Application or Library, when the code is in Kotlin, the Gradle Plugin of it won’t able to find the Proto generated file. Convert the code to Java, the issue is resolved immediately.

I reported the issue here. https://github.com/google/protobuf-gradle-plugin/issues/176

No Code Coverage report for Kotlin in Android Studio 3.0

Maybe Protobuf is something still relatively new. Something that might be an oversight. Or maybe this is more of a gradle plugin issue. Forgiven.

Well, let’s go for something more common… Code coverage. That’s what we learn about back in college day to be a quality minded developer.

However if you want to check your test code coverage for Kotlin in Android Studio 3.0, wish you good luck….

Check out https://issuetracker.google.com/issues/64929213. In case you want to trace when will it get addressed.

To be fair, actually Kotlin Code Coverage support for Android Studio is possible in version 2.3. The Android Studio 3.0 enhancement has broken it :(


Well, hopes my arguments are reasonable that Java is still more superiorly treated in Android Studio 3.0.

Java is still the “local” to Android Studio, while Kotlin just the “newly converted citizen”. Hence I understand it takes time to get the same treatment.

My heart go out for Kotlin, and hope that it’s support in Android Studio will further improve. So I write this article to bring to awareness of what I found.

If you love Kotlin and would like a better support of Kotlin in Android Studio, do share this article, so this article get more attention. Hopefully Google will read this article and do something about it.

May Kotlin be given a better “First Class Citizen” treatment.

If you like my post and want to get future update, do follow me on medium, Twitter or Facebook.

Elye

Written by

Elye

Learning and Sharing Android and iOS Development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade