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
- Less jank
- 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') ... } } }
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:
- Add keep rules to keep some code untouched.
- Adopt optimizations incrementally.
- Update your code to use libraries that are better suited for optimization.