I am trying to try out Kotlin and the Kotlin Android extensions in Android Studio. I have tried this both in Android Studio v 1.5.1 on Ubuntu 14.04, and in Android Studio v
I can't find it in the official documentation, but you must apply the plugin by adding the following to your build.gradle
file:
apply plugin: 'kotlin-android-extensions'
After applying the fixes mentioned above, I had to restart Android Studio to make it work.
The problem for me was the order in which I applied the plugins.
You must apply the kotlin-android plugin before the kotlin-android-extensions plugin
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
When you use Android Studio 2.0 and kotlin 1.0.1-2, you will come up with the same wrong. You cann't configure kotlin android extensions in your project's build.gradle, you need to configure and kotlin android extensions in every Module's build.gradle like this:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
buildscript {
ext.kotlin_version = '1.0.1-2'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc2"
defaultConfig {
applicationId "com.dazd.kotlindemo"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
Most importantly, even through the kotlin plugin included the kotlin android extension, you also need to configure the kotlin-android-extensions in your Module's bulid.gradle like this:
...
apply plugin: 'kotlin-android-extensions'
...
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
Of course, you could configure the kotlin plugin's classpath in your project's build.gradle.
Please try add to dependency block the next:
dependencies {
...
compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.2'
...
}
add apply plugin: 'kotlin-android-extensions'
in app/buildgradle
.
if you have already added it, try to remove it and sync gradle, when sync is complete, then add it back and Sync Gradle again. This work for me.