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
Add kotlin-android-extensions
in our buildscript's dependencies:
1. In your project-level build.gradle
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
and apply the kotlin-android-extensions
plugin:
2. In your module-level build.gradle
apply plugin: 'kotlin-android-extensions'
Original answer:
Most likely it's a bug in the Kotlin plugin. I've filed an issue on their tracker.
Removing the following import fixed the issue for me.
import android.R
I found why mine didn't work. My blocks were misplaced and by moving the buildscript{}
block before the plugins as follow I got it working:
buildscript {
ext.kotlin_version = '1.0.0-beta-3595'
ext.anko_version = '0.8.1'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.kotlin"
minSdkVersion 15
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.1.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.anko:anko-sdk15:$anko_version"
compile "org.jetbrains.anko:anko-support-v4:$anko_version"
}
In my case, I had put the code referring the view in a companion object
. How silly..
For me the only thing that helped was to click "File" -> "Invalidate caches / Restart..."
The buildscript block containing the kotlin-android-extensions dependency apparently needs to be in the app-module build.gradle, not in the top-level one.