Android Instant App: avoid beginners mistakes

Torcheux Frédéric
6 min readJan 13, 2020

--

Although instant app has been introduced in 2018, on a Play Store page of an application, you should not see the Try now button as often as that. There isn't so many developers that take the plunge and provide an instant app version of their application.

Try now button to download the instant app

However, Google shares very interesting data concerning applications which have an instant app version: installation and retention increase.

Here is a list of some developer success stories.

The goal of this article is not to explain what is an instant app and how to develop one from the first step. The official documentation already does it. The goal is to give you the missing information of the official documentation to avoid some bad surprises during development and at the time of the publication.

If you want to learn basics, I recommend you to check these 3 links:

Instant app development

The myth of 10Mo instant app

Everywhere in the official documentation, there is mention of a 10Mo limit to be able to release an instant app. However, this feature is in beta for now and not everyone can access to it.

You have to fill this form and hope that Google will whitelist you.

If you are not whitelisted, the limit is 4Mo.

Don't display notification except attached to a foreground service

It's not possible to display a notification in an instant app. If you do so, the instant app crashs.

For devices with Android >= 26, you can still display a notification if it's attached to a foreground service because a foreground service must be attached to a notification to work.

Nevertheless, if you really need to display a notification, you can fill a form to be whitelisted by Google

Special permission for a foreground service

Starting with Android Oreo (API 26), there is a special permission to start a service from an app in background: FOREGROUND_SERVICE. This permission is specific for an installed app. For an instant app, you must use INSTANT_APP_FOREGROUND_SERVICE.

All network traffic should be secured

An instant app runs in a special kind of SELinux sandbox for added security. Due to this secured environment, you can't have a not secured network traffic. All your requests must use HTTPS.

To allow this, you must disable the clear text traffic in your AndroidManifest.xml. Use usesCleartextTraffic="false" in the application tag or don't define usesCleartextTraffic because the default value is false.

Share data between instant app and installed app

According to the Android version of a device, data transfer can be automatic or you will have to use the Cookie API.

The behavior modification occurred since Android Oreo (API 26). Since Oreo, instant app data are automatically transferred to the installed app.

For previous versions (Nougat and lower), you must use the Cookie API.

To write data on disk, you need a PackageManagerCompat. To get one, you have to use the InstantApps class of the package com.google.android.gms.instantapps. Don’t use InstantApps from package com.google.android.gms.common.wrappers which only provides one method to check if the application is an instant app.

If you don’t have access to this class, you must add a dependency on the Play Services Instantapps library:

implementation("com.google.android.gms:play-services-instantapps:{version}")

At the time of writting this article, the last version is 17.0.0.

After that, you can get your PackageManagerCompat:

val packageManager = InstantApps.getPackageManagerCompat(context)

The method to write data needs a ByteArray as input so you must first convert your data to this type.

You can't write as much data as you want. It's depends on the device. You can get the maximum size of byte you can write:

val cookieMaxSize = packageManager.instantAppCookieMaxSize()

If your converted data size is lower than this value, you can call the method to write them on the disk.

In the next example, we just save a string:

val data = "data to pass to installed app"
val cookieData = data.toByteArray()
packageManager.setInstantAppCookie(cookieData);

Calling this method from an instant app cache data for some time after instant app has been uninstalled. There is no way to know exactly how much time data are keeped.

In the installed app code, call the following method to retrieve your data:

val cookieData = packageManager.instantAppCookie

If you need to keep, for a long time, data you retrieved, you should save them in a persistent storage because you don't know when they will be automatically deleted.

Retrieve data won't remove them from the cookie cache

When the full app is installed and you have retrieved your data, if you don't need them anymore, you have to call the setInstantAppCookie method will null parameter to reset the storage.

packageManager.setInstantAppCookie(null)

Play Store features and instant app release

Differents restrictions between internal test channel and alpha / production channel

There is no size restriction for the internal channel. You can push some app-bundles on this channel in order to do some tests but keep in mind that your app must validate the size restriction to be released to the alpha / production channel.

Only one app-bundle at a time

app bundle format has been developed to avoid to generate, from our developer side, a lot of APKs.

With just one app-bundle, we can manage:

  • density screen split
  • language split
  • ABI split.

Some time, you have to add another split kind like on Android version. So we generate several app bundles.

Unlike for an installed-app, it's not possible to release several app bundles on an instant app channel.

Be careful to version code problems between your instant app and installed app

Instant app and installed app must respect version code rule.

For a normal application, if you already have an application installed with a version code X, you can’t install another version of the application with a version code X-1. You can’t install an app with a lower version code, it should be equal or higher.

This rule must be respected for an instant app. It means it’s not possible to have an instant app with a higher version code than an installed app.

If you compile only one app bundle to publish your instant app and your installed app to do an update of the instant app, you will have to also update the installed app.

version code (instant app) <= version code (installed app)

Stop instant app publication

When you release your first instant app, whatever the channel, a new button will appear in the Android Instant Apps menu to manage the distribution of your instant app. You can stop it when you want.

Button to stop the instant app distribution on the Play Store

Filter which country can access to the instant app

The interface to whitelist countries is only accessible after the release of your instant app on the production channel. At the bottom of the production channel page, you can find the list of countries and select for which one you want to enable the instant app.

Section to select in which countries you deploy your Instant app

Only countries where your installed app is available can be enabled to distribute the instant app.

Look to instant app events on the Play Store

On the Play Store, in Statistics, for an application which has an instant app, a new report configuration is selectable.

Section in the Play Store to visualize Instant app events

In configure report, you can go the Android Instant Apps category and select one of the three metric:
- launch events: number of times an instant app is opened each day.
- launches by device: number of unique devices that launch an instant app at least once each day.
- conversion events: number of times in a day a full app is installed ona a device that previously launched the instant app.

--

--

Torcheux Frédéric
Torcheux Frédéric

Written by Torcheux Frédéric

I’m a French Android developer at MWM. I humbly try to contribute to the developer community from which I learnt everything.