paint-brush
Kotlin Support Public Betaby@Codename_One
150 reads

Kotlin Support Public Beta

by Shai AlmogJuly 11th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

We were prepared for an uphill effort in terms of porting Kotlin to <a href="http://github.com/codenameone/CodenameOne/" target="_blank">Codename One</a>…​ Turns out that getting the basic support out of the door was much easier than expected with a few expected caveats that will hopefully be acceptable moving forward. Steve did pretty much all of the work on this, I asked him to write a post where he explains the challenges as this isn’t the first time he ported a JVM language to Codename One.

Company Mentioned

Mention Thumbnail
featured image - Kotlin Support Public Beta
Shai Almog HackerNoon profile picture

We were prepared for an uphill effort in terms of porting Kotlin to Codename One…​ Turns out that getting the basic support out of the door was much easier than expected with a few expected caveats that will hopefully be acceptable moving forward. Steve did pretty much all of the work on this, I asked him to write a post where he explains the challenges as this isn’t the first time he ported a JVM language to Codename One.

I’ll try to cover the user experience of using Kotlin. But let’s start with the caveats:

  • You need to use IntelliJ/IDEA — I don’t think that’s a big sacrifice as it’s an excellent IDE with great Kotlin support. Doing this for other IDE’s would be a bit more challenging
  • You will need the latest 3.7.2 plugin we released the other day
  • You will need to install the Kotlin support libraries from the extension manager tool in Codename One Preferences
  • Don’t use the project conversion tools or accept the warning that the project isn’t a Kotlin project. We do our own build process
  • Warnings and errors aren’t listed correctly and a build that claimed to have 2 errors actually passed…​
  • This will increase your jar size by roughly 730kb which might make it harder for free tier users

Having said all that we got a couple of apps working on iOS, Android and on the web (using the JavaScript port) without a problem. This is still a beta thought so we might have missed some functionality.

Hello World

Ideally as we will refine this we will have a Kotlin option within the new project wizard that will already include all of the necessary pieces. But for now you can take the following steps.

Open your Codename One project with the 3.7.2 plugin or newer.

In the right click menu select Codename One → Codename One Preferences.

Select Extensions type in kotlin and install. Then right click the project select Codename One → Refresh Libs.

Conversion

The hello world Java source file looks like this (removed some comments and whitespace):

When I select that file and select the menu option Code → Convert Java file to Kotlin File I get this:

That’s pretty familiar. The problem is that there are two bugs in the automatic conversion…​ That is the code for Kotlin behaves differently from standard Java.

The first problem is that Kotlin classes are final unless declared otherwise so we need to add the open keyword before the class declaration as such:

open class MyApplication

This is essential as the build server will fail with weird errors related to instanceof so pay attention…​ We’ll try to make this fail on the simulator in the future.

The second problem is that arguments are non-null by default. The init method might have a null argument which isn’t used…​ So this fails with an exception. The solution is to add a question mark to the end of the call: fun init(context: Any?).

So the full working sample is:

Where do we go from Here?

That’s up to you. Our GUI builder still works with Java files, we can probably get it to work with Kotlin files too and improve the wizard to include support for new Kotlin projects.

We can add support for NetBeans & Eclipse. We can work on adapting our documentation to include Kotlin samples everywhere, the sky is the limit…​

We can do lots of things but to do those we need feedback and traction. Feedback helps us know what’s important to you, the people who use our product. Traction brings in new users and helps us promote what we are doing. If you care about this, let your friends know. They might ask for further Kotlin features and we’ll increase our investment in Kotlin.