Search code examples
androidlibgdxkotlin

Single Kotlin+libgdx codebase deployed to Android, desktop and browser?


Is this possible? I'm thinking that deploying to both Android and desktop shouldn't be too hard (since Java can already do it). I learned that Kotlin can also be transpiled to JavaScript. Does this mean that I can deploy the same codebase to all three targets, or will I need different code for the browser? (is it even possible to use libgdx when targeting the browser with Kotlin?)


Solution

  • It depends on your code base. First, Kotlin2JS is not officially released, it has many issues, so code that compiles for JVM will fail to run in JavaScript. Second, your code base should not depend on Java class library (i.e. java.lang, java.util, java.io, etc), since Kotlin2JS is source-to-source transpiler and it knows nothing about Java and Java libraries. LibGDX is written in Java, so you can't write your libGDX game on Kotlin and transpile it to JavaScript.

    For now, to transpile any Kotlin/Java/Scala code base, you can use TeaVM (it's not related to Kotlin team or JetBrains). The only problem is current stable release found in Maven (0.4.3) does not handle swap instruction properly, so you should download master branch of TeaVM (which has the appropriate fix) and build it yourself. Another problem is TeaVM does not know anything about dynamic keyword and @native annotation supported by Kotlin2JS.