Android Instant Apps enables native Android applications to run in response to launching a URL, without installing the app. Instant apps can use many Android APIs and you use Android Studio to build them.
How do they work? When Google Play receives a request for a URL that matches an instant app, it sends the necessary code files to the Android device that sent the request. The device then runs the app.
Apps as features
Android Instant Apps provide a new and unique way for developers to build apps and for users to consume apps. Before introducing the core concepts of Android Instant Apps, it helps to understand some foundational terminology.
At a very basic level, apps have at least one feature or thing that they do: find a location on a map, send an email, or read the daily news as examples. Many apps provide multiple features.
For example, a map app might allow users to look up nearby restaurants or send links to locations by email in addition to finding locations on a map. Each of those actions—finding a location, looking up nearby restaurants, sharing links to a location—is a feature within the map app.
With Android Instant Apps, users can use a single feature of an app without having to install the app with all its other features. When users request a feature from an instant app, they receive only the code necessary to run that specific feature, no more and no less. After users have finished using the feature, the system can dispose of the feature's code.
Going back to the previous map app example, the map instant app can expose each of its features as a discrete entity within the app. Users can download and use just the location finder feature, just the restaurant guide feature, or just the share feature. Once users switch to another app, the system can safely remove the feature's code.
Each feature within the instant app should have at least one
Activity that acts as the entry-point for that feature.
An entry-point activity hosts the UI for the feature and
defines the overall user flow. When users launch
the feature on their device, the entry-point activity is what they see
first. A feature can have more than one entry-point activity, but
it only needs one.
Feature modules and feature APKs
To provide this on-demand downloading of features, you need to break up your app into smaller modules and refactor them into feature modules.
When you build an instant app project, the build output are Instant App APKs that contain one or more feature APKs. Each feature APK is built from a feature module in your project and can be downloaded on demand by the user and launched as an instant app.
Every instant app must have one (and only one) base feature APK. If your
instant app only has one feature, then you only need the base feature APK;
additional feature APKs are optional.
If your instant app has multiple features, then the base feature
APK typically contains shared resources and code files that other features
depend on. Following the map instant app example, the base APK might contain
the map app's base
styles.xml file or a data structure class for modeling
locations and points of interest. The base feature APK is always downloaded
regardless of what feature is requested by the user.
You can have feature APKs in addition to your base feature APK. Additional feature APKs can contain pieces of the app that correspond to a feature. The feature APK contains the entry-point activity for the feature and any unique resources the feature requires.
When users request a feature from an instant app, they get two feature APKs: the corresponding feature APK and the base feature APK. If the same user requests another feature from the same instant app, they might receive just the feature APK because they have already downloaded the base feature APK. Of course, if the instant app only has one feature—and thus only a base feature APK—users only receive the base feature APK.
The following diagram illustrates the relationship between the Instant App APK and feature APKs.
Requesting features from Google Play
To download a feature of an instant app from Google Play, users need only to click a link. After Google Play finds an instant app that matches the link requested by the user, Google Play sends the corresponding feature APKs for that feature to the user's device and the Android system then launches the feature. If Google Play cannot find a match, it alerts the Android system. The system then broadcasts an intent to the system to handle the URL.
For this reason, each entry-point activity in an instant app needs to be addressable: it needs to correspond to a unique URL address. If the URL addresses for the features in an instant app share a domain, each feature needs to correspond to a different path within that domain.
Following the previous maps instant app example, the app has three separate features: location finder, nearby restaurants, and share location. Each of these features corresponds to resources within a web domain, 'example.com'. To provide a unique URL address for each feature, the instant app specifies a different path underneath the domain for each feature.
As mentioned previously, a single feature can have multiple entry-point activities. For example, a feature might have two related activities that the user switches between, where each activity has its own URL address. The manifest for the feature needs to specify both a path for each activity and a priority order for the activities, in case of multiple matches.
For example, imagine that the location finder feature of the maps instant app has two activities, a search activity and a details activity. The URL address for the detail activity is similar to the search activity except that the detail activity has a numerical ID appended to the URL.
|Activity||URL address||URL path||Priority|
If Google Play received a request for the URL 'http://example.com/finder/1234', it matches both the search and the details activity. Google Play needs to pick one of the two activities as the starting point for the feature. Because the instant app specifies that the details activity has a higher priority than the search activity, Google Play specifies to the system to start the feature from the details activity.
To begin developing your own instant app, take a look at the Getting Started guide.
You might also want to read the Instant Apps Developer Stories, and watch these videos: