Skip to content

Most visited

Recently visited

navigation

Project structure

This page describes the various modules contained within an instant app project in Android Studio version 3.0. You should already understand concepts like features, feature modules, and feature APKs. For a review of these concepts, see the Overview.

Structure of a basic instant app

The figure 1 shows the modules in an instant app project that includes only a base feature. The arrows represent build dependencies defined in the build configuration file of the modules.

Build dependencies for an instant
app

Figure 1. Instant app with single feature module.

The project shown in figure 1 can build both a regular APK and Instant App APKs. Here's a description of each module:

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.

The base feature module's AndroidManifest.xml contains manifest entries, such as <activity> or <uses-permission>, for the entire project. The <manifest> element must define a unique package name for the library.

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

Here's an example of an <application> element in the manifest:

<application>
    <activity android:name=".MainActivity">
        <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" />
            <data android:host="myfirstinstantapp.example.com"
                android:path="/"
                android:scheme="https" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The android:order attribute specifies an order for URL pattern matching, where a match to a lower value has higher priority. In situations where URL matches provided within the same instant app collide, the intent filter with the lower value takes precedence. If you do not specify the android:order attribute, the intent filter's order value defaults to zero. If multiple intent filters have equivalent order values, the system's order of matching is undefined for those intent filters.

The base feature module's build configuration file needs to apply the com.android.feature gradle plugin. The build.gradle file does not contain any instant app specific modifications. Because the feature module behaves similarly to a normal library, you can also use any third-party build plugins that require the library gradle plugin.

To learn more about creating and configuring Android library modules, read Create an Android Library. To learn more about the module-level build configuration file, go to Configure your build. To learn more configuring module dependencies, read Add build dependencies.

Android Instant Apps module

When you build your instant app, this module takes all of the features and creates Instant App APKs. It does not hold any code or resources; it contains only a build.gradle file and has the com.android.instantapp plugin applied to it. Here's an example:

apply plugin: 'com.android.instantapp'
...
dependencies {
    implementation project(':base')
    // if there additional features, they go here
    implementation project(':feature1')
}

App module (APK)

When you build your installed app, this module takes all of the feature modules and produces the APK. It can include any activities, fragments, and resources that are unique to the APK. Its AndroidManifest.xml is mostly empty because all of the elements inside the <manifest> element should be located in the base feature module. Here's an example:

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

Its build.gradle file must apply the com.android.application plugin and depends directly on the features modules. Here's an example:

apply plugin: 'com.android.application'
...
dependencies {
    implementation project(':base')
    // if there additional features, they go here
    implementation project(':feature1')
}

For a step-by-step example showing how to update an Android Studio project to build an instant app, see the Instant App Codelab.

Structure of an instant app with multiple features

To reduce the size of your apps, you must separate out the code and resources for individual features into separate feature modules. To optimize code reuse between your regular app and instant app, we recommend that you create a corresponding feature module for each feature plus a base feature module.

Each individual feature module contains the actual code and resources for a feature. Both the installed and instant apps depend on the feature modules.

Figure 2 shows the build configuration dependencies of modules within of an instant app that has two individual feature modules and a base feature module.

Build dependencies in an instant app with multiple
features

Figure 2. Instant app with two feature modules and a base feature module.

As you see in figure 2, 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 shown in figure 2—base feature, feature 1, and feature 2—have the com.android.feature plugin applied to their build configuration files.

However, the dependencies in the build.gradle files of the feature modules differ from traditional Android library modules. The two individual feature modules use the implementation project expression within their dependencies block, as shown in the following code snippet:

apply plugin: 'com.android.feature'

android {
    ...
}

dependencies {
    implementation project(':base')
    ...
}

The base feature module uses the feature project expression in its build.gradle file rather than the implementation project expression. The base feature module must also have the expression baseFeature true in its android block.

The base feature module also uses the application project expression in its list of dependencies, referencing the APK module. This adds a dependency from the base feature module to the APK module. By setting this dependency, the app's applicationId for each flavor is appplied to the base feature module, which in turn propagates to all of the features in the project. This expression ensures that the installable APK and Instant App APKs have the same applicationId. If this is not set, the applicationId defaults back to the one defined in the base feature module manifest.

The following code snippet shows the manifest of a base feature with the required expressions:

apply plugin: 'com.android.feature'

android {
    baseFeature true
    ...
}

dependencies {
    feature project(':feature1')
    feature project(':feature2')
    application project(':app')
    ...
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.