LOADING

Type to search

Blog

What's new with Kotlin for Android developers

admin May 31
Loading...


As Kotlin rapidly gains popularity with Android developers, more and more tooling and APIs are being released to be more productive while making Android apps.
Google announced a lot of new features at Google IO 2019 surrounding Kotlin and Android – here are some of the highlights.

Project templates

Previously, when you used Android Studio’s templates to create a new activity or other Android component, it would create it in Java first, then you would have to use the Convert to Kotlin function to change it into Kotlin.
In the newest versions of Android Studio, it is able to generate new activities or fragments in Java or Kotlin.
You can select the language that it uses in the New Android Activity or New Android Component wizard.
Read more about it on the Android Developer site.

Coroutines for all the things

Google recently added support for coroutines in a lot of the Jetpack components.

WorkManager

Instead of extending the Worker class to use with WorkManager, you can now extend the CoroutineWorker class.
This class gives you access to the suspending function doWork(), which executes on a Dispatchers coroutine context of your choice.
Read more at the WorkManager documentation.

Room

Starting in Room 2.1.0 (now in beta), Room can use coroutines for database threading.
All methods in your DAO interfaces can be suspend functions, and Room’s generated code will support that.
For simple database operations, Room handles the threading and ensures that it is using a background thread (currently the IO Dispatcher).
For more complex operations, you can specify which Dispatcher you want the operations to execute on. Florina Muntenescu from Google has a more detailed post about it here.

Lifecycle, LiveData, and ViewModel

Lifecycle-aware Jetpack components also have coroutine support now.
Both Lifecycle and ViewModel create coroutine scopes (LifecycleScope and ViewModelScope), and any coroutine initiated within these scopes is automatically canceled when the scope is completed.
For Lifecycles, this is when it is destroyed, and for ViewModels it’s when it is cleared.
This makes it simpler to manage lifecycle-dependent work with coroutines.

LiveData can also be generated using coroutines in a lifecycle-dependent manner using the liveData builder function, which allows you to call suspend functions which emit their results into the LiveData.

Read more about lifecycle-dependent coroutine support on the developer documentation.

KTX – Android API Extensions

Last year around IO 2018, Google started releasing betas of KTX, an enormous set of libaries containing extension functions for Android components.
A lot of these now have stable versions, and new libraries for different components are still being added.
These are helpful for writing commonly used Android operations with an idiomatic Kotlin syntax.

Loading...

Generally, KTX enables the use of lambdas to allow the various APIs to use slightly less boilerplate.
For example, fragment transactions can be simplified:

// without ktx
getSupportFragmentManager()
            .beginTransaction()
            .addToBackStack("FragmentName")
            .add(R.id.fragment_container, fragment)
            .commit();

// with ktx
getSupportFragmentManager().commit {
  addToBackStack("FragmentName")
  add(R.id.fragment_container, fragment)
}

KTX is available for a variety of Android components, including preferences, text, views, animation, fragments, navigation, and many more.
Most of the newer Jetpack libraries have a -ktx version of the dependency that can be used in place of the standard one, and that’s all it takes to get access to these extensions.
Read more about them on the developer documentation.

Jetpack Compose

An exciting new development with the combination of Kotlin and Android is Jetpack Compose.
It uses an idiomatic Kotlin domain specific language to build intuitive layouts, directly from your Kotlin files.
The result is that you’re able to write more complex UI operations with less boilerplate code.
Right now, Jetpack Compose is still in pre-alpha stage, but it’s a good example of how the unique features of Kotlin can be leveraged on Android to create a better developer experience.
Given its early development status, Compose specifics are likely subject to change, but if you want to try it out early, check out the official documentation.

Kotlin scratch file

A Kotlin Scratch File is a runnable Kotlin script file, and these are now supported by Android Studio.
You can write any Kotlin in these files and Android Studio will evaluate each line and print the results in grey on the right.
You don’t even have to write a main() function; everything can be top-level.
I find this extremely useful when I want to quickly test a function I’m writing without having to actually run my app to test it.

To create one in Android Studio, go to File > New > Scratch File > Kotlin.
This will open a new file called scratch_1.kts or similar.
At the top, you can specify the classpath to a specific module, so you can access the existing code in your app with the scratch file.
By default, it’s set to use Interactive Mode, meaning AS will run your code as soon as it thinks you’ve stopped typing for long enough.
In practice, I found this to be a bit annoying, so I usually turn it off and just use the run button at the top left.

These scratch files are created in the support directory for Android Studio in the scratches directory by default, so you can access them later if you need to.

For this and the other improvements to Kotlin itself in version 1.3, see the release notes on Jetbrains Kotlin documentation site.

The hot new language

We’ve briefly looked at some of the new Kotlin+Android features announced at Google IO 2019 and how they can be useful to you.
Now that Kotlin is Google’s preferred language for Android development, I’d expect more features like these fairly frequently.
Try these out in your app today!



Source link

Loading...
 

Leave a Comment

Your email address will not be published. Required fields are marked *