Why Android Architecture Components and Jetpack promise to shift how developers create apps
There’s a lot to like about the improvements to Android unveiled by Google at its recent I/O developers conference. As ArcTouch developer Fred Porciúncula wrote, we’re super excited about the evolution of Kotlin — and Google’s innovation with machine learning, augmented and virtual reality, and the new Android Studio (with its super-fast emulator) captured the attention of industry pundits and developers alike.
But for developers, the most significant news from Google I/O may have been the improvement of Android Architecture Components and the introduction of Jetpack. Together, they have the potential to significantly change how we, as an Android development company, have been building, testing and managing apps for the past decade.
Architecture Components make it significantly easier to manage app lifecycles and to work with databases. And three key new components — Navigation, Paging and WorkManager — will help us with pagination and to efficiently build background services and navigation. These new components solve common problems Android developers have always faced. The new components are also designed to be test friendly: It’s easy to write automation tests for your app when you are using the components, sparing you time and possible QA headaches later.
A little Android app development history
In the 10 years since the launch of Android, the way people develop software has evolved slowly. Best practices in app design and architecture have been driven mostly by the development community. New magical third-party frameworks have been introduced and there has been a constant ongoing debate about the best way to build Android apps. Google, of course, has been involved in that discussion. But the Android team (unlike Apple’s iOS team in many ways) has shied away from creating lots of rules and standards about how apps should be designed and built. Google has let things develop more organically.
Historically, Google has taken the role of developing tools — but not offering a lot of guidance on how to use them. Let’s face it: As much as we, as an app development company, love Android, sometimes it’s hard to make sense of all the tools and features Google lovingly puts at our fingertips. Google is awesome at innovation, but sometimes not so awesome at nicely packaging up its offerings.
Android Architecture Components: A philosophical shift
At the 2017 Google I/O, Google started to shift its philosophy by providing architectural solutions for common problems that every Android developer faces at some point. These solutions made up the first release of Android Architecture Components, a set of libraries guided by the separation of concerns (SoC) principle, making apps more modular, test friendly and easy to maintain. Lifecycle, LiveData, Room, ViewModel, and Data Binding were all part of the initial Architecture Components release. A year has passed, and a lot of improvements have been made on those core components: 26 new releases were published, and more than 700 feature requests/bug were opened, of which more than 600 have been closed.
Before I dive into the three key new features, there was one more related crucial piece of news for Architecture Components: the introduction of Jetpack.
Jetpack: The mega-guide to Android development
I was excited about the introduction of Jetpack at the recent Google I/O. Jetpack gives structure to the suite of powerful tools in Android Architecture Components, along with other fundamental components to help in the Google-named areas of UI, Behavior and Foundation (see graphic below).
Think of Jetpack as the new mega-guide to accelerate Android development. It aims to eliminate boilerplate code, decrease the number of crashes and memory leaks, facilitate backwards-compatibility and then increase productivity. Before Jetpack, it was not easy to find the correct tools to solve specific problems. Android documentation has always been good, but sparse. Jetpack organized all those amazing tools, summarizing the documentation and offering helpful guidance for all developers, but especially for Android beginners.
With Jetpack as the framework, we expect Google to add more components over time and help guide developers more than it has in the past — including the three new Android Architecture Components I’m highlighting below.
Navigation is a new component that handles screen transitions in a different way: You can use a visual editor to handle back and forward navigation.
“One thing you never have to worry about again is touching a fragment transaction with your bare hands,” said Google’s Lukas Bergstrom during a presentation about Navigation. The audience was delighted, to say the least.
Fragment transaction has been a problem that every app needs to solve and it hasn’t been easy. The new Navigation framework does all the dirty work for us.
The idea behind this is extremely simple: Instead of adding one of your fragments in your Activity, you should just add a NavHostFragment. This NavHostFragment is guided by a Navigation Graph (an XML file that can be edited using the visual editor), then the screen transitions are all guided by this graph. This is simple, functional and really quick to implement.
The Navigation Architecture Component also supports deep links and animations for screen transitions. Besides that, there’s a new plugin called safeargs, in which you can add fragment arguments in Navigation Graph. It generates builder classes to set the fragment arguments in a type-safe way. You can also define the default value for those arguments, ensuring the fragment will work as expected.
It is very common in mobile development to deal with data lists. Some of these lists may have thousands of items, and we don’t want to fetch all that data at once or performance will suffer. So, you need to develop your app in a way that allows you to load and display data in small portions. If you look on Stack Overflow, you’ll find hundreds of ways — some better than others — that developers have tried to solve this problem on Android.
The Paging component offers a standardized solution. Now you can fetch large amounts of data in an optimized way, saving processing, battery and bandwidth. We can use Paging to fetch data from your local database, from a network API or for both.
There are three classes you need to understand to use Paging efficiently:
PagedList: The most important component of the Paging library is PagedList. PagedList is a specialized list that is able to load data in pages. Here, you can configure things like page size, prefetch size and a placeholder to be shown while a screen is loading.
DataSource: This class is responsible to provide data to PagedList. DataSource knows how to fetch data according to your paging parameters (page size and current index or page). Here you can get data from network calls, a local database or any other source of information.
PagedListAdapter: This component is an enhanced ListAdapter to use PagedList in RecyclerView. The adapter observes PagedList and knows how to behave when new page loads.
WorkManager is a component created to simplify the creation of deferrable, asynchronous tasks that need guaranteed execution, even if the user leaves the app, such as synchronizing a database or downloading a playlist. This is great because it optimizes the use of Android asynchronous tools and uses the best option according to your device version and the app state.
Before WorkManager, Android had already provided many ways to do background processing, including JobScheduler, AlarmManager, and BroadcastReceiver. It’s really complicated to understand how all those solutions work and determine which is the best approach for your app.
Using WorkManager is much more simple. First, you need to create a Worker that defines what must be done in background. Then you create a WorkRequest for this worker, in which you configure things like: initial delay, whether internet connectivity is required, and if this request should repeat periodically. To finish, you just need to add your request in the WorkManager and it’s all set. WorkManager also provides LiveData of every request, then you can easily observe the request status and get the results when the request finishes.
Behind the scenes, WorkManager can use the JobScheduler if the device API is 23 or higher. If not, it will use previous background tools. The great part is you don’t need to create that boilerplate code to use the best tool for each device. WorkManager does it for us.
Can I start using Android’s Architecture Components now?
Hopefully you’re as excited as we are about the new Android Architecture Components and Jetpack. So you may be wondering if you can start using it now.
That’s really up to you — and your tolerance for experimentation. But in some of the Google I/O lectures and office hours — and even in Android Sandbox — Google is encouraging developers to start using these new components. Company reps have said all tools have been extensively tested and are virtually stable. However, they are still calling it an alpha version as they gather more feedback from the developer community before closing the APIs.
Whether or not you use it for commercial development, certainly you can experiment and gain a lot of experience now while it’s in an alpha state. And if you find any issues related to Architecture Components, you can file it on Google’s official issue tracker.
Wondering what Android’s Architecture Components and Jetpack mean for your app?
If you’re thinking about updating your Android app or building a new one to leverage Architecture Components or the new Android P, we can help. Contact us and we’ll be happy to share ideas on how you might take advantage of some of Android’s great new features.