ZOFTINO.COM android and web dev tutorials

Android Instant Apps Tutorial

Android instant apps feature allows you to create apps in such a way that features of the app can be used on the device without installing complete app with all the features. Similar to web pages of a website, instant apps can be accessed using URLs declared using app links. To provide instant apps feature for your app, so that user can use individual features without installing your app, you need to break your app into feature modules.

Instant app feature works on devices running Android 5.0 (API level 21) or higher and you can create instant apps using Android studio 3.0 or higher.

How Android Instant Apps Work

There are few parts, which you need to understand in order to get a good picture about instant apps, such as the way android apps are created to include instant apps, instant apps project structure, packaging, how launching a URL makes instant app available on the device and providing regular installable app with instant app.

To create instant apps, first you need to install instant app SDK in android studio after that it is the tools that help you in creating project structure for instant apps. Once instant app project is created, you can add feature modules and code. Shared code and resources should be part of base module. Each feature module should contain at least one activity which servers as entry point for the feature.

Once you are done with coding and testing, you can create and deploy the instant app by creating instant app APK which contains all the feature APKs and installable app APK.

Activities in the feature modules are associated to URLs. When user clicks a URL which is tied to a feature, play store will handle the URL and finds marching instant app and the requested feature’s APK in addition to base feature APK files will be downloaded to the device, android runs the code and entry point activity of the feature opens so that user can complete the task.

If the requested URL is not matched to any instant apps, play store will inform android system about it and the system will broadcast intent to handle the URL.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        <activity android:name=".Cashback">
            <intent-filter android:order="1">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />

                    android:scheme="https" />
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

Creating Instant App Project

You can create instant app project in android studio like you create installable app project. The only difference is that you need to select and enter few configuration information related to instant apps and its features.

Once you click file / new / new project in android studio and follow the flow as shown below.

You need to select minimum SDK, should be higher than API 21 as instant apps feature is available from API level 21 onwards. Then select the check box to include android instant app support.

android instant app project example

If you are creating the instant app project first time and instant app SDK is not installed, then android studio prompts for installation of instant app SDK components, click yes to install it. You can also install instant app SDK by going to tools / SDK manager / SDK tools and select Instant Apps Development SDK.

android instant app SDK components installation

In the next window, you need to enter feature module name. After creating the project, you can add feature modules as required. But when you create a project, you can begin with one feature module.

android instant app add feature

Then you need to enter instant app URL and finish creating instant app project.

android instant app path

Project Structure of Instant Apps

Once you created instant app project following the instruction described in the previous section, you will have a project with base feature module, feature module, app module and instant app module as shown in the picture below.

android instant apps project structure

Base feature module is the fundamental module which feature modules depend on. It contains shared resources such as layouts, drawables, values xml files, activities and fragments.

In the installable app APK file, which will be created for deploying regular app, base feature module is included as library AAR file. In the instant app APK built for deploying instant app, base feature module is included as APK file so that the base feature APK will be downloaded to the device along with the requested feature APK file, as all feature APKs depends on the base APK.

Feature module contains activities, fragments and resources specific to a feature.

Android instant app module takes all feature modules and creates instant app APK. You are not going to keep any code or resources in the Android instant app module. All the instant app module contains is the build files.

Android app module takes all the feature modules and creates app APK. App module can contains activities, fragments and resources unique to only app that means these resources will not be part of instant app.

android instant apps project structure

Creating Multiple Feature Modules

After you created instant app project, if you want to add additional feature modules, you can do so by clicking File / New / New Module and then following the follow as shown below.

In the new module window select feature module.

android instant apps multiple feature modules

Then enter module name.

android instant apps multiple feature modules add module details

In the next screen select empty activity and click next, then enter feature module path and click finish. Then add activities, fragments and resources specific to the feature in it.

Instant App Best Points

Instant apps work using app links. So it is important to add to activities the intent filters with app links definition. Since Instant app should support both http and https, add data element with scheme attribute set to http and https.

 <activity android:name=".PriceActivity">
        <action android:name="android.intent.action.VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
        <data android:scheme="http"></data>
        <data android:scheme="https"></data>
        <data android:host="www.zoftino.com"></data>

As usual, define app entry point by configuring an activity as the launcher activity. That means intent-filter element of the launcher activity should include the CATEGORY_LAUNCHER and ACTION_MAIN intents.

It is must that the app entry point activity should be able to handle default URL of your app for that you need to add meta-data element with default URL to activity definition in manifest file so that system can identify the default URL of your app and direct the default URL request to the activity.

 . . . .   
      android:value="http://www.zoftino.com" />

An Activity within the instant app can access other activities in the instant app by requesting URL address of the target activity.

Instant and installable apps need to be signed with the same certificate.

Apps that use login need to use android smart lock for password.

You can test instant apps on devices or emulator with Android 5.1 (API level 21) or higher.

Please see prepare your app for instant apps for information about things that need to be taken care of when working on instant apps.