
Posted by Wojtek Kaliciński, Developer Advocate, Android
Once we adopted Kotlin as a supported language on Android, after which shifted to a Kotlin-first method, one of many important drivers was the thrill and adoption from the developer neighborhood. As Kotlin has grown, we’ve seen continued funding within the language from JetBrains (Kotlin’s creators), the open supply neighborhood, and more and more our personal groups at Google.
As we speak we’re excited to share the information concerning the Kotlin 1.4 launch, the following milestone within the evolution of Kotlin, which accommodates new language options, improved compilers and instruments. Under you may discover a transient rundown of some thrilling new options on this launch. You possibly can learn extra about Kotlin 1.4 within the official announcement.
New language options
New language options launched in Kotlin 1.4 enhance the ergonomics of writing Kotlin code. This is only one instance:
SAM conversions for Kotlin interfaces
Beforehand, solely practical interfaces (i.e. having only a Single Summary Methodology – SAM) outlined within the Java programming language benefited from the shorthand syntax in Kotlin:
executor.execute { println("That is shorthand for passing in a Runnable") }
In Kotlin 1.4 now you can mark your Kotlin interfaces as practical and get them to work in an identical method by including the enjoyable
key phrase:
enjoyable interface Transformer<T, U> { enjoyable rework(x: T): U } val size = Transformer { x: String -> x.size }
You possibly can learn extra about new language options reminiscent of: mixing named and positional arguments, trailing comma, callable reference enhancements, and utilizing break
and proceed
inside when
included in loops on the Kotlin 1.4 release notes page.
Specific API mode
One further function is the brand new Explicit API mode for authors of libraries written in Kotlin.
It enforces sure language properties of Kotlin which can be usually elective, reminiscent of specifying visibility modifiers, in addition to specific typing for any public declarations, with a view to stop errors when designing the general public API of your library. Check with the linked documentation for directions find out how to allow Specific API mode and begin utilizing these further checks.
Compiler enhancements
The language options talked about above are a number of the most developer-facing adjustments in Kotlin 1.4, nevertheless the majority of labor went into bettering the general high quality and efficiency of the Kotlin compiler.
One of many advantages all builders can make the most of proper now’s the new, extra highly effective sort inference algorithm, which is now enabled by default. It can assist builders be extra productive by supporting extra smart-casts and instances the place sorts will be inferred robotically.
Aside from the sort inference algorithm, Kotlin 1.4 additionally brings in elective, Alpha stability compiler backends for Kotlin/JVM and Kotlin/JS, which generate code in what’s known as inner illustration (IR) additionally used within the Kotlin/Native backend.
The Kotlin/JVM IR backend is a requirement for Jetpack Compose, and Google engineers are working along with JetBrains to make it the default JVM compiler backend sooner or later.
That is why, even in the event you’re not presently creating with Jetpack Compose, we encourage you to check out the brand new Kotlin/JVM backend, presently in alpha, and to file any points and have requests to the issue tracker.
To allow the brand new JVM IR backend, specify an extra compiler choice in your Gradle construct script:
kotlinOptions.useIR = true
Strive Kotlin 1.4 now!
There are two steps to updating your tasks and IDE to Kotlin 1.4.
First, ensure you are on the most recent model of Android Studio to maximise the efficiency advantages and compatibility with the most recent Kotlin plugin. Android Studio will immediate you when a Kotlin 1.4.0 plugin that’s appropriate along with your IDE model is out there. Alternatively, you possibly can go to Preferences | Plugins and manually set off the replace.
As soon as the plugin is enabled, you possibly can improve your app undertaking to make use of Kotlin 1.4 by updating the Kotlin Gradle plugin model in your construct.gradle scripts. Relying on the way you handle your plugins, you both need to replace the model within the top-level undertaking’s buildscript
block:
buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0" } }
Or change the model quantity within the plugins
block in a module degree construct.gradle file:
plugins { id 'org.jetbrains.kotlin.android' model '1.4.0' }
Be sure that to learn the language adjustments rigorously and replace your undertaking’s code to make sure compatibility with the most recent launch. Take pleasure in Kotlin 1.4!
Java is a registered trademark of Oracle and/or its associates.