Kotlin - New Language For Android |
Written by Mike James |
Thursday, 18 May 2017 |
This is not one you could have guessed and even with perfect hindsight it doesn't seem obvious. The latest Android Studio 3.0, now in the Canary channel, supports Kotlin, the JVM language that JetBrains invented in 2011. What does this mean for Android? There are few important things to say right at the start. The first is that Kotlin is a JVM language that mixes very easily with Java. The second is that there is a tool that automatically converts your Java app into Kotlin. Having tried it, the conversion seems to work fairly well. The reason is, of course, that Kotlin is basically Java with lots of syntactic sugar. Now at this point you need to realize that while sugar isn't necessarily a good thing, syntactic sugar usually is. Anything that makes code more concise without making it more difficult to understand is a good thing and Kotlin does succeed in this. The bottom line is that you could opt to convert all of your programs to Kotlin with out too much effort. You can also opt to just covert particular Java classes or modules so you can introduce Kotlin slow and easy. It inter-operates with Java almost perfectly. You can just call a Kotln code without any worries and most of the time you can call Java code without any changes. What changes are needed are small changes to the Kotlin code to tell it how to handle the Java. What this means is that you can call all of the JDK and Android library code and carry on writing programs as before. Trying it out is easy. All you have to do is download and install Android Studio 3.0 alongside you existing production version. You can then load an existing project and select Convert Java File to Kotlin and it just does it. There are still some rough edges and I found I had to restart an initial install and compile after clearing the cache, but it did more or less just work. The new Kotlin files are stored in the same project folder as the Java files with the extension kt. You could easily miss the fact that you were working with a Kotlin file rather than a Java file. For example, the converted onCreate function in Kotlin is:
The syntax is a bit strange, but it is completely understandable to any Java program familiar with the Java version of the function. Of course in this case the lambda isn't just an editor convention, it's how you write the function. The use of "as type" is also an immediate plus point over casting but there are more advantages. In particular the need to explicitly declare variables that can hold null might well make apps more reliable by eliminating, in the Kotlin code at least, null pointers. Another useful but more dangerous option is the ability to add methods - extension methods - to existing classes without having to create a new type. This is a feature that has been used extensively in C# to bring the benefits of new facilities, notably Linq. It makes you wonder if similar new features might make their way into mainline Android programming because of Kotlin. Put simply Kotlin is like Java but with all the repetitious boilerplate code reduced to a reasonable minimum. It is possibly what Java will evolve into if you wait long enough. Before the Android team picked Kotlin to be part of Android, it was very much a minority hobby. More JetBrain's folly than a language that had much chance of taking over the world. Now in one single act it has been transformed into something that could become as important as Java. JetBrain's people must be feeling good at the moment. You probably don't need reminding the Android Studio is based on JetBrain's IntelliJ IDE. Will this relationship between Google and JetBrains get any deeper? It is also interesting to speculate on the impact this might have on Google's lawsuit with Oracle. If by any chance Google was ordered to stop using Java it now has a lifeboat in the form of Kotlin. Smart move all round if you ask me. Look out for a more detailed news item on Android Studio 3.0.
More InformationAndroid Announces Support for Kotlin Related ArticlesJetBrain's Project Rider Cross-Platform IDE Project Rider, A Cross Platform C IDE Kotlin - another Java alternative?
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info
|
Last Updated ( Wednesday, 28 June 2017 ) |