Introduction to Android Jetpack

Learn about what’s available in Android Jetpack, a consolidation of a number separate libraries and tools under one banner that was announced at I/O 2018. By Kevin D Moore.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

LiveData

The LiveData library uses the Observer pattern for data but handles it in a lifecycle-aware manner. You get the benefits of automatic UI updates when data changes without calling UI elements when the UI is not in the correct state.

LiveData is the class that implements the observer pattern, holds that data, and notifies listeners when that data has changed.

You can find more information here: LiveData.

Room

If you have ever struggled working with the SQLite database in an Android app, you will appreciate what the Room library does for you. You create several simple classes that define your data and how to access them, and the Room library will do most of the rest. The only SQL code you have to write is for queries, which are usually pretty straightforward. And you gain compile-time checks of your SQL code when using Room.

There are three important classes you need to use with Room: Database (this contains your main entry point and holds a reference to the database object for the app), Entity (you create one for each table in the database), and DAO (this contains the methods for retrieving and managing the data).

You can find more information here: Room.

ViewModel

While the Room library persists your data in permanent storage, the ViewModel class allows you to hold onto data in device memory in a lifecycle-aware manner. One of the nice features of a ViewModel is that it can survive the re-construction of an Activity or Fragment over a configuration change such as a device rotation. The system will hold onto that ViewModel re-associate it with the Activity or Fragment. ViewModels are also where you can load data in the background and use LiveData to notify listeners that the data is ready.

You can find more information here: ViewModel.

Jetpack: Foundation

The Foundation area of Jetpack involves core system components, Kotlin extensions and Testing Libraries. This includes the AppCompat library, which you’ve probably been using for awhile, and the new Kotlin KTX extension library for easier development in Kotlin.

Testing is very important and has it’s own section to with frameworks to let you test your app, for both UI testing or unit testing.

Android app codebases are getting bigger and bigger, so you’ll want to visit the Multidex section of Foundation to see how to handle the 64K method limit.

You can find more information about what’s available in Foundation here: Foundation.

AppCompat

The AppCompat library in Jetpack Foundation consists of all of the components from the old v7 libraries. This includes:

  • AppCompat
  • Cardview
  • GridLayout
  • MediaRouter
  • Palette
  • RecyclerView
  • Renderscript
  • Preferences
  • Leanback
  • Vector Drawable
  • Design
  • Custom tabs
  • And even a few others…

You can find more information here: AppCompat.

Android KTX

Android KTX is the only new library in Foundation and is a set of Kotlin extensions designed to streamline the development of Android apps when using Kotlin.

There are several KTX modules that are linked to other libraries in Jetpack. For instance, if you are working with the Navigation library, then you could use:

  • android.arch.navigation:navigation-common-ktx
  • android.arch.navigation:navigation-fragment-ktx
  • android.arch.navigation:navigation-runtime-ktx
  • and android.arch.navigation:navigation-ui-ktx

SharedPreferences is an example of how using KTX can make your code simpler. Take a look at the Kotlin code below:

sharedPreferences.edit()
    .putBoolean("key", value)
    .apply()

Compare that with the KTX-based code:

sharedPreferences.edit {
    putBoolean("key", value)
}

The KTX code is streamlined a bit and removed the need to add apply().

And here’s a SQLite example without KTX:

db.beginTransaction()
try {
  // insert data
  db.setTransactionSuccessful()
} finally {
  db.endTransaction()
}

And the corresponding KTX version:

db.transaction {
    // insert data
}

KTX streamlines a SQLite transaction into a simple function call with a trailing lambda.

You can find more information here: Android KTX.

Test

The Test part of Foundation includes the Espresso UI testing framework and AndroidJUnitRunner for unit testing. Unit tests are for writing small tests on the logic within your code, usually at the level of individual methods. They should run fast and help you test a specific piece of logic. Espresso is used for the testing of UI elements.

You can find more information here: Testing.

Multidex

As you build out your app and include more and more libraries, your app can grow large enough that you need to use the Multidexing capabilities of Android. Once your app includes more than 65,536 methods across all classes, you will need to have the system split your .dex file (basically, a .zip file of classes) into multiple .dex files.

You can learn more about multidexing and how to use it here: Multidex.

Jetpack: Behavior

The Behavior area of Jetpack features libraries that help you interact with your user through the UI, including using video or sound. It includes many components such as media, notifications, permissions, downloading, sharing and the new Slices library.

You can find more information here: Behavior.

Notifications

Android Notifications have been around since the beginning but have changed over time. They have become more expressive and can contain buttons and images. Since Android 5.0 Lollipop, a notification called a heads-up notification can be displayed. You can even use notifications on Android Wear and TV for controlling media.

You can find more information here: Notifications.

Permissions

This part of the Behavior area showcases how to use and request permissions. Since Android 6.0 Marshmallow, permissions are now required to be requested and given before certain elements of a device’s components can be accessed, such as contacts, location and camera information. You declare permissions in the manifest, and you must deal with both cases of a user accepting or denying your permission request.

You can find more information here: Permissions.

Sharing

The Sharing part of Behavior explains how to share content and the ShareActionProvider class and how to use it. You will can share information with other apps and receive information from other apps. You can create a share action, share files and use ContentProviders for sharing data.

You can find more information here: Sharing.

Media

The Behavior area of Jetpack includes the MediaPlayer and AudioManager classes. You can play media and sounds, use the MediaPlayer in a service, and control the device volume. Android supports various media formats. You can also use the ExoPlayer library, which Google uses for it’s own media players in apps such as YouTube.

You can find more information here: Media.