This document walks you through the basic process of building a simple, Jetpack-based app. This example is based on a sample Kotlin app called ComponentsBasicSample.
For guidance about how to create apps in general, see Build Your First App. To learn more about the classes that this document covers, see Android Architecture Components.
Set up your app
Perform the following steps to set up your app to use Jetpack:
- Launch Android Studio 3.2 or higher, and enter information in the Create Android Project and Target Android Devices as always.
- After the Create Android Project screen, the Add an Activity to Mobile screen appears, offering you a variety of templates to use in starting your project. The Activity & Fragment + ViewModel template, shown in figure 1, is designed to make it easy to incorporate Jetpack into your app. Click that template, and then click Next.
- On the Configure Activity screen, enter names for the initial activity, fragment, and ViewModel object with which to start your project. Optionally, you can also enter a fragment package path. Then, click Finish.
If you are writing your app in Kotlin, remember to check the Include Kotlin support box on the Create Android Project screen.

Figure 1. The Activity & Fragment + ViewModel template

figure 2. The Configure Activity screen for the Activity & Fragment + ViewModel template
Opening the java
folder in your project reveals,
as shown in figure 3, that
the project initially contains three classes: StartActivity
,
StartFragment
, and StartViewModel
.
Figure 3. Initial classes that the Activity & Fragment + ViewModel template places into a project
StartActivity
is your app's entry point. It is the stub of anActivity
to serve as the container for the fragments that appear in your app's initial screen.StartFragment
is the stub of an initialFragment
for you to use in your app.StartViewModel
is the stub of an initialViewModel
for you to use in your app.
Take advantage of Jetpack
With these pieces in place, you might choose to implement your ViewModel object, as in the following example:
class StartViewModel : ViewModel() {
private val _data = MutableLiveData<String>()
val data: LiveData<String>
get() = _data
init {
_data.value = "Hello, Jetpack!"
}
}
If your app contains more than one screen, you can add the
Navigation
class to implement navigation triggers for your fragments. The
following code is an example of implementing a navigation trigger:
// Set up a click listener on the login button
view?.findViewById<Button>(R.id.navigate_bt)?.setOnClickListener {
// Navigate to the login destination
view?.let { Navigation.findNavController(it).navigate(R.id.end_action) }
If your app requires local access to SQLite data, you might also add the Room persistence library. If your app needs to display large volumes of data on a single screen, you should consider using the Paging library.
Configure your Gradle file
In order to use Jetpack, you must remember to add the appropriate lines to
your gradle file. Because this app uses
ViewModel
,
LiveData
,
and NavigationController
,
its Gradle file contains these lines:
// LiveData + ViewModel
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
// Navigation
implementation 'androidx.navigation:navigation-fragment:' + rootProject.navigationVersion
implementation 'androidx.navigation:navigation-ui:' + rootProject.navigationVersion
Learn more
To learn more about the sample and classes discussed in this document, see the following resources:
The Sunflower demo app uses many different Jetpack components to demonstrate Android development best practices.