Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(1 edit)

Part 2 - Configuring Kotlin

These instructions are based on the ones that I found here. However, they only worked for a desktop-only project and failed with an error for projects with an Android sub-project. The solution was to follow those instructions then tweak build.gradle a little. These steps are reproducible - I went through them twice more when writing this guide.

Go to the core project and find the single Java file.

Right-click on it and select New -> Kotlin File/Class.

Enter the name, setting it to the same as the Java file, and setting "Kind" to "Class".

You will be prompted to configure Kotlin. Click on the link to do this. It will make changes to build.gradle in the "core" sub-project.

Open the Java class and copy its contents to the clipboard.

Switch to the Kotlin class that you just created, delete its contents, then paste from the clipboard. Select "Yes" when prompted to convert the code from Java to Kotlin.

Delete the old Java file. Do not use safe-delete otherwise it won't work.

Insert "lateinit" in front of the "var" for both of the member variables.

 internal lateinit var batch: SpriteBatch
 internal lateinit var img: Texture

Edit build.gradle in the "core" project, replacing "kotlin-android" with "kotlin" and removing the "android" section.

The resulting file should look like this:

apply plugin: "java" 
apply plugin: 'kotlin' 
 
sourceCompatibility = 1.6 
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' 
 
sourceSets.main.java.srcDirs = [ "src/" ] 
 
 
eclipse.project { 
 name = appName + "-core" 
} 
buildscript { 
 ext.kotlin_version = '1.0.0-beta-3595' 
 repositories { 
 mavenCentral() 
 } 
 dependencies { 
 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
 } 
} 
repositories { 
 mavenCentral() 
} 
dependencies { 
 compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
}

Sync the project, then run the desktop configuration. Everything should work as expected.