https://kotlinlang.org logo
Title
s

StefMa

12/13/2017, 7:34 AM
Hey guys, I don’t get kotlin-dsl running. 😕 For testing I’ve created a simple hello tasks like that:
task("Hello") {
  println("Hello World")
}
This works. Now I wanted to map this to my android project. I’ve updated the script to:
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.kts:18:1: Unresolved reference: android
build.gradle.kts:19:3: 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

Czar

12/13/2017, 8:08 AM
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:
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

StefMa

12/13/2017, 8:40 AM
Wuhu 🎉 Thank you. That works! But how solve the
hello-android
the static android accessor?
e

eskatos

12/13/2017, 8:52 AM
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

StefMa

12/13/2017, 9:00 AM
Wow. Thank you @eskatos. 👍 Haven’t thought that the
plugin{}
does all the magic stuff 🙂 Thanks guys 👍