Let’s have a look at the most important announcements that were given during the conference in San Francisco:
- Kotlin is now officially supported by Google
- Android Studio 3.0 with new features (including Android Profiler)
- New features for Android O
- Instant apps are now available for all developers
- New features for Google Play Console
- Architecture Components
Today we cover Kotlin and Android Studio 3.0. Stay tuned for more articles about new features coming to Android!
Kotlin is now officially supported by Google!
If you are a member of the official Kotlin Slack channel, you’ve probably seen the explosion of enthusiasm, just a second after it was officially confirmed, that Kotlin is now supported by Google and is becoming a first-class programming language!
Kotlin has gone through a very interesting journey. When I started learning it over 2 years ago, I’ve already seen a great potential in Jet Brains’ programming language. In fact, in September 2015 I already wrote about programming in Kotlin. We’ve re-worked our news application for a Swedish newspaper – Omni to Kotlin in 2015. Of course, it was still a pretty early version, but it already supported a various features that were missing in Java (like Lambdas).
Here we are, almost two years later. In front of several thousand developers in San Francisco and hundreds of thousands via the Internet, Google announced official support for Kotlin. It wouldn’t be possible without supportive community, which constantly shared experiences and ideas for improvement. Even some of Google engineers are already active on the Kotlin scene with their side-projects. That definitely helped.
You may ask, what does it mean for the world of Android development? Well, first and foremost:
Start to learn Kotlin if you haven’t already.
The Internet is already full of courses, tutorials, documentation, examples and case studies. If you already know Java, it won’t take you a very long time to understand the logic behind JetBrains’s programming language.
For the foreseeable future, I would expect parity between the two languages. However, I’m pretty sure that switching from Java to Kotlin is going to be a natural process (in fact, it’s already happening, but it will speed up).
It’s easier than ever to start doing projects in Kotlin – Android Studio 3.0 supports it out of the box. You have an ability to start project in Kotlin without any extensions. You can run both languages on a particular project or switch libraries using a built-in converter.
You may think that official Google support will end the era of the independence of Kotlin. But JetBrains remains in full control and new versions of Kotlin will continue to be released regardless of Android versions. It means that release cycles won’t be affected.
You may also ask, why Kotlin? It obviously takes time and resources to port the application. Dan Lew wrote a great with the mentioned title “Why Kotlin?”, you should definitely check it out if you consider switching.
Kotlin doesn’t sacrifice comprehension in order to achieve brevity.
There are a ton of improvements that we could describe for hours, so let’s focus on 3 features that are missing from Java:
- Extension functions
- Null-safety
- Data classes
Extension functions
In short:
- It’s very easy way to extend a class with new functionality;
- You can use those functions and properties as if they were part of those classes;
- You don’t have to use any type of design pattern or inherit from the class;
- Extensions do not modify classes they extend;
It’s easy to describe by an example from the official Kotlin documentation:
To declare an extension function, we need to prefix its name with a receiver type, i.e. the type being extended. The following adds a swap function to MutableList:
fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] = tmp }
The keyword inside an extension function corresponds to the receiver object (the one that is passed before the dot). Read more here. It’s one of the deal-breaker features – after you start using it, there is no point to return to Java.
Null-safety
You’ve probably heard about a billion-dollar mistake? Sir Tony Hoare invented the null reference back in 1965. So easy to implement and such a horrible mistake that for years generated errors and crashes.
Thankfully, Kotlin drastically reduces the danger of null references. NullPointerException is not present in a way that it exists in Java. You can:
- Declare a variable as nullable string, written
String?
; - Your second option is the safe call operator, written ?.;
write b!!
, and this will return a non-null value of b (e.g., a String in our example) or throw an NPE ifb
isnull
;- Use safe casts that return null if the attempt was not successful;
There are plenty of options, but all of them are supported by additional functions that help you avoid null reference mistakes in the code.
Data classes
Data classes are used to (surprise!) hold data. In those classes derivables from the data defines the standard functionality of the class. This is how it looks like in the code:
data class User(val name: String, val age: Int)
After setting it up, the complier automatically handles derives from the properties declared in the primary constructor:
equals()/hashCode() pair, toString() of the form "User(name=John, age=42)", componentN() functions corresponding to the properties in their order of declaration, copy() function (see below).
Best of all, since version 1.1, data classes may also extend other classes which add an additional layer of functionality and gives you much more flexibility with the code.
It’s just a tip of the iceberg for Kotlin’s capabilities. All in all, it’s the best time to think about switching. The list of pros was already impressive, and official Google support is the cherry on the top that will shape the future of Android development for upcoming years.
Android Studio 3.0 with new features
One of the biggest changes in Android Studio 3.0 Canary is, of course, the Kotlin support, mentioned earlier. But the list of new features doesn’t stop here.
Designers will be happy that Layout Editor has been expanded, it’s now much easier to create icons looking attractive on a variety of devices (thanks to the Icon Wizard) and downloadable font-management is easier than ever.
But let’s focus on the new feature that I’m most excited about – Android Profiler. Google is giving a lot of attention to the resource usage in Android O. Google is going to limit the number of WiFi/locations scans and ensure that apps running in the background don’t consume too much battery.
New requirements come with a new tool to measure the performance and identify the source of problems. That’s what Android Profiler is all about. It provides a real-time and unified view of the CPU, Memory, and Network activity for your app!
To better analyze all the data, these live charts are combined with the data from UI event timeline. In the end you receive information that helps you identify if touch events, key presses or activities changes are the ones to blame.
Each of the segments of Android Profiler – CPU, Memory and Network has it’s own features. In the CPU Profiler you can trigger a sample or instrumented CPU trace and look out for the load spikes and potential bottlenecks. The Memory Profiler has the features from tools previously known as Heap Viewer and Allocation Tracker and is an all-in-one solution for detecting the source of poor RAM management.
The Network Profiler helps you to inspect the payload of each of your network requests and find the perfect balance between performance and mobile data usage. It works with HttpURLConnection, OkHttp (very reliable and one of the most popular; I already wrote about cache in OkHttp sometime ago), and Volley network libraries.
Last but not least, with the APK Analyzer Improvements, we can now analyze .zip files and AARs, and view dex bytecode of classes & methods. It’s nothing groundbreaking, but still a useful tool for optimizing the application size.
I still didn’t cover all of 20 biggest new features (many of them may interest designers and testers), check the full list here.
That’s it for today, please subscribe to our newsletter, so that you won’t miss our future Android articles.