Deliver an instant-app experience without installing one on your phone
Photo by freestocks on Unsplash
Takeaway From the Article
In this article, you?re going to learn how to create instant apps for your Android users. Along with that, you?ll also learn how to prompt the user to install the full version of the app, a proper way of testing instant apps, how to use instant apps with URLs, and more. Read on to learn about them.
Instant apps are a technology that provides the native experience of Android apps without explicit installation.
When your Android app has the instant-app feature, then users will see the ?try now? button beside ?install? on your app details screen in the Google Play store. By clicking on the ?try now? button, a virtual app is installed.
Only the modules that developers declare as an instant app in their manifest file will be downloaded at this moment. This allows users to experience the app briefly before installing the entire application. If your app size is large, this option will give a peek into the features of your app without actually downloading it.
Things to Keep in Mind
Now we know what an instant app is and how it can be helpful to users and business owners. As many benefits as it provides, it also comes with some restrictions.
- Instant-app enabled app bundles can only use few app permissions, such as ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION, ACCESS_NETWORK_STATE, CAMERA, INSTANT_APP_FOREGROUND_SERVICE [only in Android 8.0 (API level 26) and higher], INTERNET, READ_PHONE_NUMBER, RECORD_AUDIO, VIBRATE, and WAKE_LOCK.
- By default, instant apps can?t interact with the installed app unless one of the following conditions are satisfied:a.) At least one of the activities in the installed app should have the attribute android:visibleToInstantApps in its manifest file marked as true.b.) The installed app contains a CATEGORY_BROWSABLE intent filter.
- The instant-app feature is built on top of Android app bundles using a split APKs feature that was introduced in Android Lollipop. So developers need to change their app distribution to Android app bBundles from the traditional APK to work with instant apps.
Include Google Play Instant API in your app by adding the following line under the dependency node in the app-level build.gradle file.
If you?re using any previous instant-app plugins, you can remove them. The new instant API doesn?t need any plugins, and with this new API, you can use one code for both instant and full versions of the app.
Enable the Instant Experience
The version code of the instant experience of your app should be less than the actual version code of the app. This is because when users move from the instant app to the full-app installation, Google Play considers it as an app update. So the ascending order of the version code makes sense.
Even if the users already installed the application, they experience instant apps through URLs, which you?ll see in a bit. But before that, you should be careful with version code ? at any instance, the instant app should have less version code.
Google?s recommendation is to increase the version code of the installable APK by a large number, such as 1,000, to ensure there?s enough space for your instant-experience?s version number to increase.
As I said, instant apps won?t install in the device. Instead, they?ll be operated from a sandbox storage. For this purpose, we need to include targetSandboxVersion attribute in the manifest file, as shown below:
<manifest xmlns:android=”http://schemas.android.com/apk/res/android” … android:targetSandboxVersion=”2″ …>
Enable instant-experience at the module level
To make a module instant-app support, we just need to add the following line in the manifest file, as shown below. This is mandatory in the base module and in the modules that you intended to include in the instant-app experience.
<manifest xmlns:dist=”http://schemas.android.com/apk/distribution”> <dist:module dist:instant=”true” …/> …</manifest>
Dynamic modules with instant apps
We can also use dynamic feature modules in instant apps. The easiest way to create a dynamic feature module that supports instant apps is to make use of the module templates from Android Studio.
To create an instant dynamic feature module, navigate as so:
File > New > New Module
Then, a new window is opened with different templates related to different configurations. In the list, select the instant dynamic feature module option, and click next. After that, you can give a name to the module, and you can give apackage name as well.
What if you want to provide an instant-app experience when an URL is loaded into the mobile. It?s possible, but you should implement the following things to make it work:
- First, you should add a second intent filter by setting autoVerify to true.<intent-filter android:autoVerify=?true?>
- Then, add the following category and action flags under the above-created intent filter.<action android:name=”android.intent.action.VIEW” /><category android:name=”android.intent.category.BROWSABLE” /><category android:name=”android.intent.category.DEFAULT” />
- Finally, we need to add the URL schemas, as shown below:
<data android:scheme=”http”/><data android:scheme=”https”/><data android:host=”example.com”/><data android:path=”/instantapp/schedule”/>
We can also specify a default URL that?s used to launch the instant experience more easily, as shown below:
<meta-data android:name=”default-url” android:value=”https://example.com/instantapp/experience”/>
This is not much to do, but it?ll be helpful when you want to show a demo to someone on the fly. This is optional.
Request Users to Install the Full Version
Now that we?ve configured the instant experience, the next step would be to encourage the user to install the actual app. To do this, first we need to confirm that the user is in the instant experience. We can determine that by using the isInstantApp() method. This method returns true if the running process is in an instant experience.
The next step is to show a prompt to the user that encourages them to install the actual app. This can be done as shown below:
No matter what you do, testing is one of the core things to make it stable. Instant apps can be tested directly by installing from the Android Studio or downloading from the Play store using the internal testing feature.
Test in the local device
To install directly from Android Studio, execute the following steps:
- First, uninstall if the app is already installed in the device.
- Then, navigate to the general tab on installation options. Select ?deploy as instant app.?
- Then click the run button.
Internal test track
If you?re unsure how the instant app works when it?s downloaded from the Play store, then it?s an excellent time to test it using an internal test track.
- First, generate an Android app bundle as usual.
- Upload it under the internal test track instead of under production in release management.
- Add your tester?s mail IDs to the testing list.
- Now log in with one of those emails. When you search for the app in the Play store, you?ll see ?try now? option.
To learn more about dynamic delivery and Android app bundles, read the following articles:
- ?Dynamic Delivery in Android ? Part 1?
- ?Dynamic Delivery in Android ? Part 2?
- ?Dynamic Delivery in Android ? Part 3?
I hope you learned something useful ? thanks for reading.