[MUSIC PLAYING]
FLORINA MUNTENESCU: When using Android Java APIs in Kotlin,
you'll quickly realize that you're missing out
on some of the Kotlin features that make coding so much easier
and pleasant.
Instead of writing your own wrappers and extension
functions for these APIs, take a look
at the Jetpack KTX libraries.
Currently, more than 20 libraries
have a KTX version, creating sweet, idiomatic versions
of Java APIs ranging from Android platform
APIs to ViewModels, SQLite, and even Play Core.
In this video, we'll look at some
of the APIs available and peek under the hood
to see how they were made.
So let's get started.
So first, how do you discover the KTX functionality?
Well, as a best practice, always import the dash KTX artifact
when available.
As the dash KTX artifact depends transitively
on the non-KTX version, you don't need
to include the other artifact.
So for example, for a view model,
you get to artifacts, view model, and view model KTX.
And the KTX artifact will actually
contain the Kotlin extensions.
For extensions on the Android platform,
APIs import the core KTX artifact.
The majority of KTX functionality
is implemented as extension functions.
So you'll be able to easily find them by using autocomplete
in Android Studio.
Other functionality, like destructuring and operator
overloading, available on classes like color,
can be discovered by checking out the list of KTX extensions.
Core KTX provides idiomatic Kotlin functionality
for APIs coming from the Android platform.
So for example, if you're working
with shared preferences, when you
want to update the value instead of executing
three different calls, you can do just one.
Under the hood, the KTX edit method
calls the same functionality, providing a good commit default
option, apply.
Because apply, unlike commit, commits the changes
on disk asynchronously.
In core KTX, you'll also find an easier way
of handling frequently used platform listeners.
So for example, if you want to trigger an action when
text was changed in an edit text,
you'd have to implement all the methods of the TextWatcher,
even if you're actually only interested in onTextChanged.
Core KTX creates the corresponding TextWatcher
method--
doOnTextChanged, doAfterTextChanged,
and doBeforeTextChanged.
So in your Kotlin code, you can just use the one you need.
This brings several benefits-- your code becomes easier
to read, as it's more concise, and you get better naming
and all of the annotations.
You'll find similar lists in our APIs for AnimatorListener
and TransitionListener.
So under the hood, doOnTextChanged
is implemented as an extension function on TextView.
This is the class that also has the addTextChangedListener
event.
DoOnTextChanged creates end theme implementations
for the other functions of the TextWatcher.
So this was a TLDR of the platform KTX APIs.
But how about the Jetpack APIs?
Well, actually, the majority of extensions available
are for Jetpack APIs.
So let's go over some of the ones
that I actually found myself I'm using most often.
So first up, LiveData.
LiveData is ideal for exposing data to be consumed by the UI.
So to convert from full to LiveData and from LiveData
to flow.
The lifecycle-livedata-ktx artifact
provides two handy extension functions--
flow as LiveData and LiveData as flow.
A lot of the LiveData functionality
is implemented as extension functions, as well--
methods like map, search map, or this changed.
So for example, using map from LiveData KTX
removes the need to call transformations.map,
and allows us to call directly LiveData.map
in a more Kotlin idiomatic way.
When you observe a LiveData object,
you'll have to implement an observer.
But using the observe from lifecycle-ktx,
the code becomes simpler.
Make sure you call import and/or lifecycle.observe
if the method isn't found.
To construct the ViewModel, you would extend the ViewModel
class and implement ViewModelProvider.Factory
if your ViewModel has dependencies.
To instantiate it, use the ViewModels delegates,
so by ViewModels and Factory.
The ViewModels is available in the dash KTX artifact
of activity and fragment.
Both Room and WorkManager offer coverage and support
of the other KTX artifacts.
As we think it's worth covering these a little bit more
in depth, stay tuned for MAD Skills video focused
on these specific libraries.
AndroidX artifacts are not the only ones
to provide KTX versions.
Firebase has created common KTX extensions,
Google Maps offers maps and places KTX libraries,
and then Play Core has the core KTX artifact,
providing coverage and support for monitoring in-app updates.
Concise, readable, and Kotlin idiomatic-- these
are the features that will benefit your code once you
start using the KTX extensions.
Stay tuned for more ways to take advantage of Kotlin and Jetpack
in your app.
Thanks for watching.
[MUSIC PLAYING]