R8, Shrinking & App Size Interview Questions

BUILD & TOOLING › Gradle

What does R8 do?

What a strong answer covers: In release builds, R8 shrinks (removes unused classes, methods and fields), optimises (inlining, removing dead code, merging classes), obfuscates (renames to short names) and converts to DEX. Resource shrinking removes unused resources. The result is a smaller, faster app that's harder to reverse engineer.

What are keep rules, and when do you need them?

What a strong answer covers: Keep rules (-keep in proguard-rules.pro) tell R8 not to remove or rename certain code. You need them for code used through reflection, like serialisation models, JNI, classes referenced from XML or by name, and some libraries. Many libraries ship consumer rules automatically. Overly broad rules undo the benefits of R8.

How do you read an obfuscated crash stack trace?

What a strong answer covers: R8 writes a mapping.txt file per build mapping obfuscated names back to the originals. Upload it to Play Console and your crash reporter (Crashlytics plugins do this automatically) so traces are deobfuscated, or use the retrace tool locally. Keep each release's mapping file; without it the trace is unreadable.

Your release build crashes but debug works. How would you investigate whether R8 is to blame?

What a strong answer covers: Reproduce on a release build locally, read the deobfuscated stack trace, look for ClassNotFoundException, NoSuchMethodException or serialisation fields arriving null or missing, which point to removed or renamed code used by reflection. Check usage.txt and seeds.txt from R8, use -printconfiguration to see the merged rules, add a targeted keep rule or annotation (@Keep, @SerialName or library codegen), and add a release-build smoke test to CI.

How would you reduce app size beyond turning on R8?

What a strong answer covers: Measure with the APK Analyzer: publish AABs so users get only their density, ABI and language splits; resource shrinking and removing unused languages (androidResources.localeFilters); convert images to WebP or vectors; remove unused dependencies and find heavy ones; use R8 full mode; strip native debug symbols; move rarely used features or assets to on-demand dynamic feature modules or download them; and track size in CI so it doesn't creep up.

Back to R8, Shrinking & App Size