Setting up Kotlin in Android Studio
Last time I made a simple Android app using the Spotify SDK. Now, it's time to convert this to Kotlin code. Kotlin is a statically-typed programming language that runs on the Java Virtual Machine.
Install Kotlin plugin
In Android Studio first download the Kotlin plugin. To use the plugin you can automatically configure or update your build configuration. In this example it will be configured manually:
- In
build.gradle
add the different version string and classpath for the plugin. These values will vary depending on the version you're using:
buildscript {
ext.support_version = '25.2.0'
ext.kotlin_version = '1.1.0'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
}
}
- Add the new dependency in
app/build.gradle
:
...
compile "com.android.support:appcompat-v7:$support_version"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
...
Convert to Kotlin
After syncing the new build configuration you should now be able to convert Java
to Kotlin code. In the Code
menu select Convert Java File to Kotlin File
.
Or use this short-cut on Linux: Ctrl+Alt+Shift+K
.
All changes made for the example app are viewable in this pull request.