Configuring Kotlin Build


Gradle Configuration

kotlin-Gradle-plugin is used to compile Kotlin code with Gradle. Its version should correspond to the Kotlin version you want to use. E.g., if you want to use Kotlin 1.0.3, you need to apply kotlin-Gradle-plugin version 1.0.3. It's good to externalize this version in Gradle. Properties (opens new window) or in ExtraPropertiesExtension.

Using Android Studio

Android Studio can configure Kotlin automatically in an Android project.

Install the plugin

To install the Kotlin plugin, go to File > Settings > Editor > Plugins > Install JetBrains Plugin > Kotlin > Install, then restart Android Studio when prompted.

Configure a project

Create an Android Studio project as normal, then press Ctrl+Shift+A. Type "Configure Kotlin in Project" in the search box and press Enter.

Android Studio will alter your Gradle files to add all the necessary dependencies.

Converting Java

This will change the current file's extension to .kt and convert the code to Kotlin.

To convert your Java files to Kotlin files, press Ctrl+Shift+A and find "Convert Java File to Kotlin File."

  • Targeting JVM: apply plugin: 'kotlin.'
  • Targeting Android: apply plugin: 'kotlin-android.'
  • Targeting JS : apply plugin: 'kotlin2js'

These are the default paths:

  1. kotlin sources: src/main/kotlin
  2. java sources: src/main/java
  3. kotlin tests: src/test/kotlin
  4. java tests: src/test/java
  5. runtime resources: src/main/resources