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:
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.
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.
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:
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.