Kotlin (libgdx and Android Studio)

    This page is to Setup and Config  Kotlin (libgdx and Android Studio)
 

install Kotlin

  • Install with Android Studio 3.X
  • Libgdx Generator and Modify to Kotlin
  • Create Kotlin by Android Studio


Libgdx Generate and Modify to Kotlin

  • Reference : https://www.javacodegeeks.com/2016/01/using-libgdx-kotlin.html

  1. Create a project by  https://github.com/libgdx/libgdx/wiki/Project-Setup-Gradle 

  2. Once the project is created, import it into your IDE. Usually importing/opening the build.gradle file from the root project root
  3. Modify build.gradle

  4. The version number can be obtained from Kotlin’s repository at maven central. http://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin
  1. buildscript {
    repositories {
    mavenLocal()
    mavenCentral()
    maven { url
    "https://oss.sonatype.org/content/repositories/snapshots/" }
    jcenter()
    }
    dependencies {
    classpath
    'com.android.tools.build:gradle:2.2.0'
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.30'

    }
    }

    allprojects {
    apply
    plugin: "eclipse"
    apply plugin: "idea"

    version = '1.0'
    ext {
    appName =
    "gdxkotlin"
    gdxVersion = '1.9.6'
    roboVMVersion = '2.3.1'
    box2DLightsVersion = '1.4'
    ashleyVersion = '1.7.0'
    aiVersion = '1.8.0'
    kotlinVersion = "1.2.30"
    }

    repositories {
    mavenLocal()
    mavenCentral()
    maven { url
    "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url
    "https://oss.sonatype.org/content/repositories/releases/" }
    }
    }

    project(
    ":desktop") {
    apply
    plugin: "java"


    dependencies {
    compile project(
    ":core")
    compile
    "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
    compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
    compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
    }
    }

    project(
    ":android") {
    apply
    plugin: "android"

    configurations { natives }

    dependencies {
    compile project(
    ":core")
    compile
    "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
    compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
    }
    }

    project(
    ":core") {
    apply
    plugin: "java"
    apply plugin: "kotlin"


    dependencies {
    compile
    "com.badlogicgames.gdx:gdx:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    }
    }

    tasks.
    eclipse.doLast {
    delete
    ".project"
    }
    7. Convert Java file to Kotlin file.

  •     In Kotlin, declared non null properties have to be initialised in the constructor or the class’s init method. But doing so, we won’t be using the libGdx lifecycle methods, so we can apply one Kotlin feature: the Late Initialized Properties. It is achieved by using the lateinit modifier.
    1internal var batch: SpriteBatch
    2internal var img: Texture
    Convert to 
1internal lateinit var batch: SpriteBatch
2internal lateinit var img: Texture


    8 The simplest way is to run it from the command line with the Gradle wrapper. In the project’s root, execute the following command (linux, OSX): ./gradlew desktop:run or on Windows gradlew.bat desktop:run