BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JetBrains Releases Kotlin 1.2.30

JetBrains Releases Kotlin 1.2.30

This item in japanese

Bookmarks

JetBrains recently released version 1.2.30 of their popular programming language, Kotlin, as a bug fix and tooling update that comes about a month-and-a-half after the release of version 1.2.20. New features include:

  • New function, suspend, for coroutines.
  • Support for Gradle's build cache tool.
  • IDE support for the new Kotlin style guide.
  • New inspections, performance improvements, and bug fixes in the IntelliJ plug-in.
  • Support for TestNG in kotlin.test.

All new features and enhancements are supported in IntelliJ IDEA versions 2017.1 through 2017.3 and 2018.1 EAP, along with Android Studio 3.0. We highlight some of these enhancements here.

Multiplatform Projects

Kotlin Multiplatform Projects, introduced in November 2017 with the release of version 1.2, can compile the same codebase to multiple target platforms which, at this time, are the JVM and JavaScript.

As described on the website:

A multiplatform project allows you to build multiple tiers of your application ‐ backend, frontend and Android app ‐ from the same codebase. Such a project contains both common modules, which contain platform-independent code, as well as platform-specific modules, which contain code for a specific platform (JVM or JS) and can use platform-specific libraries. To call platform-specific code from a common module, you can specify expected declarations ‐ declarations for which all platform-specific modules need to provide actual implementations.

While regular Kotlin projects can be built with both Gradle and Maven, Gradle is the only build tool that supports Kotlin multiplatform projects at this time. The following is a basic build.gradle file for building a common module:

    
buildscript {
    ext.kotlin_version = '1.2.30'

    repositories {
        mavenCentral()
        }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }

apply plugin: 'kotlin-platform-common'

repositories {
    mavenCentral()
    }

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
    }
    

The multiplatform projects feature is currently experimental. JetBrains acknowledges that the design is subject to change in future releases of Kotlin, but will offer migration tools, if necessary.

New Function Added to the Standard Library

To further enhance the use of coroutines, a new function was added to the standard library:

    
public inline fun <R> suspend(
    noinline block: suspend () -> R
    ): suspend () -> R = block
    

This new function wraps a function literal and enables its use as a suspending function for coroutines. For example:

    
suspend {
    val result = deferredResult.await()
    renderResult()
    }.startCoroutine(completion)
    

This new function lays the groundwork for Kotlin to ultimately support the use of a suspend modifier in lambda expressions.

Kotlin Style Guide

IntelliJ IDEA supports Kotlin's new style guide that can be applied in Kotlin projects. A corresponding style guide for Android is also available. The style guides provide recommended coding conventions that include such categories as source code organization, naming rules, formatting, documenting comments, and idiomatic use of language features.

For mixed Kotlin/Java projects, the style guide recommends separate Kotlin and Java directories according to the conventions used by Gradle and Maven. For example, note the partial directory structure from the kotlin-examples GitHub project:

Compilation Performance Improvements

The first of these improvements is support for Gradle's build cache tool. Available since version 4.3, it allows reuse of outputs from other Gradle builds. As specified in the overview:

The Gradle build cache is a cache mechanism that aims to save time by reusing outputs produced by other builds. The build cache works by storing (locally or remotely) build outputs and allowing builds to fetch these outputs from the cache when it is determined that inputs have not changed, avoiding the expensive work of regenerating them.

It is not recommended, however, to use a build cache for Kotlin's annotation processor, kapt, because Gradle cannot accurately track annotation processing dependencies. This is disabled by default, but if necessary, may be enabled in a Gradle build script:

    
kapt {
    useBuildCache = true
    }
    

The second of these improvements is an experimental new feature that can more precisely track changes in Java files to avoid recompiling unchanged Kotlin files in mixed Kotlin/Java applications. It is disabled by default, but adding the following line to the gradle.properties file enables it:

    
kotlin.incremental.usePreciseJavaTracking=true
    

Full details for the entire Kotlin programming language can be found in the full reference documentation.

Resources

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT