Android Instant Apps allows Android users to run your apps instantly, without installation. Users can get your flagship Android experience from any URL—including search, social media, messaging, and other deep links—without needing to install your app first. Android Instant Apps supports the latest Android devices from Android 6.0 through Android O. Google will be rolling out to more devices soon, including expanding support to Android 5.0 (API level 21) devices shortly.

How does Instant app work?


When Google Play receives a request for a URL that matches an instant app, it sends the necessary code files to the Android device that sent the request. The device then runs the app. How does Instant app work

Structure of the Instant App


  • Base feature module: The fundamental module of your instant app is the base feature module. All other feature modules must depend on the base feature module. The base feature module contains shared resources, such as activities, fragments, and layout files. When built into an instant app, this module builds a feature APK. When built into an installed app, the base feature module produces an AAR file.
  • Features: At a very basic level, apps have at least one feature or thing that they do: find a location on a map, send an email, or read the daily news as examples. Many apps provide multiple features.
  • Feature ModulesTo provide this on-demand downloading of features, you need to break up your app into smaller modules and refactor them into feature modules.
  • Feature APKs: Each feature APK is built from a feature module in your project and can be downloaded on demand by the user and launched as an instant app.

Each feature within the instant app should have at least one Activity that acts as the entry-point for that feature. An entry-point activity hosts the UI for the feature and defines the overall user flow. When users launch the feature on their device, the entry-point activity is what they see first. A feature can have more than one entry-point activity, but it only needs one. Structure of Instant App   As you see in the figure, both “Feature 1” and “Feature 2” depend on the base feature module. In turn, both the instant and installed app modules depend on the feature 1 and feature 2 modules. All three feature modules are shown in figure -base feature, feature 1, and feature 2—have the com.android.feature plugin applied to their build configuration files.

Upgrade Your Existing App

Android Instant Apps functionality is an upgrade to your existing Android app, not a new, separate app. It’s the same Android APIs, the same project, and the same source code. Android Studio provides the tools you need to modularize your app so that users load only the portion of the instant app that they need when they need it.

Step 1: Develop a use case for your instant App


Focus on a core user experience that completes a specific action and optimizes a key business metric besides app installs. Then review the user experience guidelines for Android Instant Apps.

Step 2: Set up your development Environment


To develop an instant app, you need the following:

    • Android SDK Build Tools 26+
    • Android SDK Tools 25+
    • Android SDK Platform O
    • Latest Android Support Library
  • Latest Android Repository

Install Instant App SDK

Build an Android Instant App, we need to install the SDK. Go to Tools >Android > SDK Manager. Click on the “SDK Tools” tab and install “Instant Apps Development SDK” by checking the box and hitting “Apply” Install Instant App SDK

Set up your device or emulator

You can develop instant apps on the following devices and emulators:

  • Devices: Nexus 5X, Nexus 6P, Pixel, Pixel XL, Galaxy S7 running Android 6.0 or higher.
  • Emulator: Nexus 5X image running Android 6.0 (API level 23), x86, with Google APIs(You cannot use x86_64 architectures).

Step 3: Moving existing code into a feature module


In this step, we will convert the existing application module into a shareable feature module. We will then create a minimal application module that has a dependency on the newly formed feature. Note that this feature module will be included into the Instant App build targets later. 

Convert the app module into a feature module called app-base

We start with renaming the module from 'app' to 'app-base': create base module

Change Module type

Next, we change the module type to Feature module by changing the plugin type from com.android.application to com.android.feature  and also remove applicationId   because this is no longer an application module in the app-base/build.gradle file:

// replace apply plugin: 'com.android.application'
// with
apply plugin: 'com.android.feature'
// remove application id
applicationId "com.google.samples.apps.topeka"

Specify base feature in the project  app-base/build.gradle

android {
    ...
    baseFeature = true
    ...
}

Synchronize gradle files and re-build the project with Build->Rebuild Project.

Step 4: Create appapk module to build APK file


Now that we have transformed our source code into a reusable library module, we can create a minimal application module that will create the APK. From File->New Module Create New Module Enter application name “app apk”, leave suggested module name (topekaapk). If your project uses Data Binding, you need to ensure that appaapk/build.gradle includes the following in the android { ... } section.

android {
    ...
    dataBinding {
        enabled true
    }
}

Replace compile dependencies in appaapk/build.gradle:

dependencies {
    implementation project(':app-base')
}

Switch to “Project” view and remove unused files:Instant app remove unused folder Switch back to “Android view” and remove the application element from appaapk/src/main/AndroidManifest.xml. It should only contain this single manifest element.

<manifest package="com.foobar"
    xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>

Finally sync Gradle files, re-build and run the project. The application should behave exactly the same despite all of our changes. Create Feature module We have just moved the application’s core functionality into a shareable feature module and we are now ready to start adding in the Instant App modules.

Step 5: Creating the instant app APK


Instant Apps uses feature APKs to break up an app into smaller, feature focused modules. One way to look at Instant Apps is a collection of these feature APKs. When the user launches a URL, Instant Apps will only deliver the necessary feature APKs to provide the functionality for that URL. The app-base module feature APK that encompasses the full functionality of our app. We will create an Instant App module that bundles our single feature APK. At the end, we are going to have our single feature instant app! single feature instant app The Instant App module is merely a wrapper for all the feature modules in your project. It should not contain any code or resources.

Create an Instant App module

Select File -> New -> New Module Create Instant App Next, we need to update the instant app gradle file to depend on the base feature module. At this point we also need to add buildToolsVersion to explicitly use 26.0.1 since there is an issue in the current Android preview which makes the default 26.0.0. Since we’ve installed 26.0.1 with the project we don’t want to fetch 26.0.0 necessarily. instantapp/build.gradle

android {
    buildToolsVersion "26.0.1"
}
dependencies {
    implementation project(':app-base')
}

The instant app does not hold any code or resources.It contains only a build.gradle file. Now do a clean rebuild: Build -> Rebuild project.

Step 6: Defining App Links


App links are required in instant apps because URLS are the only way a user can launch an instant app (instant apps are not installed).Each entry-point activity in an instant app needs to be addressable: it needs to correspond to a unique URL address. If the URL addresses for the features in an instant app share a domain, each feature needs to correspond to a different path within that domain. In order to enable Instant App runtime to call your application, we must establish the relationship between your web site and the app. To associate links, we will use a new feature built into Android Studio called “App Links Assistant.”. Invoke this tool through the Android Studio “Tools” menu: App Links Assistant From the sidebar, tap on the “Open URL Mapping Editor” button: add url intent filters From the editor, tap the “+” button to add a new app link entry: Create a new URL mapping with the following details: Host: http://topeka.samples.androidinstantapps.com Path: pathPattern/signin Activity: activity.SigninActivity (app-base) map url to activity Repeat this dialog for https variation, as well as other links: In the end, you should have 4 mappings like this: Url to Activity mapping Sync gradle files if required and rebuild the project. Again, since we have not used Android Studio wizard, the run configuration for instant app is not valid, we need to define the URL before we can launch the instant app from the IDE. Click the Run configuration dropdown and choose “Edit Configurations…” Select instantapp under Android App. Edit Configuration Replace the text ‘<< ERROR – NO URL SET>>’ with https://topeka.samples.androidinstantapps.com/signin To run your Instant App, select instantapp from the Run configuration dropdown and click Run: Run Instant APP   Now, you have created and deployed an Android Instant App. You took an existing Android application and restructured it to build both a full installed APK and an Instant APK that will be loaded when the user taps on the associated URLs.

Conclusion


You may need to separate the code in multiple features for various reasons, the most obvious one is feature separation, to let users download only the relevant portions of the app.We will create another feature module and move all UI code there (activities and related fragments). It will let us create two features ( You will create another feature module and move all UI code there (activities and related fragments). It will create two features (app-base and app-ui) later.

References


1.https://codelabs.developers.google.com/codelabs/android-instant-apps

2.https://developer.android.com/topic/instant-apps/index.html

3.https://www.youtube.com/watch?v=oispNrpGnIY

4.https://www.youtube.com/watch?v=9Jg1D07NgeI