Introduction to Android Jetpack
Learn about what’s existing in Android Jetpack, a consolidation of a number separate libraries and tools under one banner that was announced at I/O 2018.
Jetpack is a group of Android software components to make it easier for you to build up great Android apps. These mechanism help you follow best practices, free you from writing boilerplate code, and simplify complex tasks, so you can focus on the code you care about.
Jetpack embrace the androidx.* package libraries, unbounded from the platform APIs. This means that it provides backward compatibility and is updated more frequently than the Android platform, making sure you always have access to the latest versions of the Jetpack components.
Most Android apps now use the support libraries to assist users add all kinds of updated widgets and to address compatibility issues across Android devices. You’d be hard-pressed to locate an Android app that doesn’t use something from them, and they’re incorporated as a dependency in template projects made in Android Studio. Widgets as fundamental as Recycled View is included in them.
The support libraries are immense to use, but they have changed quite a bit over the years and the names have become somewhat confusing. There is com.android.support: support-v4 and com.android.support: support-v13, for instance. Finding at the names alone, do you know what classes are in them? The names are supposed to assign what version of Android they support, but, as they have changed, the minimum version has increased to API Level 14.
Google has understood that this variety of ever-changing libraries is very confusing for Android developers and has determined to start over. Consequently, it has created Android Jetpack as a way of both offering guidance and also as a recommended set of libraries and tools. Through Jetpack, Google provides components that form a suggested way to design your app. Furthermore, Google hopes that Jetpack will remove boilerplate code and simplify complex tasks while still providing backwards compatibility.
To assist address version confusion, Google is also rearranging all of the Jetpack-related libraries to version 1.0.0 and initiating their package names with a base name of androidx. furthermore, each component will be in its own library, though it’s important to note that Google is still working on transitioning libraries to the new package names.
There are a couple of other assets in Jetpack, including the Android KTX library, which makes it easier to use Kotlin with Android. And, Android Studio 3.2 Canary 14+ now has a move around to AndroidX option to convert your existing code and libraries to androidx .
Jetpack is divided into four main areas: Architecture, Foundation, Behavior and UI. Most of the things in Jetpack are a resetting and categorization of existing libraries, but a few of the things are new. While completing this article you should have a good sense of Jetpack’s features.
The Architecture area of Jetpack contains eight special libraries and tools that help you build your app and manage the data used by and displayed in the app. Most of these are existing libraries. But, there are three new libraries: Navigation, Paging, and WorkManager. Beginning with the newest libraries:
Navigating between actions and/or fragments has never been easy. Now, with the Navigation library and the navigation viewer construct into Android Studio, you can visually design how your screens are connected to one another. Several people have noticed that this is similar to Storyboards in Apple’s Interface Builder for iOS app development.
Using Navigation, you visually connect one screen with another using “destinations”. You make a navigation graph that will have a starting objective that can have actions to go to further destinations. The immense thing about this is that you can define animations in the visual editor. The library handles deep linking into your app. passing data between destinations can be done in a safe way with a new plug-in called safeargs. You can describe what arguments the Fragments in the navigation graph accept from within the navigation file itself.
Have you ever had to deal with big amounts of related data? The Paging library will help by offering ways to handle the paging of data in a Recycler View.
The Paging library uses various key classes: PagedList, PagedListAdapter, and DataSource. PagedList is a list that loads data idly from a DataSource, allowing the app to load data in chunks or pages. PagedListAdapter is a custom RecyclerView.Adapter that handles pages with a DiffUtil callback.
For the Data Source, you will use one of three subclasses: PageKeyedDataSource, ItemKeyedDataSource, or PositionalDataSource.
Over the years, there have been many systems build into Android for handling background task or alarms. They differ on various versions of Android and you have to write a lot of code to handle the different versions of the OS.
WorkManager resolve this problem and gives you one library for building deferrable, asynchronous tasks and defining when they should run. You can define one-time jobs or repeating jobs.
This library has been around for awhile. Data binding lets you join your data to your layout in XML, so that, when you change your data in running code, the views distinct by the layout are automatically updated. When you’re UI changes, your data objects are updated.
The Lifecycle library assists you listen for lifecycle events in other mechanism besides an activities and fragments. This permits you to have lifecycle-aware logic in places other than just an Activity or Fragment. The library works by using explanation on methods so you get notified for the events that you are interested in.
You implement Lifecycle Observer, explain methods and add this observer to a lifecycle owner, which is usually an Activity or Fragment. The Lifecycle Observer class can get the current lifecycle state by calling lifecycle.getCurrentState() and can use this information to avoid calling code when not in the correct state.
A Lifecycle Owner is an object that has Android lifecycle events. The support library action and Fragment classes already applied the Lifecycle Owner methods. A Lifecycle Owner has a Lifecycle object that it can return to let the observer know what the present state is.
The Live Data library employs the Observer pattern for data but grips it in a lifecycle-aware manner. You catch the benefits of automatic UI updates when data changes without calling UI elements when the UI is not in the correct state.
Live Data is the class that gears the observer pattern, holds that data, and notifies listeners when that data has changed.
If you have ever troubled working with the SQLite database in an Android app, you will be grateful for what the Room library does for you. You create many simple classes that describe 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 typically pretty straight forward. And you get compile-time checks of your SQL code when using Room.
There are three significant classes you need to use with Room: Database (this holds your major entry point and holds a reference to the database object for the app), Entity (you make one for each table in the database), and DAO (this holds the methods for retrieving and managing the data).
While the Room library persevere your data in permanent storage, the ViewModel class permits you to hold onto data in device memory in a lifecycle-aware manner. One of the great features of a View Model is that it can stay alive the re-construction of an Activity or Fragment over a configuration change such as a device rotation. The system will hold onto that View Model re-associate it with the action or Fragment. View Models are also where you can load data in the background and use LiveDatato notify listeners that the data is ready.
The Foundation area of Jetpack involves core system mechanism, Kotlin extensions and Testing Libraries. This contains 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 significant and has its own section to with frameworks to let you test your app, for both UI tests and unit testing.
Android app codebases are getting bigger, so you’ll want to visit the Multidex section of Foundation to see how to handle the 64K method limit.
The AppCompat library in Jetpack Foundation consists of all of the components from the old v7 libraries. This includes:
- Vector Drawable
- Custom tabs
- And even a few others…
Android KTX is the only library in Foundation and is a set of Kotlin extensions intended to streamline the development of Android apps when using Kotlin.
There are many KTX modules that are linked to other libraries in Jetpack. For example, if you are working with the Navigation library, then you could use:
- and android.arch.navigation:navigation-ui-ktx
The Test part of Foundation contains the Espresso UI testing framework and AndroidJUnitRunner for unit test. Unit tests are for writing little tests on the logic within your code, generally at the level of individual methods. They must run fast and assist you test a specific piece of logic. Espresso is used for the test UI elements.
As you create out your app and include more libraries, your app can produce large enough that you need to use the Multidexing capabilities of Android. Once your app contains 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.
The Behavior area of Jetpack features libraries that assist you interact with your user through the UI, including using video. It includes several components such as media, notifications, permissions, downloading, sharing and the new Slices library.
Android Notifications have been around since the beginning but have transformed over time. They have become more communicative and can hold buttons and images. Since Android 5.0, a notification called a heads-up notification can be displayed.
This part of the Behavior area displays how to use and request permissions. Since Android 6.0, permissions are now required to be requested and given before certain elements of a device’s components can be accessed. You state permissions in the manifest, and you must deal with both cases of a user accepting or denying your permission request.
The Sharing part explains how to share content and the ShareActionProvider class and how to use it. You will can share information with another app and receive information from another app. You can build a share action, share files and use ContentProviders for sharing data.
The Behavior area of Jetpack contains the MediaPlayer and AudioManager classes. You can play media, use the Media Player in a service, and control the device volume. Android supports several media formats. You can also use the ExoPlayer library, which Google uses for its own media players in apps such as YouTube.
The DownloadManager service assists you download files in the background. Avoid connection problems, retrying and even system reboots by using the DownloadManager service. Since the DownloadManager is a system service, you can just begin a download and listen for a broadcast event to handle the finished download. You need not to worry about network issues or crashes.
The Slices library is allows you create UI templates to share your data through the system in rich layouts. One of the instances Google gave at Google I/O was a weather app that can show more data depending on the space it has to show. presently, it is only used by the Google Search App but should extend to the Google Assistant.
You can build your app data available to these apps using Slices so that a user can find information from your app by using Google Search or the Assistant.
Many of the UI libraries in Jetpack are based on existing code. The EmojiCompat library is the latest of the libraries and gives you up-to-date emojis and the fonts.
This portion of Jetpack contains APIs for the various types of animations available on Android. The Jetpack site documentation covers the previous as well as new ways to use animations. Vector graphics and animations are included as well.
There is also a physics-based animation system that contains spring and fling animations. You can setup evolutions between activities as well as property and object animations. You can also build a layout to animate any updates you make to the layout.
EmojiCompat holds emoji characters and uses downloadable font support. This permits your app to stay up to date with the latest emojis without depending on the Android OS. You will have the latest emojis while you updating ever. There is a solid Span class called EmojiSpan that is used to create emojis in your text.
The Fragment support class has encouraged into this part of Jetpack. It contains the different kinds of fragments, such as: DialogFragment, ListFragment, and PreferenceFragmentCompat. An significant part of a Fragment is the lifecycle, and the Fragment class included in Jetpack is well-integrated with the Lifecycle class in Jetpack: Architecture.
A Layout describes the Views and ViewGroups in your app. In the Jetpack Layout documentation, you study how to declare layouts in XML and in code. It also explains some of the more common layouts, such as LinearLayout, RelativeLayout and the newer ConstraintLayout. You’ll choose tips on more specific features like creating lists of items with RecyclerView, as well as the card layout CardView.
The Palette library permits you to pick colors for themes and from images to make your UI’s equal your images. You can also create a palette and choose diverse colors using the Palette.Builder class. Some of the category of colors produced is: Light Vibrant, Vibrant, Dark Vibrant, Light Muted, Muted and Dark Muted.
If you are developing your app for Android TV, then the TV part of Jetpack is for you. You can manage TV Hardware and controllers and create a navigation system that works for TVs. There is the leanback theme that is used for TV layouts and the Leanback library assists with TV controls. You can build up a D-pad controller to let users navigate the TV UI.
Wear OS is one of the versions of Android for wearable devices. You can build an app that plays media, controls your media from a observe, or create a standalone watch app or watch face.
Jetpack assists you build apps for Android Auto — audible only, messaging, working with hardware and more. You can offer audio playback for the car as well as messaging. You can exam Auto apps on the car screen using the Desktop Head Unit (DHU), which is a testing tool that allows you to test without the hardware.
Where to Go From Here?
As you can see, there is pretty a lot included in Google’s new Android Jetpack package. All of the libraries you know can be found there, as well as many new ones. Now you can develop a complex applications easily by hire android developers with sound skill in Android Jetpack.