Author/Florina Muntenescu, Android Developer Advocate

Android Jetpack is a set of libraries, tools, and guidelines to help developers follow best practices, reduce template code, and write code that runs consistently across Android versions and devices. Today, 84% of the top 1,000 apps on Google Play use Jetpack.

Let’s take a look at the latest updates to Jetpack. If you’ve already seen the Jetpack update presentation, this article will be updated further, so don’t miss it!

Stable channel update list

CameraX

The CameraX library provides a unified API interface for accessing camera functionality across operating system versions, including device-specific compatibility fixes and workarounds. Some of the most recent enhancements to the library address common functional requirements, including support for adjusting exposure compensation and accessing more detailed information about the camera’s state and functionality. In addition, it is now possible to change camera Settings, such as the FPS range, through Camera2Interop while the camera is running. The library also brings support for the latest device and operating system features, including high dynamic range (HDR) previews, zoom rate control, and support for Android do not disturb mode. But perhaps most importantly, the CameraX library continues to address performance issues, making image capture and initialization faster, especially on older devices.

Hilt

Hilt is Jetpack’s recommended dependency injection solution built on top of Dagger. As part of the transition to stable release, ViewModel support for Hilt has been moved up to the core Hilt Android API, and SavedStateHandle has been added as a default dependency in ViewModelComponent. In addition, Hilt is now integrated with Navigation and Compose: you can get an annotated Hilt ViewModel whose scope is the destination or Navigation graph itself. Developers are already starting to use Hilt in their applications, so check out this blog post to see what they’ve learned.

The Paging 3.0

A Paging library enables you to load and display small pieces of data to improve network and system resource consumption. This release features a complete rewrite using Kotlin, first-class support for coroutines and flows, asynchronous loading with RxJava and Guava primitives, and comprehensive improvements to the repository and presentation layer.

Version 3.0 is a significant usability improvement over Paging 2 and was rewritten with partial and phased migration in mind so that developers can make the transition according to their own schedule. See Paging 3.0 documentation and Paging 3.0 Codelab for more details and hands-on experience.

ConstraintLayout and MotionLayout

ConstraintLayout (a flexible system for designing layouts in Jetpack) and MotionLayout (an API for managing motion and widget animation) are now in stable release. MotionLayout now supports foldable devices, image filters, and motion effects. Check out this Google I/O talk to learn more about what’s new with design tools.

Security Crypto

The Security Crypto library allows you to securely and easily encrypt files and SharedPreferences. If you want to encrypt SharedPreferences, please use the appropriate keys and create a EncryptedSharedPreferences object, and then use it as a standard SharedPreferences object.

val prefs: SharedPreferences = EncryptedSharedPreferences.create(
        context,
        "prefs_file_name",
        mainKey,
        prefKeyEncryptionScheme = AES256_SIV,
        prefValueEncryptionScheme = AES256_GCM,
)
// Use the resulting SharedPreferences object as usual.
prefs.edit()
    .putBoolean("show_completed".true)
    .apply()
Copy the code

Fragment

Over the past year, we’ve focused a lot on the Fragment library, cleaning up its internal implementation and reducing unrecorded behavior, making it easier for developers to follow best practices in their applications and write reliable tests. This sets the stage for future improvements to the library, such as support for multiple backstacks in Navigation, which may require some work to enforce the API contract strictly. Specifically, after updating the library, pay close attention to your tests. You can check the Fragment release notes for specific cases to watch out for.

The most recent version of Fragment also introduced the ActivityResult integration, making it possible to register Activity results from the Fragment. Fragment also adds a new FragmentOnAttachListener interface to replace the less flexible onAttachFragment method. Existing code that overrode this method in a Fragment or FragmentActivity will still work fine, but we’ve deprecated onAttachFragment to prevent new code from accidentally adopting a less flexible approach.

// Obtain the fragment manager. May be a childFragmentManager,
// if in a fragment, to observe child attachment.
val fm = supportFragmentManager


val listener = FragmentOnAttachListener {
    fragmentManager, fragment ->
  // Respond to the fragment being attached.
}


fm.addFragmentOnAttachListener(listener)
Copy the code

Beta channel update overview

Once the functionality of the development library is completed, it goes into Beta to maintain stability. After that, API changes are made only when major issues are identified or based on community feedback.

DataStore

DataStore provides robust data storage solutions that address the shortcomings of SharedPreferences while keeping API interfaces simple and highly available. DataStore comes with support for best practices, such as Kotlin coroutines with Flow and RxJava. DataStore allows you to store key-value pairs through Preference DataStore or type objects in protobuff format through Proto DataStore. You can also plug in your own Serialization solution, such as Kotlin Serialization.

Alpha channel update overview

The Alpha version of the library is in an active state of development: apis may be added, changed, or removed, but the content in the library is tested and generally highly usable.

AppSearch

AppSearch is a new on-device search library that provides high-performance, feature-rich full-text search capabilities. Compared to SQLite, AppSearch supports multiple languages, simplifies the sorting of query results, and has lower latency for indexing and searching large data sets.

AppSearch 1.0.0-Alpha01 brings LocalStorage support, which allows your application to manage structured data, called “documents,” and then query it. Your application uses “schema types” to define structures. For example, you can model a Message as a schema type that includes data such as subject, body, and sender.

Use the builder to create schema-type files and then add them to the store. Querying “Body: Fruit” will retrieve all documents with the word “fruit” in the message body.

In Android S, AppSearch will also provide PlatformStorage, allowing you to securely share data in your app with other apps, and your app’s binary size will be smaller because you don’t need to link to additional native libraries. Note that AppSearch is not currently available in Jetpack because it has not been developed for the Android S SDK.

Centralized storage on Android S+ for integration into all-device search

Room

Room is the data persistence layer we recommend for more availability and security on the platform.

Room 2.4.0-Alpha brings support for automatic migration. When your database schema changes, you can now declare a @automigration that indicates which version you want to migrate from and Room will generate the migration result for you. For more complex migrations, you can still use the Migration class.

@Database(
-   version = 1,
+   version = 2,
    entities = { Doggos.class },
+   autoMigrations = {
+         @AutoMigration (from = 1, to = 2)
+     }
  )
public abstract class DoggosDatabase extends RoomDatabase { }
Copy the code

Room 2.3.0 stability brings experimental support for Kotlin symbol handling, which in our benchmark Kotlin code was twice as fast as KAPT, as well as built-in support for enumerations and RxJava3.

Room also introduces the QueryCallback class, which provides callbacks when SQLite statements are executed to simplify tasks such as logging. A new @ProVidedTypeconverter annotation has also been added to give you more flexibility when creating type converters.

WorkManager

The WorkManager library is Android’s recommended way of scheduling deferred asynchronous tasks that run even if the application exits or the device restarts. WorkManager has improved the reliability of task tuning to ensure that all tasks are performed and provides various workarounds for specific Android OS versions.

The latest version of WorkManager has improved support for multi-process applications, including the performance benefits of unifying work request scheduling into a single process and limiting database growth when scheduling many requests.

Version 2.7 is now in alpha, developed for the Android S SDK, and fits the platform’s new foreground restrictions. Watch the Effective Background Tasks talk on Android for more details.

Background Tasks Inspector is now available in Android Studio Arctic Fox, making it easy to view and debug WorkManager jobs when using the latest version of the library:

Delta Background Tasks Inspector

Navigation

Jetpack’s Navigation library, a framework for navigating through applications, now provides support for multiple backstacks and simplifies destinations at the same depth, such as the bottom Navigation bar.

Macrobenchmark

The Macrobenchmark library extends Jetpack’s benchmark scope to include application startup and composite behavior, such as scrolling performance. You can use the library remotely to track metrics in continuous integration tests or locally with profiling results in Android Studio. Watch the Google I/O talk for more details.

For developers who want to integrate more closely with Google Assistant, the Google Shortcuts library provides a way to provide actions to Google Assistant and other Google services through the existing ShortcutInfo class.

You can send up to 15 shortcuts at a time through the ShortcutManager to show up in Google Assistant and other services, making them available for voice and other interactions.

To do this, you define a shortcut with an Intent and a capability binding; This binding provides semantically meaningful information that will help Google services figure out the best way to present it to users.

// expose a "Cappuccino" action to Google Assistant and other services
ShortcutInfoCompat siCompat =
  ShortcutInfoCompat.Builder(ctx, "id_cappuccino")
    .setShortLabel("Cappuccino")
    .setIntent(Intent(ctx, OrderCappuccino::class.java))
    .addCapabilityBinding(
        "actions.intent.ORDER_MENU_ITEM",
        "menuItem.name",
        asList("cappuccino")
    )
    .build()


ShortcutManagerCompat.pushDynamicShortcut(ctx, siCompat)
Copy the code

EmojiCompat

All user-generated content in your app contains 🎉, and supporting modern emojis is key to making your app ✨! The EmojiCompat library, which supports modern emojis in API 19 and later, has been moved to the new widget :emoji2: Emoji2, replacing the previous :emoji: Emoji widget. The new Emoji2 library adds 🪄 auto-configuration functionality with the AppStartup library (you don’t need to add any code 💻 to display 🐼)!

AppCompat added emoji2 from AppCompat 1.4. If your app uses AppCompat, users will be able to see the modern emoji ⭐ without further configuration. For apps that do not use AppCompat, you can add emoji2:emoji2-views. For custom TextViews, use emoji2:emoji2-views-helpers helpers, or inherit AppCompat views to support modern emojis.

Jetpack Compose

Jetpack Compose is a modern toolkit for building native UI on Android that simplifies and speeds up UI development on Android. Jetpack Compose is currently in Beta and plans to release a stable version in July. Many of the libraries mentioned in this article, as well as others you may already be using, offer features specifically for integration with Jetpack Compose. Including Activity, ViewModel, Navigation, and Hilt, all of these libraries can help you use Compose more smoothly in your application. Watch the Google I/O talk for more details.

Different types of equipment

Jetpack makes it easier to develop for a variety of devices, including foldable devices, large-screen devices, and Wear devices. We introduced new specifications for large-screen device development, as well as improved Jetpack libraries such as WindowManager and SlidingPaneLayout. Read this blog post for details.

conclusion

That’s an overview of what’s new for Jetpack. Please read the AndroidX release notes for all the update details for each library, and watch the Google I/O talk for details on some of these libraries.

If you have any feedback or questions, please submit them to us via the qr code below. Your questions may appear and be answered in the next FAQ. Thank you for your support!