Convert a feature module to an instant app

Codelab: Build Your First Instant App

Add support for Google Play Instant in an existing app.

Codelab: Build a Multi-Feature Instant App

Modularize a multi-feature app.

The guide that teaches you how to Convert an existing app to a base feature module separates your app's workflows into one or more feature modules. This guide describes how to create instant apps that link users into your app's feature modules.

By having feature modules, your app is easier to update. These modules also make it easier for you to create instant apps, where users can click a link to access functionality within a particular feature module without having to install your app.

Create a new feature module for Google Play Instant

If you're creating a feature module from scratch, you can add functionality for Google Play Instant immediately.

To create a new feature module for Google Play Instant, complete these steps:

  1. In Android Studio, select File > New > New Module...
  2. In the Create New Module window that appears, choose Instant App. Click Next.
  3. Provide a name for the new feature module. This guide calls it "my_instant_module". Click Finish.

Android Studio finishes creating and building your new instant app feature module. To continue adding instant app functionality, follow the steps in the remainder of this guide.

Add functionality for Google Play Instant in an existing feature module

After you've created a feature module that represents a particular workflow in your app, you can make the changes described in this section so that it's compatible with Google Play Instant.

Verify base feature module dependency

Make sure that the feature module includes a dependency on your app's base feature module:

my_instant_module/build.gradle

dependencies {
    implementation project(':my_base_feature_module')
}

Request the proper permissions

Instant apps can only use the following Android permissions:

  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION
  • ACCESS_NETWORK_STATE
  • BILLING
  • CAMERA
  • INSTANT_APP_FOREGROUND_SERVICE (API level 26 or higher)
  • INTERNET
  • READ_PHONE_NUMBERS (API level 26 or higher)
  • RECORD_AUDIO
  • VIBRATE

Enable "Try Now" functionality

Figure 1. The "Try Now" button that's shown for an instant app

Google Play Instant allows you to deliver your instant app to users without having them click on a URL. This experience, called "Try Now", is available in the Play Store, as shown in Figure 1.

To provide this capability in your app, you have two options:

  1. Leave your default activity as an entry point for "Try Now" experience.
  2. Add URLs to activities and declare a default URL, which becomes the entry point for the "Try Now" experience.

Use default activity

An activity that includes the following intent filter becomes the entry point:

<activity android:name="com.example...MainActivity" ... >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

Create default URL

If you provide URL access to your instant app, you must include a default URL. This URL is the one that's invoked when users click Try Now in the Google Play Store or when users pin your instant app to a launcher. Usually, the default URL corresponds to your app's main activity, found in your app's base feature module.

To add a default URL, include a <meta-data> attribute in the app module that contains your app's main activity, as shown in the following code snippet:

my_base_feature_module/manifests/AndroidManifest.xml

<activity android:name="com.example.my_base_feature_module.MainActivity" ... >
    <meta-data android:name="default-url"
               android:value="https://example.com/welcome" />
</activity>

Provide URL access to instant app

If you'd like users to be able to open your instant app outside of the Play Store, such as in a banner on your app's website, complete one of the following tasks:

  • Use the Launch API: https://play.google.com/store/apps/details?id=<package_name>&launch=true
  • Add app links to your app. Each app link is a specific URL that you own that, when clicked, loads a specific activity in your instant app.

Associate your domain with your app

The URL Mapping Editor helps you associate your app links with the respective activities that should serves as entry points into your instant app. To learn more, see Verify Android App Links.

Make sure that the app link appears in the manifest file of the feature module that launches when users click the link:

my_survey_module/manifests/AndroidManifest.xml

<activity android:name="com.example.survey_module.SurveyActivity" ... >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:scheme="http"
            android:host="example.com"
            android:pathPrefix="/survey" />
    </intent-filter>
</activity>

Send users directly to your instant app

You can send users directly to your app when they click on your app link. To do so, provide app link verification by following these steps:

  1. Declare website associations with your instant app.
  2. Add auto-verification functionality to each <intent-filter> element that corresponds to an app link:

    my_survey_module/manifests/AndroidManifest.xml

    <activity android:name="com.example.survey_module.SurveyActivity" ... >
         <intent-filter android:autoVerify="true">
             ...
         </intent-filter>
    </activity>

If you've created app links for your instant app, you can load a different feature module in one of the following ways:

  • Invoke an intent that loads a URL associated with that feature module.
  • Use the Play Core Library.

Learn more

For further information about how to create an instant app, go through the Build Your First Instant App codelab.