Hey guys, I don’t get kotlin-dsl running. :confuse...
# gradle
s
Hey guys, I don’t get kotlin-dsl running. 😕 For testing I’ve created a simple hello tasks like that:
Copy code
task("Hello") {
  println("Hello World")
}
This works. Now I wanted to map this to my android project. I’ve updated the script to:
Copy code
buildscript {
  repositories {
    jcenter()
    google()
  }

  dependencies {
    classpath("com.android.tools.build:gradle:3.0.1")
  }
}

apply {
  plugin("com.android.application")
}

android {
  compileSdkVersion("26")
}
But if I run it I get:
build.gradle.kts181: Unresolved reference: android
build.gradle.kts193: Unresolved reference: compileSdkVersion
What I’m doing wrong? I’ve already checked out the samples. With the hello-android I don’t have that issue 😕
c
You won't be able to use
android {}
, it is not generated. Groovy dsl being dynamic can provide it dynamically, Kotlin can't. What you should do here is add import:
import com.android.build.gradle.AppExtension
then configure the android app extension:
Copy code
configure<AppExtension> {
	compileSdkVersion = "26"
}
if Google was kind enough to provide their plugins in gradle plugins repository you could've used
plugins {}
block and gradle-dsl would've been able to generate static
android
accessor, but hey, separatism rules and Google wants you to download plugins from their repo.
s
Wuhu 🎉 Thank you. That works! But how solve the
hello-android
the static android accessor?
e
The sample use plugin resolution rules to be able to request the android plugin using the
plugins {}
block, see here: https://github.com/gradle/kotlin-dsl/blob/develop/samples/hello-android/settings.gradle.kts
s
Wow. Thank you @eskatos. 👍 Haven’t thought that the
plugin{}
does all the magic stuff 🙂 Thanks guys 👍