Sub topic 7
Obfuscation and split debug info
This chapter assumes you can build a release APK or app bundle with the flutter tool and read a Dart stack trace. You should also have used --analyze-size from the earlier chapter on measuring size, because you will use it to check the effect of each flag here.
Two flags are involved. --split-debug-info removes debugging data from the compiled app and saves it in separate files. --obfuscate replaces readable Dart names with short meaningless ones. They solve different problems, but they are used together, and both change how you read crash reports.
What the flags do
When Flutter compiles your Dart code ahead of time (AOT) for a release build, the output contains two kinds of information:
- Machine code and data the app needs to run.
- Debug information that maps machine addresses back to Dart function names, files and line numbers. This is what turns a crash into a readable stack trace.
Users never need the debug information. --split-debug-info=<directory> tells the compiler to write it to files in that directory and leave it out of the shipped binary. The result is a smaller app, and the symbol files stay with you.
--obfuscate renames the classes, methods, fields and top-level functions in your compiled Dart code. A class called PaymentSession might appear as aB in the binary. This makes reverse engineering harder. It also makes names in crash reports unreadable unless you translate them back.
| Flag | What it changes | Effect on size | Effect on stack traces |
|---|---|---|---|
| `--split-debug-info=<dir>` | Moves debug info out of the binary into `.symbols` files | Smaller binary | Frames show addresses, not names |
| `--obfuscate` | Renames Dart identifiers in the compiled code | Small, because names are shorter | Names that do resolve are scrambled |
| Both together | Both of the above | Smaller binary | Needs the `.symbols` file to read |
--obfuscate requires --split-debug-info. Flutter refuses to build with --obfuscate alone, because obfuscated names can only be recovered from the symbol files.
Correction: the diagram must be a proper fence; here it is.
Building with the flags
The commands below build an app bundle and an APK with symbol splitting and obfuscation.
flutter build appbundle --obfuscate --split-debug-info=build/symbols
flutter build apk --split-per-abi --obfuscate --split-debug-info=build/symbolsOn iOS the flags work the same way:
flutter build ipa --obfuscate --split-debug-info=build/symbolsYou can use --split-debug-info on its own when you want the size saving but do not need obfuscation:
flutter build apk --split-debug-info=build/symbolsAfter the build, look in the directory you named. You will find one file per target architecture, for example app.android-arm64.symbols and app.android-arm.symbols. A --split-per-abi build or an app bundle produces files for each ABI it includes.
Only release and profile AOT builds are affected
These flags apply to AOT compilation. A debug build uses the Dart VM's just-in-time (JIT) mode, so it keeps full names and information, and the flags have no useful meaning there. As the first chapter explained, you should measure size on release builds, and the same applies here. Do your before-and-after comparison with --analyze-size on release builds only.
flutter build apk --analyze-size --target-platform android-arm64 \
--split-debug-info=build/symbolsRun it once without --split-debug-info and once with it, and compare the totals. The debug info removed by splitting is usually the larger saving. Renaming identifiers adds a smaller one, so do not expect obfuscation alone to shrink the app much. Your own numbers are the only reliable guide, so check them.
Reading an unsymbolized stack trace
With split debug info, a crash trace no longer contains function names. It looks like this:
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
pid: 12345, tid: 12378, name 1.ui
os: android arch: arm64 comp: no sim: no
build_id: '5346e01103ffeed44e97094ff7bfcc19'
isolate_dso_base: 723d447000, vm_dso_base: 723d447000
isolate_instructions: 723d452000, vm_instructions: 723d449000
#00 abs 0000723d6346d7a7 virt 00000000001d67a7 _kDartIsolateSnapshotInstructions+0x1e5
#01 abs 0000723d637d8f4b virt 00000000001d3f4b _kDartIsolateSnapshotInstructions+0x1b4Two details matter. The build_id identifies the exact build that produced the trace. The frames use offsets into the snapshot rather than names.
To translate a trace, save it to a file and run flutter symbolize with the matching symbols file:
flutter symbolize -i crash_trace.txt -d build/symbols/app.android-arm64.symbolsThe tool prints the same frames with Dart function names, file paths and line numbers. If you obfuscated the build, it also restores the original identifier names. You can also pipe a trace in through standard input if you omit -i.
The -d argument can be a single symbols file or a directory. Use the file for the architecture the user's device ran. The trace header's arch: line tells you which one that is.
Keeping symbols so you can use them later
The symbols file is the only way to read traces from a given build. Treat it as a build artifact that you must archive, in the same way as a signing key's backup.
- Store the
build/symbolsdirectory for every build you release, labeled with the app version and build number. - Do not rely on rebuilding later. A rebuild with a different compiler, dependency version or source commit can produce different addresses and different obfuscated names. The
build_idwill not match. - Do not put the symbols inside the app or in a public location. They undo the obfuscation.
- Upload the files to your crash reporting service if it supports Dart symbols. Services such as Firebase Crashlytics and Sentry have upload steps that read these files, so they can symbolize traces for you. Follow your service's current documentation for the exact command.
A build that passes --obfuscate but whose symbols were discarded produces crash reports that no one can decode. Add an automated check to your release process that fails if the symbols directory is empty.
What obfuscation does not protect
Obfuscation is a deterrent, not a security boundary. Know its limits before you rely on it.
- It renames identifiers only. String literals stay readable. An API key or URL in your Dart code can still be extracted from the binary.
- Assets are not touched. Images, JSON files and fonts are shipped as they are.
- Native code is separate. Kotlin, Java, Swift and Objective-C code is not obfuscated by this flag. On Android, minification for Java and Kotlin is configured through Gradle and R8, so check that setting in your project.
- The program logic is still there. A determined person can read control flow and call patterns even with scrambled names.
For secrets, the correct approach is to keep them on a server and not in the app at all.
Code that breaks under obfuscation
Obfuscation changes the text produced by runtimeType.toString(). Any code that treats a class name as data will behave differently in a release build than in debug. That is a bug that your debug testing will not reveal.
The program below shows the difference. Run it with plain dart run and it prints the real class name. In an obfuscated Flutter release build, the first line would print a short scrambled name instead.
abstract class Shape {
String get kind;
}
class Circle implements Shape {
@override
String get kind => 'circle';
}
void main() {
final shape = Circle();
// Unsafe as a stored or compared value: changes when obfuscated.
print(shape.runtimeType.toString());
// Safe: an explicit string you control.
print(shape.kind);
}Typical places this causes trouble:
- Using
runtimeType.toString()as a key in a map, a cache or a database column. - Sending a class name to your analytics or logging backend and then grouping on it.
- Building route names from class names.
- Comparing
toString()output of a type in a test or an assertion that also runs in release mode.
The fix is the same each time: give the concept an explicit string, constant or enum of your own. Code that uses is checks, generics and normal method calls is not affected, because the compiler renames every use consistently.
Always test an obfuscated release build on a real device before you ship. A build that passes in debug mode may behave differently once names change.
A release checklist for these flags
- Build with
--split-debug-info=<unique directory>, and add--obfuscateif you want it. - Compare the
--analyze-sizetotal against a build without the flags. - Archive the symbols directory with the version number.
- Install the release build on a device, force a test crash and symbolize the trace to confirm the process works before real users depend on it.
- Search your code for
runtimeType.toString()and similar uses of type names as data.
Exercise
Your team ships version 2.3.0 built with --obfuscate --split-debug-info=build/symbols. A week later a crash report arrives with frames like _kDartIsolateSnapshotInstructions+0x1e5. A teammate rebuilds the app from the same source commit and runs flutter symbolize with the new build/symbols files, but the output is wrong. Give two reasons this can happen, and say what should have been done at release time.
Show answer
A rebuild is not guaranteed to match the shipped binary. A different Flutter or Dart version, different resolved dependency versions or any other change in the build inputs can produce different addresses and obfuscated names, so the new symbols' build_id does not match the one in the crash trace. A second possibility is that the wrong architecture file was used, for example app.android-arm.symbols for a trace whose arch: line shows arm64. At release time the team should have archived the exact build/symbols directory for 2.3.0, stored under that version, and used the file that matches the trace's architecture and build_id.
Exercise
A class Invoice is stored in a local cache using the key invoice.runtimeType.toString(). Everything works in debug mode, but cache lookups fail in an obfuscated release build. Why, and how do you fix it?
Show answer
In an obfuscated build the class is renamed, so runtimeType.toString() returns a scrambled name rather than "Invoice", and it may differ between builds. Stored keys from an older build then no longer match. Replace it with an explicit constant, such as static const cacheKey = 'invoice';, and use that for storing and looking up entries.