How to create Android instant app

1. Create a simple application by following the project creation wizard in Android Studio, File -> New -> New Project -> Empty Activity -> Next -> Finish.

2. In Android Studio, go to SKD manager -> SDK Tools, and make sure Google Play Instant Development SDK is installed.

3. Create a new module for the instant app within the existing app. File -> New -> New Module -> Instant Dynamic Feature Module -> Next, make sure the package name is the same as the base app’s package name. If the package name for the base app is com.example.instantappex, the package name for the instant app also need to be com.example.instantappex

4. Add design libraries and instant app library in dependencies block in the instant app’s app gradle. The instantapp app gradle should look like this.

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}
android {
    compileSdkVersion 30
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.instantappex"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation 'androidx.annotation:annotation:1.2.0'

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    implementation 'com.google.android.gms:play-services-instantapps:17.0.0'
}

5. Create a main activity in instant app and name it InstantMainActivity.kt with the following content.

package com.example.instantappex

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class InstantMainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

6. Copy over the res folder from base app to instant app’s directory(copy project_base_dir/app/src/main/res -> project_base_dir/instantapp/src/main/res), update the activity_main.xml file with the following. The only change is changing the hello text to ‘Hello instant app!’




    


7. Update the Manifest file in instant app’s module with the following.




    
    
        
            
                

                
            
        
    


8. Uninstall the base app if it’s installed, then select instantapp and go to Edit run configuration in Android Studio, and check the box for Deploy as Instant app under Installation options, and run the instantapp. If everything goes correctly, you should see a screen says ‘Hello instant app!’. Then if you quit the app, you won’t see app icon from your device for the instant app. It is installed or downloaded to device’s system cache, user has no direct access to launch it, and it will get deleted when device is restarted or if the system runs out of storage.

9. Make sure the base app always have a higher version code than the instant app. For a brand new app, you can set the version code for base app to 1000, and the version code for instant app to 1, and increase from there for new releases. For existing app, if the base app already has a very high version code, you might not need to worry about it as long as the instant app starts the version code at 1.

10. To release the app as instant app and make the try now button to show in play store, it has to be built as bundle aab file and make sure Instant Apps Only is selected when clicking the Create new release button in Google Play Console.

Complete example in Github

Ref: https://developer.android.com/topic/google-play-instant/getting-started/instant-enabled-app-bundle#test-from-studio

Search within Codexpedia

Custom Search

Search the entire web

Custom Search