Configuring an existing Gradle project with Kotlin

suggest change

You can create a New Project in Android Studio and then add Kotlin support to it or modify your existing project. To do it, you have to:

  1. Add the Kotlin plugin to the root Gradle file. Add the kotlin-gradle-plugin classpath to the root build.gradle. The module applies it as kotlin-android.
buildscript {

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  1. Apply the Kotlin Android plugin. Add apply plugin: 'kotlin-android' to the module build.gradle.
  2. Add the Kotlin standard library. Use the same version as kotlin-gradle-plugin. This page uses 1.1.2.

A new module build.gradle can look like this:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "org.example.example"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'org.jetbrains.kotlin:kotlin-stdlib:1.1.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:appcompat-v7:25.3.1'

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    testCompile 'junit:junit:4.12'
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents