Kotlin

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. Once the project is created, import it into your IDE. Usually importing/opening the build.gradle file from the root project root

    2. Modify build.gradle

7. Convert Java file to Kotlin file.

Convert to

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

                • 1

                • 2

              1. buildscript {

              2. repositories {

              3. mavenLocal()

              4. mavenCentral()

              5. maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }

              6. jcenter()

              7. }

              8. dependencies {

              9. classpath 'com.android.tools.build:gradle:2.2.0'

              10. classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.30'

              11. }

              12. }

              13. allprojects {

              14. apply plugin: "eclipse"

              15. apply plugin: "idea"

              16. version = '1.0'

              17. ext {

              18. appName = "gdxkotlin"

              19. gdxVersion = '1.9.6'

              20. roboVMVersion = '2.3.1'

              21. box2DLightsVersion = '1.4'

              22. ashleyVersion = '1.7.0'

              23. aiVersion = '1.8.0'

              24. kotlinVersion = "1.2.30"

              25. }

              26. repositories {

              27. mavenLocal()

              28. mavenCentral()

              29. maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }

              30. maven { url "https://oss.sonatype.org/content/repositories/releases/" }

              31. }

              32. }

              33. project(":desktop") {

              34. apply plugin: "java"

              35. dependencies {

              36. compile project(":core")

              37. compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"

              38. compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"

              39. compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"

              40. compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"

              41. compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"

              42. }

              43. }

              44. project(":android") {

              45. apply plugin: "android"

              46. configurations { natives }

              47. dependencies {

              48. compile project(":core")

              49. compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"

              50. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"

              51. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"

              52. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"

              53. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"

              54. natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"

              55. compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

              56. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"

              57. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"

              58. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"

              59. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"

              60. natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"

              61. compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"

              62. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"

              63. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"

              64. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"

              65. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"

              66. natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"

              67. }

              68. }

              69. project(":core") {

              70. apply plugin: "java"

              71. apply plugin: "kotlin"

              72. dependencies {

              73. compile "com.badlogicgames.gdx:gdx:$gdxVersion"

              74. compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

              75. compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"

              76. compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

              77. }

              78. }

              79. tasks.eclipse.doLast {

              80. delete ".project"

              81. }

              • internal var batch: SpriteBatch

              • internal var img: Texture

1

2

internal lateinit var batch: SpriteBatch

internal 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