R8 Keep Radius

The “keep radius” of a keep rule refers to the negative impact that the rule has on the final application.

R8 provides a tool to analyze and visualize this keep radius. This helps developers:

  • Identify expensive rules: Find keep rules that prevent R8 from optimizing large parts of the application.
  • Debug dependencies: See which libraries introduce broad keep rules.
  • Remove redundancy: Discover rules that are not needed because they are covered by other rules.

Note: This feature is currently experimental and requires R8 version 9.2.14-dev or higher.

Generating the R8 Configuration Analysis Report

R8 supports outputting a Configuration Analysis Report as an HTML file.

Gradle

To generate the HTML report from a Gradle build, pass the com.android.tools.r8.dumpkeepradiushtmltodirectory system property.

See Replacing R8 in AGP for instructions on how to update the R8 version in your build.

./gradlew assembleRelease \
    --no-daemon \
    -Dcom.android.tools.r8.dumpkeepradiushtmltodirectory=<output_directory>

Example:

./gradlew assembleRelease \
    --no-daemon \
    -Dcom.android.tools.r8.dumpkeepradiushtmltodirectory=/tmp/blastradius

Android Platform

For builds within the Android Platform, set the R8_DUMP_BLAST_RADIUS environment variable to true.

R8_DUMP_BLAST_RADIUS=true m

This will generate an r8blastradius.pb file for each R8 build target within out/soong/.intermediates.

Visualizing the Data

The raw .pb file can be converted into an interactive HTML report using the BlastRadiusHtmlReportGenerator.

Converting a single file

Use the following command to convert a specific .pb file to HTML:

java -cp r8.jar com.android.tools.r8.blastradius.BlastRadiusHtmlReportGenerator \
  <path_to_input_pb> <path_to_output_html>

Example:

java -cp r8.jar com.android.tools.r8.blastradius.BlastRadiusHtmlReportGenerator \
  /tmp/blastradius/blastradius.pb /tmp/blastradius/report.html

Converting multiple files (Android Platform)

If you have multiple blast radius files (e.g., from an Android Platform build), you can generate a summary report for the directory:

java -cp prebuilts/r8/r8.jar com.android.tools.r8.blastradius.BlastRadiusHtmlReportGenerator \
  <path_to_input_directory> <path_to_output_directory>

Example:

java -cp prebuilts/r8/r8.jar com.android.tools.r8.blastradius.BlastRadiusHtmlReportGenerator \
  out/soong/.intermediates /tmp/blastradiusreport

This will scan the input directory for .pb files and generate an HTML report in the output directory.