Gradle Script Kotlin 0.9.0 Release Notes

Gradle Script Kotlin 0.9.0 Release Notes

Rodrigo B. de Oliveira

Gradle Script Kotlin 0.9.0 Release Notes

Gradle Script Kotlin v0.9.0 is another major step forward in usability, bringing improvements to the DSL, IntelliJ experience, performance, and finally automatic detection of Kotlin based builds.

v0.9.0 is expected to be included in the upcoming Gradle 4.0 RC1.

The features in this release are also available for immediate use within the latest Gradle Script Kotlin distribution snapshot. To use it, upgrade your Gradle wrapper in the following fashion:

$ cd $YOUR_PROJECT_ROOT
$ gradle wrapper --gradle-distribution-url https://repo.gradle.org/gradle/dist-snapshots/gradle-script-kotlin-4.0-20170518042627+0000-all.zip

Updates since v0.8.0

  • Automatic detection of Kotlin based builds (#37, #80). After a little more than an year since the issue was first added to our board this huge usability improvement has finally landed!

    No more rootProject.buildFileName = 'build.gradle.kts' boilerplate or settings.gradle file required in order to enable Kotlin build script! :tada:

  • Default imports for the whole Gradle API (#215, #347). To make build scripts more concise, the set of default imports now includes the whole Gradle API as documented in the Default imports section of the Gradle User Guide.

  • Improved Gradle API with type safe setters (#341). Kotlin recognizes mutable JavaBean properties only when both the getter and at least one setter agree on the property type.

    More than 50 strongly typed setters have been recently added to the Gradle API enabling build scripts to migrate from invocation heavy configuration syntax such as:

    val someBuild by tasks.creating(GradleBuild::class) {
        setDir(file("some/path"))      // NOT RECOGNIZED AS PROPERTY BECAUSE OF UNTYPED SETTER
        setTasks(listOf("foo", "bar")) // NOT RECOGNIZED AS PROPERTY BECAUSE OF UNTYPED SETTER
    }
    

    to the more declarative:

    val someBuild by tasks.creating(GradleBuild::class) {
        dir = file("some/path")
        tasks = listOf("foo", "bar")
    }
    
  • Improved project extension accessors with properties (#330). So one can now write java.sourceSets instead of java().sourceSets as in 0.8.0.

  • API documentation (#209). A first cut of this important piece of documentation, generated using Dokka, is now available at https://gradle.github.io/gradle-script-kotlin-docs/api/.

  • IntelliJ improvements

    • Classpath computation is now asynchronous (#249). And should no longer block the UI (pending a fix to this IntelliJ issue)

    • Type-safe accessors are correctly included in the classpath given to IntelliJ (#340). Upon changes to the plugins block (pending a fix to this IntelliJ issue)

    • Source code navigation now works for everything Gradle (#281).

      source-code-navigation

    • Source code navigation to sources of included Kotlin libraries (#96). As long as there's at least one buildscript repository configured that can resolve the Kotlin source artifacts.

  • Miscellaneous

    • Polished Android Sample (#351). With all the improvements in this release, our hello-android sample is now boilerplate free:

      buildscript {
          dependencies {
              classpath("com.android.tools.build:gradle:2.3.1")
              classpath(kotlinModule("gradle-plugin"))
          }
          repositories {
              jcenter()
          }
      }
      
      apply {
          plugin("com.android.application")
          plugin("kotlin-android")
      }
      
      android {
          buildToolsVersion("25.0.0")
          compileSdkVersion(23)
      
          defaultConfig {
              minSdkVersion(15)
              targetSdkVersion(23)
      
              applicationId = "com.example.kotlingradle"
              versionCode = 1
              versionName = "1.0"
          }
      
          buildTypes {
              getByName("release") {
                  isMinifyEnabled = false
                  proguardFiles("proguard-rules.pro")
              }
          }
      }
      
      dependencies {
          compile("com.android.support:appcompat-v7:23.4.0")
          compile("com.android.support.constraint:constraint-layout:1.0.0-alpha8")
          compile(kotlinModule("stdlib"))
      }
      
      repositories {
          jcenter()
      }
      

      And it works with the latest Android Studio (2.3.2).

    • Gradle initialization overhead removed (#320). The implementation of type-safe accessors in 0.8.0 added some undue overhead to project configuration even when there was no Kotlin build script involved. This has been fixed.

    • Idiomatic support for Gradle's PropertyState<T> and ConfigurableFileCollection properties (#344). Via Kotlin delegated properties:

      open class GreetingPluginExtension(project: Project) {
      
          // Declare a `PropertyState<String>` backing field
          private
          val messageState = project.property<String>()
      
          // Expose `messageState` as the `message` property whose type is inferred as String
          var message by messageState
      
          // Can also be exposed as `Provider<String>` for additional functionality
          val messageProvider: Provider<String> get() = messageState
      
          // `outputFiles` property type is inferred as `ConfigurableFileCollection`
          // with the following behaviour:
          //  - getting will always return the original instance
          //  - setting will `setFrom` the source
          var outputFiles by project.files()
      }
      

      Check out the provider-properties sample for more information.

    • Better caching behaviour for type-safe accessors ([#338][338]).

  • Bug fixes

    • Setting non-existent Kotlin build script in settings.gradle no longer causes the build to fail (#302, #331). Following standard Gradle behaviour.

    • Generated extension accessor for the publishing extension will work as expected (#327, #328). And defer configuration until necessary.

    • Projects with Kotlin build scripts in buildSrc can be edited with the correct classpath in IntelliJ (#339). As build scripts will now be executed in a best-effort manner when computing the classpath.