Enable app optimization

For the best user experience, you should optimize your app to make it as small and fast as possible. Our app optimizer, called R8, streamlines your app by removing unused code and resources, rewriting code to optimize runtime performance, and more. To your users, this means:

  • Faster startup time
  • Improved rendering and runtime performance
  • Fewer ANRs

To enable app optimization, set isMinifyEnabled = true (for code optimization) and isShrinkResources = true (for resource optimization) in your release build's app-level build script as shown in the following code. We recommend that you always enable both settings. We also recommend enabling app optimization only in the final version of your app that you test before publishing—usually your release build—because the optimizations increase the build time of your project and can make debugging harder due to the way it modifies code.

Kotlin

android {
    buildTypes {
        release {

            // Enables code-related app optimization.
            isMinifyEnabled = true

            // Enables resource shrinking.
            isShrinkResources = true

            proguardFiles(
                // Default file with automatically generated optimization rules.
                getDefaultProguardFile("proguard-android-optimize.txt"),

                ...
            )
            ...
        }
    }
    ...
}

Groovy

android {
    buildTypes {
        release {

            // Enables code-related app optimization.
            minifyEnabled true

            // Enables resource shrinking.
            shrinkResources true

            // Default file with automatically generated optimization rules.
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')

            ...
        }
    }
}

Optimize resource shrinking for even smaller apps

The 8.12.0 version of Android Gradle Plugin (AGP) introduces optimized resource shrinking, which aims to integrate resource and code optimization to create even smaller and faster apps.

Enable optimized resource shrinking

To turn on the new optimized resource shrinking pipeline for a version of AGP before 9.0.0, add the following to your project's gradle.properties file:

android.r8.optimizedResourceShrinking=true

If you are using AGP 9.0.0 or a newer version, you don't need to set android.r8.optimizedResourceShrinking=true. Optimized resource shrinking is automatically applied when isShrinkResources = true is enabled in your build configuration.

Verify and configure R8 optimization settings

To enable R8 to use its full optimization capabilities, remove the following line from your project's gradle.properties file, if it exists:

android.enableR8.fullMode=false # Remove this line from your codebase.

Note that enabling app optimization makes stack traces difficult to understand, especially if R8 renames class or method names. To get stack traces that correctly correspond to your source code, see Recover the original stack trace.

If R8 is enabled, you should also create Startup Profiles for even better startup performance.

If you enable app optimization and it causes errors, here are some strategies to fix them: