Sub topic 8
Shrinking, obfuscating, and keeping symbols
This chapter assumes you can build a signed Android release with flutter build appbundle (covered in the earlier chapters on signing and artifacts). It also assumes you know what a stack trace is. Here you will shrink the Android part of your app, obfuscate the Dart part, and keep the symbol files that let you read crash reports afterward.
Three different techniques
People often use "shrinking", "minifying", and "obfuscating" as if they meant one thing. In a Flutter app they act on different code and are controlled by different settings.
| Technique | Applies to | What it does | Controlled by |
|---|---|---|---|
| Tree shaking | Dart code, icons, fonts | Drops code and assets that are never used | Automatic in release builds |
| R8 shrinking | Java and Kotlin code (Android) | Removes unused classes, renames them, optimizes bytecode | Gradle `minifyEnabled` and `shrinkResources` |
| Dart obfuscation | Dart code (Android and iOS) | Replaces readable class, function, and field names with short meaningless ones | `--obfuscate` and `--split-debug-info` |
Tree shaking happens without any flag. R8 is the Android tool that handles the native layer, which includes your MainActivity and any plugin code written in Java or Kotlin. Dart obfuscation only touches compiled Dart.
What obfuscation does and does not do
Obfuscation renames identifiers in the compiled Dart code. A class named PaymentService with a method chargeCard may appear as something like aB with a method cD in the binary. Someone who unpacks your app and inspects it finds much less to read.
Obfuscation has limits:
- It is not encryption. Strings, URLs, and API keys remain in the binary as plain text. Never rely on obfuscation to hide a secret.
- It slows down reverse engineering but does not stop it.
- It changes stack traces. A crash report from an obfuscated build shows the renamed identifiers, so you need the symbol files to read it.
- It does not change behavior, except for code that depends on names at runtime (see the gotchas below).
Building with obfuscation
Two flags always go together:
--obfuscateturns on identifier renaming.--split-debug-info=<directory>moves the debug information out of the app and writes it to the directory you name.
Flutter requires both. If you pass --obfuscate alone, the build fails with an error telling you to add --split-debug-info.
# Release build with Dart obfuscation and separate symbol files
flutter build appbundle --release \
--obfuscate --split-debug-info=build/symbols \
--dart-define=API_BASE_URL=https://api.example.comAfter this build, build/symbols contains one file per target architecture. On Android the names look like app.android-arm.symbols, app.android-arm64.symbols, and app.android-x64.symbols. For an iOS build the file is named like app.ios-arm64.symbols.
The same flags work for the other build commands:
flutter build apk --release --split-per-abi \
--obfuscate --split-debug-info=build/symbols
flutter build ipa --release \
--obfuscate --split-debug-info=build/symbolsSplit debug info also makes the app smaller, because the debug information no longer ships inside it. You get this size benefit even if you think you do not need obfuscation, but you then need the symbols to read stack traces.
Keeping symbols so you can read crashes
A stack trace from an obfuscated build looks like this:
Warning: This VM has been configured to produce stack traces that violate the Dart standard.
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
pid: 9102, tid: 9143, name 1.ui
build_id: '5f7a3c0d9e1b4c2a8d6e0f1a2b3c4d5e'
isolate_dso_base: 7b2c000000, vm_dso_base: 7b2c000000
#00 abs 0000007b2c3f1a24 virt 00000000003f1a24 _kDartIsolateSnapshotInstructions+0x2a124
#01 abs 0000007b2c3f1b88 virt 00000000003f1b88 _kDartIsolateSnapshotInstructions+0x2a288Nothing in that output names your code. To decode it, run flutter symbolize with the symbol file that matches the architecture and build of the crashing app:
flutter symbolize \
-i crash.txt \
-d build/symbols/app.android-arm64.symbolsHere -i is the file holding the stack trace and -d is the debug symbol file. If you omit -i, the command reads the trace from standard input, so you can pipe a trace into it.
The symbol file must come from the same build as the crashing app. A symbol file from a different build, even one from the same source code, produces wrong or empty output. That is why the build_id line in the trace matters: it identifies the exact build.
A practical archiving routine
Treat the symbols directory as a release artifact, like the keystore. Do these steps for every release you ship:
- Build with
--split-debug-infopointing at a directory that is outside the places that get cleaned.flutter cleandeletesbuild/, so copying the symbols out is safer than leaving them there. - Copy the directory to storage you control, in a folder named with the version and build number, for example
symbols/1.2.0+7/. - Do not overwrite older symbols. Users keep old versions for weeks or months.
- Match the architecture. A single App Bundle can install as arm64 on one phone and arm on another, so keep all the files.
Uploading symbols to crash reporting
Crash reporting services such as Firebase Crashlytics can decode traces for you if you upload the symbol files. Google Play Console also accepts a native debug symbols file for native crashes. The Dart symbols from --split-debug-info are not the same thing as Android native symbols, so for a Flutter app you may have two kinds of symbols to keep: the Dart ones in build/symbols, and the native ones produced by the Android build.
Check your crash service's current documentation for the exact upload command, because those tools change more often than Flutter does.
Android native shrinking with R8
For the Java and Kotlin layer, Android uses R8. In current Flutter templates, R8 is not necessarily enabled for release builds, so it is worth checking your own project. You turn it on in the release build type of android/app/build.gradle.kts:
// android/app/build.gradle.kts
android {
buildTypes {
release {
signingConfig = signingConfigs.getByName("release")
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}What each setting does:
isMinifyEnabled = trueturns on R8 code shrinking, renaming, and optimization.isShrinkResources = trueremoves resources that nothing references. It only works when minification is on.proguardFileslists rule files. The default file gives sensible Android rules, andproguard-rules.prois yours for project-specific rules.
Projects that use Groovy build.gradle write minifyEnabled true and shrinkResources true instead.
Keep rules for code found by name
R8 removes and renames anything it cannot prove is used. Code that is reached only by name, such as through reflection or from a native library, can break after shrinking. The usual symptom is a crash that happens only in release builds, often a ClassNotFoundException or NoSuchMethodError.
The fix is a keep rule in android/app/proguard-rules.pro:
# Keep a class that a plugin loads by name
-keep class com.example.myapp.LegacyBridge { *; }Most popular plugins ship their own rules, so you only add rules for your own code or for libraries that do not. The R8 output also writes a mapping.txt file that maps the renamed Java and Kotlin names back to the originals. The Android App Bundle build includes it, and Play Console uses it to deobfuscate native Java and Kotlin crashes. Keep a copy of it with your Dart symbols.
Gotchas
- Code that depends on names at runtime. Using
runtimeType.toString()orType.toString()as a key, a route name, or a database column returns obfuscated names in release builds. Use explicit string constants instead. - Logging class names. Logs that print
runtimeTypebecome unreadable. Log your own labels. - Different symbols per build. Rebuilding the same commit can give a different
build_id. Keep the symbols from the exact build you uploaded, not a later rebuild. - Profile versus release. Obfuscation applies to release builds. Test an obfuscated release build on a device before submitting, because a name-dependent bug will not appear in debug mode.
- Reading the trace by eye. Do not try to guess from offsets. Always run
flutter symbolize.
Quick checks before you ship
- The build command includes both
--obfuscateand--split-debug-info. - The symbols directory exists and holds a file for each architecture.
- The symbols are copied to storage labeled with the version and build number.
- R8 settings are confirmed in
build.gradle.kts, and a release build launches and exercises its main flows without crashing. - No secret is relying on obfuscation for protection.
Exercise
Your team ships version 1.2.0+7 built with --obfuscate --split-debug-info=build/symbols. A week later a crash trace arrives, and you find that someone ran flutter clean and later rebuilt the same commit to create 1.2.0+8. Can you use the symbols from the new build to decode a trace from 1.2.0+7? What should the release routine have done?
Show answer
No. Symbol files are tied to the exact build, identified by the build_id in the trace, and a rebuild of the same commit can produce different symbols, so decoding with the new files gives wrong or empty output. The routine should have copied build/symbols to storage labeled 1.2.0+7 right after building, before any flutter clean, and kept it for as long as that version could still be installed.
Exercise
A release build crashes at startup with a ClassNotFoundException for a class your own Kotlin code loads by name, but the debug build works. What is the likely cause and the fix?
Show answer
R8 shrinking is enabled in the release build type and removed or renamed the class because nothing references it directly. Add a keep rule for that class to android/app/proguard-rules.pro, for example -keep class com.example.myapp.LegacyBridge { *; }, then rebuild and test the release build again.
Exercise
A teammate says that turning on --obfuscate means the API key in the code is now safe. Is that correct?
Show answer
No. Obfuscation renames identifiers but leaves string values such as API keys and URLs readable in the binary. Secrets should be kept on a server or issued at runtime, not placed in the app.