Android Instant Apps Tutorial

Filed Under: Android

In this tutorial, we’ll be discussing what are Instant Apps in Android and how to implement them in our android application.

Android Instant Apps

It often happens on the web that when we open an URL link, it asks us to download the application. This can hamper the User Experience.

Hence google has come up with Instant Apps which are a mini version of the full application and can be launched from url without downloading the application entirely.

In Google Playstore, applications with instant apps feature would have a TRY NOW button along with the install button.

Instant Apps are currently supported for devices running Android 5.0 and above.

Instant Apps require the developers to modularise their project such that it would contain different screens/resources in different modules.

An instant app project largely consists of the following modules:

  • app module
  • instantapp module
  • base feature module

app module

app module contains everything that must not be there in the instant app. Having said that, the applicationId would be defined in the base feature module only.

base feature module

This module consists of the shared activities that would be used in the instant app and app module.

The applicationId of the application resides here. The main AndroidManifest.xml file is defined here too with all the activities, permissions etc.

This module would hold all the resources for the application – drawable, mipmap icons etc.

In the build.gradle file of this module, we set the plugin to com.android.feature as well as set baseFeature true inside the android block.

feature module

This contains activities and classes for a specific feature of the instant app. By default, an instant app when loaded would download only one feature and one base feature module along with the instant app.

You can create multiple features as per your requirements

instantapp module

This cannot contain any code. In the build.gradle file of this module, we add the dependencies for the base and feature modules.

Following is a diagram depicting how each of these modules is related.

android instant app module dependencies

The app module takes all the feature modules in aar format when the APK is built.

Where do the URL link to launch certain activities reside?

Thanks to App Links Assistant, we can create url links mapped to every activity. Once it is created, an intent filter with the url data is set inside the AndroidManifest.xml file.

Once an application is uploaded to the playstore, which url is launched first?

We need to set a default-url in our manifest. On whichever activity it is set, that gets launched first.

Once an application on playstore is instant app compatible, we can launch it using the link:
https://play.google.com/store/apps/details?id={package_name}&launch=true.

Pre-requisites for developing instant apps:

  • Android SDK Build-Tools 26.x or higher
  • Android SDK Platform Tools 25.x or higher
  • Android SDK Tools (latest)
  • Instant Apps Development SDK (latest)
  • Support Repository (latest)
  • For emulators, Google Play Image must be installed
  • You need to implement Smart Lock for instant apps that require user login.

Enough talk. Let’s code!

Let’s create our first AndroidInstantApps application in Android Studio.

Getting Started with Android Instant Apps

android instant apps project wizard

On the next screen set the name of a feature module:

android instant apps android studio project

In the following screen, select the activity template.

We’ve used the Login Activity template.

On the next screen, it’ll ask for the app link for the first activity.

Following is the random url path that we’ve set:

android instant apps default url

Once this is done, we have our project structure ready as shown below:

android studio instant apps project structure

Once you navigate through the folders, you’ll see that the activity gets created in our feature module. The manifest file resides in the same place:

android instant apps feature manifest

Now to run the application we need to set a default-url in the manifest.

We have two ways to do so:

Set the meta-data tag with the default url in the manifest itself.


<meta-data android:name="default-url"
        android:value="https://www.journaldev.com/preview" />

Other way, we can set the url of the activity to be launched from the Edit Configurations.

android instant app edit config

The output of the above application is:

android instant app output

As you can see from the recent tabs screenshot, the app icon have the instant app badge on it.

Clicking the info button would show the following screen:

android instant app output app info

Clicking install would install the full application.

Up until now, we haven’t added anything into the app module.

Let’s create an activity in the app module and try running the instant app and the app modules separately.

Creating Activities in the App Module

Right click on main in the app module and create a new directory named java.

Create a new package with the name: com.journaldev.androidinstantapps.app inside the java folder.

Inside the package create an activity by right-clicking and then New | Activity | Settings Activity.

Following is how the project structure looks:

android instant app project structure detailed

Since this is an Instant App project we need to add an URL for activities in the app module too.

This is because Instant Apps navigate between activities within the same and different modules through urls.

So goto Tools | App Links Assistant | Open URL Mapping Editor. Over there add a new url mapper for the SettingsActivity.

android instant app project app links

app/main/AndroidManifest.xml

android instant app manifest main

Now goto The LoginActivity.java class in the feature module and replace the following code:


mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://journaldev.com/settings"));
                intent.setPackage(getPackageName());
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                startActivity(intent);

                //remove the below line and add the above ones.
                //attemptLogin(); 
            }
        });

So in the Intent, we pass in the link for the SettingsActivity that was created in the app module.

This way you can switch between activities in different modules.

Now run the application in the app module configuration.

android instant app change configuration

Now the output of the application in action is given below:

android instant app final output

If you run the above code, but as an instant app, it will crash.

WHY?

As told at the beginning of this tutorial, an instant app cannot access the app module.

This brings an end to this tutorial. We’ll see how to refactor an existing project to use instant app feature in a later tutorial. You can download the project from the link below:

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages