Sub topic 8
Shrinking, obfuscating, and keeping symbols
Assume you can run Flutter release builds and have already configured app signing. This chapter focuses on Android and iOS release size, code protection, and crash diagnosis.
What changes in a release build
A release build removes development features such as the debugger connection and extensive diagnostic checks. Flutter also compiles Dart code ahead of time. The result is faster and smaller than a debug build, but its names may be harder to understand when a crash occurs.
Two separate tools affect the result:
| Tool | What it changes | What you must keep |
|---|---|---|
| Native code shrinking | Removes unused Android code and resources | Rules for code accessed indirectly |
| Dart obfuscation | Renames Dart classes, methods, and symbols | The symbol file for each build |
Native shrinking is handled by R8 on Android. Dart obfuscation is enabled by Flutter. Obfuscation does not encrypt your application. It raises the cost of reading compiled code.
Enable Dart obfuscation
Use --obfuscate with --split-debug-info. The second option writes a symbol map outside the application artifact:
flutter build appbundle --release \
--obfuscate \
--split-debug-info=build/symbols \
--dart-define=API_BASE_URL=https://api.example.comThe command produces an Android App Bundle and symbol files in build/symbols. Keep those files in protected build storage. Do not commit them to the application repository or publish them with the app.
Every obfuscated build needs its own symbols. Match a crash report with the exact version and build number that produced it. A symbol file from another build can decode names incorrectly or fail completely.
To decode a Dart stack trace, use the Flutter symbol tool with the symbol directory:
flutter symbolize \
-i crash-stack-trace.txt \
-d build/symbols/app.android-arm64.symbolsThe generated output replaces obfuscated names with their original Dart names when the files match.
Shrink Android native code
Flutter release builds can use Android's R8 shrinker for Java, Kotlin, and Android library code. In a Flutter project, configure this in the Android app module's Gradle file. The exact syntax depends on whether the project uses Groovy or Kotlin DSL.
A Kotlin DSL configuration commonly looks like this:
// android/app/build.gradle.kts
android {
buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
}
}
}R8 follows references that it can identify. It can remove a class that your application loads by a string name, reflection, a generated registry, or a plugin callback. If a release-only crash reports a missing class or method, add a narrowly scoped rule to android/app/proguard-rules.pro, then test again. Avoid keeping every class. Broad keep rules prevent shrinking and increase the artifact size.
iOS symbols and crash reports
iOS uses different native tooling. Xcode creates a dSYM file that maps compiled native addresses to readable names. Keep the dSYM for every archived build and upload it to the crash-reporting service your team uses.
You can also pass Flutter's --obfuscate and --split-debug-info options when building an iOS release. Keep both the Flutter symbol files and the archive's dSYM files. They diagnose different parts of a crash.
Test the actual release artifact on physical devices. Check startup, deep links, notifications, plugins, reflection-based libraries, and crash reporting. Shrinking and obfuscation can expose problems that debug mode hides.
Exercise
Why must symbol files be stored separately for each release?
Show answer
Obfuscation names depend on the particular build, so only the matching symbol files can reliably decode that build's crash traces.