An Introduction to Android Instant Apps

Mateus Gondim
8 min readJul 6, 2017

--

What if we didn’t have to install an app in order to run it? Instant apps have now made this possible on Android! In this article, we’ll introduce the basic concepts around Instant Apps, and create a simple project to understand its structure.

Instant Apps?

Instant Apps are native Android apps launched from a URL, that run without being installed on the device.

Example: A friend sends you a link for a BuzzFeed article on WhatsApp. If you don’t have the BuzzFeed app installed, tapping on the link will open your browser and load the corresponding web page. With an Instant App, that link would instead open BuzzFeed app directly, downloading only the app module needed to load the screen associated to the link, even if you didn’t have that app installed.

Instant Apps were presented for the first time on Google I/O 2016, but it was only on Google I/O 2017 that the required development tools were made available to all android developers.

Motivation

Over the last years, Google has been exploring ways of reducing friction in the app installation process. That was one of the key goals of the new permissions system introduced in Android Marshmallow, for example, which doesn’t ask for permissions at install time anymore.

Instant Apps allow users to skip that step at first, bringing them directly to the experience of using a native app. From a URL that you get on an e-mail, for instance, you can access the corresponding app immediately, without the hassle of being redirected to Play Store and waiting for a few minutes before download and installation finishes.

Furthermore, apps discovery is hugely improved. Google search results may include links that take users straight to an instant app, so that they will not only discover a new app, but also experience it before making the decision to download it. Based on tests with a closed group of partners, Google reports a positive impact in a number of metrics, such as engagement and likelihood of users making in-app purchases, after the adoption of Instant Apps.

How does it work?

In order to download pieces of the app on demand, we need to break the app into components called feature modules. Each one of them represents a feature of the app. Through this new structure, it will be possible to generate both the installable and the Instant App APKs from the same code.

Every instant app must have at least a base feature APK. This module usually contains shared resources used by more than one feature. The base feature APK is always downloaded, regardless of which feature the user is requesting. The only exception is when the user requests a feature after having previously requested another one. In this scenario, the base feature APK is already downloaded, so it’s possible that only the feature APK itself will be downloaded this time.

This way, each feature module is associated to a unique URL, and upon accessing it, only the respective feature APK and the base feature APK are downloaded.

Note: Each module must have at most 4 MB in size.

URLs mapped to your app features must belong to domains you own. Google verifies its links with a DAL(Digital Assets Link) file, that must be stored in your website, so as to avoid apps to be connected to domains they don’t own in the first place. Click here for more details on how to set up that file.

Creating a Hello World Instant App

In this section we’ll create a simple Instant App and explore its basic structure.

The first requirement is to have Android Studio 3.0 installed. Click here to download it.

After launching Android Studio, open the Android SDK Manager(Tools > Android > SDK Manager), click on SDK Tools tab, select Instant App Development SDK, and finally hit Apply to install it. It’s also recommended to update the following items to their latest versions: Android SDK Build Tools, Android Emulator, Android SDK Platform-Tools, Android SDK Tools and Support Repository.

Currently, Instant Apps are supported only in specific devices and emulators:

  • Devices: Nexus 5X, Nexus 6P, Pixel, Pixel XL e Galaxy S7, running Android 6.0 or higher.
  • Emulator: Nexus 5X running Android 6.0 (API level 23), x86, with Google APIs.
System Image that must be selected is highlighted.

In this example, we are using the emulator. Click here for guiding on how to create one.

After the emulator is ready, let’s create a new project:

  1. Click on File > New Project.
  2. In the Create Android Project window: Fill in the fields Application Name and Company Domain with “My First Instant App” and “root.example.com”, respectively. Click Next.
  3. In the Target Android Devices window: Confirm that the option Phone and Tablet is selected and pick API 23 as Minimum SDK. Check the option Include Android Instant app support and click Next.
  4. In the Customize Instant App Support window, leave the default setup and click Next.
  5. In the Add an Activity to Phone and Tablet window, select the option Empty Activity and click Next.
  6. In the Configure Activity window: Fill in the fields Instant App URL Host and Instant App URL Route with “myfirstinstantapp.example.com” and “/hello”, respectively. Click Finish.

Voilà, we have a project! Before taking a peek at its structure, here are some point on what we just did:

  • In step 3, API 23 was chosen to illustrate that instant apps only work in devices running Android 6.0 or higher. Your project can even have a minSdkVersion set to lower than 23, but it won’t behave as an Instant App in those older versions.
  • In step 6, we are using a URL that doesn’t map to an actual website. As this app won’t be published, it’s not a problem. For a real case though, those fields must be filled with the website URL and the route that we want the feature to be associated with. For example, if we want to link the URL https://domainofmywebsite.com/search to the activity that’s being created, we should enter “domainofmywebsite.com” in the Instant App URL Host field, and “/search” in the Instant App URL Route field.

Analyzing the code structure

The project we just created has 4 modules: app, base, feature and instantapp. Let’s explore them one by one.

base

This is the base feature module, which will generate the base feature APK. As previously said, this module will always be present regardless of the feature requested by the user. It contains resources that may be shared by multiple feature modules, like the values in strings.xml and styles.xml, for instance. Here is its AndroidManifest.xml:

This is where we add the application tag’s attributes. It won’t be needed to replicate that data to the AndroidManifest.xml files from other modules. If the app needs to request any permissions, this would also be the place to add the uses-permission tags.

Points to highlight about this module’s build.gradle:

  • “apply plugin ‘com.android.feature’”: indicates that this module is a feature module.
  • “baseFeature true”: line required to identify this module as the one responsible for the base feature APK.
  • “application project(‘:app’)”: connects this module to the app module, which is responsible for the installable version of the app, as we shall see soon. By adding this dependency, the applicationId of the app is applied to the base module which, in turn, propagates that info to the other feature modules. This allows the installable APK and the Instant App APK to have the same applicationId.
  • “feature project(‘:feature’)”: declares that the feature module is a feature module(I know, a bit obvious), and as so, depends on this base module. All feature modules should be listed in this section. If we had another module of that kind called feature2, for example, we’d need to add the line “feature project(‘:feature2’)”.

feature

This is a feature module, and represents a feature of the app, such as a search or display of an item’s details. In this example, we have a single activity(MainActivity), but it’s possible to have several activities and/or fragments associated to the same feature. This would likely be the case if we were isolating a purchase flow, for instance. The res/ directory contains resources used only in this module, such as the MainActivity’s layout.

In its AndroidManifest.xml, the data tag determines the association between the MainActivity and the URL https://myfirstinstantapp.example.com/hello. Note that the manifest tag’s package attribute is different from the one defined in the base module, as each module must have a unique package name.

Just like in the base module, the plugin ‘com.android.feature’ must be applied to its build.gradle. In addition, the line “implementation project(‘:base’)” should be included to indicate that this module depends on base.

app

This module is responsible for generating the installable APK. It might contain code and resources specific to this kind of build. Here we have only the AndroidManifest.xml and the build.gradle. In the former, nothing except for the manifest tag and a package different from the other modules:

The gradle file must apply the ‘com.android.application’ plugin and add dependencies for all feature modules:

instantapp

Finally, this module will generate the APKs for your Instant App. It contains no code or resource, just this simple build.gradle:

Similarly to the app’s build.gradle, all feature modules must be listed as dependencies.

As we can see, the necessary changes to the code structure of an existing app are definitely not negligible, so planning is of the utmost importance. According to Google, the average time to convert an existing app to an instant app takes between 4 and 6 weeks.

Running the Instant App

As illustrated above, in the section Select Run/Debug Configuration of Android Studio, we can opt between the installable(app) and the Instant App(instantapp) versions before running the app.

Upon uploading an instant app to an emulator for the first time, Android Studio will run an initial setup process. After it is done, the dialog below should be displayed:

Click “Yes, I’m in” to enable Instant Apps. The app will then start.

Conclusion

Instant Apps unlock a whole new range of user experiences that Android developers can explore in their apps. Building on this new way of interacting, it’s possible to improve the experience of existing users, as well as expand your reach, attracting new ones. What’s more, complete new use cases can be created. However, it’s important to consider the specifics and goals of each app/business, and how an Instant App can be helpful indeed. Time and effort required to make the necessary changes to the code structure should also be taken into account.

Instant Apps are a recent technology currently available only in some countries and devices, but it should spread and become more popular over time, so as devs we should keep an eye on it!

--

--