Kotlin and Gradle

Anything libgdx related goes here!

Kotlin and Gradle

Postby newgame » Tue May 20, 2014 5:32 pm

For anyone who wants to use Kotlin with libGDX I'm going to post some notes here to make it easier for you to get started. I'll update the post whenever I find out something noteworthy to add.

----------------------

Kotlin & Gradle

In your top-level gradle.properties file you should define the kotlin version
Code: Select all
kotlinVersion = 0.7.270

The buildscript section at the top of the top-level build gradle file should have the following
Code: Select all
buildscript {
    repositories {
       // ...
        maven {
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    }
    dependencies {
        // ...
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

In the core section of the top-level build.gradle add the following:
Code: Select all
project(":core") {
    // ...
    dependencies {
        // ...
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    }
}

The build.gradle of the core project should look like this:
Code: Select all
apply plugin: "java"
apply plugin: "kotlin"
// ...
sourceSets.main.java.srcDirs = [ "src/main/java", "src/main/kotlin" ]
// ...

It is very important, that you have the directory structure as illustrated above. That is, a scr dir that contains a main dir that contains both a kotlin and a java dir. Consequently, you put your Kotlin code under the kotlin directory and your Java code under the java directory.

Best to save nullability annotations in the core folder (as afaik relative paths in Gradle don't work, i.e. ../annotations is not a valid path) and add the following to core's build.gradle
Code: Select all
compileKotlin {
    kotlinOptions.annotations = file('annotations')
}


More information: http://confluence.jetbrains.com/display ... uild+Tools

--------------------

Other Notes

To not make your fields nullable only because you cannot initialize them in a constructor for whatever reason you can use delegates. So instead of
Code: Select all
var camera: OrthographicCamera? = null
// ...
override fun create() {
    camera = OrthographicCamera()
// ...

And then having to convince the compiler every time you use camera that it is not null, you write
Code: Select all
var camera: OrthographicCamera by Delegates.notNull()
// ...
override fun create() {
    camera = OrthographicCamera()
// ...

More information in this blog post: http://blog.jetbrains.com/kotlin/2013/0 ... -and-more/
newgame
 
Posts: 16
Joined: Thu Jan 10, 2013 8:19 pm

Re: Kotlin and Gradle

Postby eggheadgames » Sat Aug 16, 2014 10:35 am

Thanks so much for posting all this info!

Have you tried any of this with RoboVM? I'm intrigued by the idea of starting to convert parts of my app to use Kotlin, piece by piece. But it has to keep working on iOS too...
Egghead Games - http://eggheadgames.com
eggheadgames
 
Posts: 137
Joined: Sat Apr 12, 2014 9:18 pm

Re: Kotlin and Gradle

Postby jrenner » Sat Aug 16, 2014 1:58 pm

You might interested in checking out the game called True Fool, it was written in Kotlin
Superior Tactics - tactical spaceship battles RTS
Google Play: https://play.google.com/store/apps/deta ... r.superior
jrenner
 
Posts: 520
Joined: Sun Apr 14, 2013 5:09 am


Return to Libgdx

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 1 guest