Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

Reduce the size of your instant app

Google Play Instant provides rich, native experiences at the tap of a web link. People can experience your app without upfront installation, enabling a higher level and quality of engagement. To make an instant app load as quickly as a typical mobile webpage does, though, you need to create a well-structured, efficient instant app. The smaller your instant app's binary, the faster it loads and the smoother the user experience is.

This document conveys best practices for managing your app's structure and binary size to enable a smooth instant app experience. You can apply these same practices to benefit your installable app, too.

Refactor into multiple feature modules

The largest improvement to your app's binary size occurs when you refactor the app into multiple feature modules. Start with a base feature module, then extract thematically-related workflows into their own feature modules. Assign a starting activity and unique URL to each feature module so that users can complete the module's workflow successfully.

As you create feature modules, keep the base feature module as small as possible. In particular, pay close attention to the parts of your app that require access to your dependent libraries. If only one feature module uses a given library, import that library in the feature module itself, not the base feature module. Keep in mind that, in order to release an instant app for a particular feature module, the total size of that feature module and the base feature module must be less than 4 MB.

Best practices

When refactoring your app, keep the following best practices in mind:

Use the same codebase for both app types
You can simplify your app's project management process by using the same modular codebase to create both your installed app and your instant apps.
Design for multiple feature modules
Even if your app has only one workflow and requires only a single feature module for now, it's still a good idea to design for multiple feature modules. That way, you can add existing modules to your app without affecting the original feature module's size.
Don't focus on the feature module size limit at the beginning
Feature module size limits don't apply to locally-built binaries. You can also release an instant app through the internal test track, which enforces a 10 MB limit on feature module sizes. Only the alpha and production tracks enforce the 4 MB limit.

Update app resources

Some apps, particularly those that have longer codebase histories, contain resources that your app's binaries no longer use. As you look for ways to make your app's modules smaller, consider the following common sources of unneeded code.

Reduce file size of images

You can significantly reduce the total size of your app's drawables by using the WebP file format instead of PNG. Google Play Instant provides complete support for WebP, including transparency and lossless compression, so image quality remains the same.

If possible, remove all backward compatibility requirements for using other PNG images. If you must use PNG images, place them in the module that's used to build and install your app.

Remove extra languages

If your app supports multiple languages, reduce as many localized resources as you can. This step is particularly useful to complete if you use an "app compat" library, such as android.support.v7.appcompat. This library includes messages in many language, some of which your app might not support.

To learn more, check out how to remove unused alternative resources, particularly unused languages.

Remove extra files

Your app might no longer use some of the resources that you've imported into your project. To help remove these resources, Android Studio has a Lint check for this specific situation. To use the tool, complete the following steps:

  1. Press Control+Alt+Shift+I (Command+Alt+Shift+I on Mac OS).
  2. In the dialog that appears, type "unused resources".
  3. Select the Unused resources option to start the resource usage inspection process.

If any large resources remain in your app, consider whether it's possible to unpackage them from your app and download them as standalone files after the user starts interacting with your app. This sort of image-loading deferral usually requires a code change, but it can substantially reduce your instant app's file size by downloading only the resources that a user explicitly requests.

Remove unused libraries

As an app grows in scope, it can take on a surprising number of dependencies, particularly one of the following types:

  • Native libraries: Libraries that contain native code that your instant app never runs.
  • Transitive dependencies: Libraries upon which your app's imported libraries depend.

Android Studio has several useful tools for identifying any extraneous dependencies in your app's project:

External libraries

Android Studio's Project view includes an External Libraries section.

This section contains every library that your app uses, including native code and all transitive dependencies. In this view, look for unused or dupicate libraries that your app doesn't require.

APK Analyzer

You can use the APK Analyzer tool to compare different builds, including instant app builds.

After you've determined which libraries your app doesn't need, exclude them by adding lines similar to the following to your Gradle build file:

<feature_module>/build.gradle

dependencies {
    implementation('some-important-but-large-library') {
        exclude group: 'com.example.imgtools', module: 'native'
    }
}

For more information on reducing the total import size of your app's dependencies, see Gradle's guide to Dependency Management.