Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Codename One: Kotlin Support Public Beta

DZone's Guide to

Codename One: Kotlin Support Public Beta

Codename One is currently porting Kotlin and they need your help! Get a look at how Java-to-Kotlin conversion works and the work that still needs to be done.

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

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, so 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, though, 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 OneCodename One Preferences.

Select Extensions, type in kotlin, and install. Then right click the project, select Codename OneRefresh Libs.

Conversion

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

public class MyApplication {
    private Form current;
    private Resources theme;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");
        Toolbar.setGlobalToolbar(true);
        Log.bindCrashProtection(true);
    }

    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }
}


When I select that file and select the menu option CodeConvert Java file to Kotlin File , I get this:

class MyApplication {
    private var current: Form? = null
    private var theme: Resources? = null

    fun init(context: Any) {
        theme = UIManager.initFirstTheme("/theme")
        Toolbar.setGlobalToolbar(true)
        Log.bindCrashProtection(true)
    }

    fun start() {
        if (current != null) {
            current!!.show()
            return
        }
        val hi = Form("Hi World", BoxLayout.y())
        hi.add(Label("Hi World"))
        hi.show()
    }

    fun stop() {
        current = getCurrentForm()
        if (current is Dialog) {
            (current as Dialog).dispose()
            current = getCurrentForm()
        }
    }

    fun destroy() {
    }
}


That’s pretty familiar. The problem is that there are two bugs in the automatic conversion. 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 that 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:

open class MyApplication {
    private var current: Form? = null
    private var theme: Resources? = null
    fun init(context: Any?) {
        theme = UIManager.initFirstTheme("/theme")
        Toolbar.setGlobalToolbar(true)
        Log.bindCrashProtection(true)
    }

    fun start() {
        if (current != null) {
            current!!.show()
            return
        }
        val hi = Form("Hi World", BoxLayout.y())
        hi.add(Label("Hi World"))
        hi.show()
    }

    fun stop() {
        current = getCurrentForm()
        if (current is Dialog) {
            (current as Dialog).dispose()
            current = getCurrentForm()
        }
    }

    fun destroy() {
    }
}


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

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
kotlin ,java ,conversion ,intellij idea ,codename one

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}