This example demonstrates how to build and optimize a simple Android application using R8.
R8 is the code shrinker, optimizer, and dexer for Android developed by Google. In a single step, R8 handles:
shrink_resources = True, removes unused Android resources that are no longer referenced after code shrinking.Using R8 significantly reduces APK/AAB download and install sizes and memory footprint, decreases app startup time, and keeps method counts within optimal limits.
For in-depth details on configuring ProGuard/R8 keep rules and optimization flags, see docs/r8-optimization.md.
Ensure that the ANDROID_HOME environment variable is set to the path of your Android SDK:
export ANDROID_HOME=/path/to/android/sdk
android_binary)To build the standalone optimized APK:
bazel build //:r8_app
Output: bazel-bin/r8_app.apk
android_application)To build the optimized Android App Bundle (AAB):
bazel build //:r8_app_bundle
Output: bazel-bin/r8_app_bundle_unsigned.aab
To build and install the application directly onto a connected Android device or running emulator:
bazel mobile-install //:r8_app
BUILD: Defines the android_binary (r8_app) and android_application (r8_app_bundle) targets with proguard_specs and shrink_resources = True.proguard-rules.pro: App-specific keep rules for preserving entry points like MainActivity.proguard-android-optimize.txt: Default Android optimization rules for R8..bazelrc: Flags and settings needed to build the app with rules_android.