Improve Your App's Memory and Performance with R8 Optimisation

Improve Your App's Memory and Performance with R8 Optimisation

Technical quality report: Release 9 (9.0)

1 action recommended:
Improve your app's memory and performance with R8 optimisation.

Google Play Console may display this recommendation when R8 optimisation is not fully enabled for an application's production release.

R8 is the Android build optimiser. It analyses the application and its dependencies, removes unused code, renames classes and methods, and rewrites parts of the program to improve efficiency.

Enabling R8 for the release build can produce a smaller application, reduce memory pressure, improve startup performance, and reduce unnecessary runtime processing.

What Does R8 Do?

R8 performs several important optimisation processes:

  • Code shrinking: Removes classes, fields and methods that are not used by the application.
  • Code optimisation: Rewrites code to reduce unnecessary operations and method-call overhead.
  • Obfuscation: Shortens class, method and field names to reduce the size of DEX files.
  • Resource shrinking: Removes unused images, layouts, strings and other application resources.
Simple analogy: R8 is similar to packing a suitcase before travelling. It removes items that are not needed, folds the remaining items efficiently and creates a smaller, lighter package.

Benefits of R8 Optimisation

Properly configured R8 optimisation may provide the following benefits:

  • Smaller APK or Android App Bundle size
  • Reduced memory usage
  • Faster application startup
  • Improved runtime and rendering performance
  • Fewer unnecessary classes and methods
  • Reduced download and installation size
  • Potential reduction in Application Not Responding events

Enable R8 in build.gradle.kts

If the application uses Kotlin DSL, open the module-level app/build.gradle.kts file and check the release build type.

android {
    buildTypes {
        release {
            isMinifyEnabled = true
            isShrinkResources = true

            proguardFiles(
                getDefaultProguardFile(
                    "proguard-android-optimize.txt"
                ),
                "proguard-rules.pro"
            )
        }
    }
}

The important settings are:

  • isMinifyEnabled = true enables R8 code shrinking, optimisation and obfuscation.
  • isShrinkResources = true removes unused Android resources.
  • proguard-android-optimize.txt applies Android's recommended default optimisation rules.
  • proguard-rules.pro contains application-specific keep rules.

Enable R8 in build.gradle

If the project uses the Groovy build format, open app/build.gradle and use the following configuration:

android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true

            proguardFiles getDefaultProguardFile(
                'proguard-android-optimize.txt'
            ), 'proguard-rules.pro'
        }
    }
}
Recommended: Enable R8 for the release build first. Enabling it for debug builds can make normal development and debugging slower and more difficult.

Configuration for Newer Android Gradle Plugin Versions

Recent Android Gradle Plugin versions provide an updated optimisation configuration. If this syntax is supported by the project's plugin version, the release configuration can use:

android {
    buildTypes {
        release {
            optimization {
                enable = true
            }
        }
    }
}

Do not change an existing working project to the newer syntax unless it is supported by the Android Gradle Plugin version used by the project. Older projects should continue using minifyEnabled and shrinkResources.

Understanding Keep Rules

R8 analyses which classes are used by the application. However, some frameworks access classes dynamically through reflection, annotations, native code or class names stored as text.

R8 may not always detect these dynamic relationships. In such cases, a specific keep rule may be required inside proguard-rules.pro.

Example: Keep One Class

-keep class com.example.myapp.models.PaymentResult {
    *;
}

Example: Keep Annotated Members

-keepclassmembers class * {
    @com.google.gson.annotations.SerializedName <fields>;
}

Keep rules depend on the libraries and architecture used by the application. Check the official documentation of each library before adding custom rules.

Avoid overly broad rules. A rule such as -keep class ** { *; } keeps almost everything and prevents R8 from delivering meaningful optimisation.

Build and Test the Optimised Release

A debug build normally does not behave exactly like an optimised release build. Therefore, the release variant must be tested before uploading it to Google Play.

Build an Android App Bundle

./gradlew bundleRelease

On Windows PowerShell, use:

.\gradlew.bat bundleRelease

The generated Android App Bundle is normally available inside:

app/build/outputs/bundle/release/

Important Functions to Test

  • Application startup
  • User login and authentication
  • Database operations
  • JSON parsing and API responses
  • Notifications and background services
  • Firebase and analytics integrations
  • Advertisements
  • Payment or purchase functions
  • Deep links
  • Camera, location and file access
  • Any feature that uses reflection or native code

If a feature works in the debug build but fails in the release build, inspect Logcat and add only the specific keep rule required by the affected class or library.

Keep the R8 Mapping File

R8 may rename classes and methods during obfuscation. Consequently, a release crash report may contain shortened names such as a.b.c().

The generated mapping.txt file allows these obfuscated names to be converted back into their original names.

The file is normally generated at:

app/build/outputs/mapping/release/mapping.txt
Important: Preserve the mapping file for every published release. A mapping file from a different release may not correctly decode the crash report.

Android's R8 Retrace tool can use the mapping file to reconstruct an original stack trace from an obfuscated crash report.

How to Verify the Improvement

  1. Record the size of the current Release 9 Android App Bundle or APK.
  2. Enable R8 and resource shrinking for the release build.
  3. Build a new signed release.
  4. Compare the size of the old and new release files.
  5. Test the optimised release on a physical Android device.
  6. Monitor memory and startup performance using Android Studio Profiler.
  7. Upload the new Android App Bundle to an internal testing track.
  8. Complete functional and regression testing.
  9. Promote the tested build to production.
  10. Monitor Android vitals and the technical quality report.

The Google Play recommendation may not disappear immediately after the new build is uploaded. Google Play may require processing time and sufficient application usage data before reassessing the release.

Conclusion

R8 optimisation is an important part of preparing an Android application for production. It removes unused code and resources, optimises program execution and reduces the size of the final application package.

For Release 9 (9.0), enabling R8 in the release build, applying precise keep rules, testing the optimised application and preserving the mapping file can improve memory usage and overall application performance.

For further technical information, refer to the official Android Developers guide to enabling R8 optimisation, the R8 keep-rules guide, and the R8 Retrace documentation.


Suggested Blogger labels: Android Development, Android Performance, R8, ProGuard, Google Play Console, Kotlin, App Optimisation

Suggested search description: Learn how to enable R8 optimisation in an Android release build to reduce application size, improve memory usage, remove unused resources and enhance runtime performance.

Improve Your App's Memory and Performance with R8 Optimisation Improve Your App's Memory and Performance with R8 Optimisation Reviewed by Admin on 11:23 PM Rating: 5

No comments:

Powered by Blogger.