Flutter\'s wiki mentions obfuscation is an opt-in in release mode.
And yet, the flutter build command has no relevant option - see:
flutter he
Obfuscation is needed - a flutter app knows its function names, which can be shown using Dart's StackTrace class. There's under-tested support for obfuscation. To enable it:
For Android:
Add to the file [ProjectRoot]/android/gradle.properties
:
extra-gen-snapshot-options=--obfuscate
For iOS:
First, edit [FlutterRoot]/packages/flutter_tools/bin/xcode_backend.sh
:
Locate the build aot
call, and add a flag to it,
${extra_gen_snapshot_options_or_none}
defined as:
local extra_gen_snapshot_options_or_none=""
if [[ -n "$EXTRA_GEN_SNAPSHOT_OPTIONS" ]]; then
extra_gen_snapshot_options_or_none="--extra-gen-snapshot-options=$EXTRA_GEN_SNAPSHOT_OPTIONS"
fi
To apply your changes, in [FlutterRoot], run
git commit -am "Enable obfuscation on iOS"
flutter
(Running "flutter" after the commit rebuilds flutter tools.)
Next, in your project, add following to [ProjectRoot]/ios/Flutter/Release.xcconfig
file:
EXTRA_GEN_SNAPSHOT_OPTIONS=--obfuscate
PS: Haven't tried the --save-obfuscation-map flag mentioned at https://github.com/dart-lang/sdk/issues/30524
Again, obfuscation isn't very well tested, as mentioned by @mraleph.
At the moment obfuscation is not directly exposed via the flutter CLI.
You can try to explicitly pass arguements to the gen_snapshot
like this:
flutter build --extra-gen-snapshot-options=--obfuscate,--save-obfuscation-map=build/symbols.json --release
Note: that obfuscation support is not very well tested and might not work at the moment.
All the above answers are correct, but no answer tells you that we need to add a relative path or directory path while generating build.
Example using Relative Path:
flutter build apk --obfuscate --split-debug-info=./ProjectFolderName/debug
Example using Folder Path:
flutter build apk --obfuscate --split-debug-info=/Users/apple/Desktop/items/debug
The above command will generate a build inside the given project directory, it will create a new folder called ProjectFolderName
or 'debug' on the respective command, and there you can find the release build.
https://flutter.dev/docs/deployment/obfuscateRefer this link for more info
Note: Flutter’s code obfuscation, Supported as of Flutter 1.16.2.
For AppBundle:
Without splitting:
flutter build appbundle --obfuscate --split-debug-info=/<directory>
Splitting:
flutter build appbundle --target-platform android-arm,android-arm64,android-x64 --obfuscate --split-debug-info=/<directory>
For APK:
Without splitting:
flutter build apk --obfuscate --split-debug-info=/<directory>
Splitting:
flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi --obfuscate --split-debug-info=/<directory>
Works only for Flutter version v1.16.2 or higher.
PS: About Splitting:
By default, fat apk contains arm v7, arm v8 and x64 which increases apk size, which you don't want to. So, when you split it, you have separate binaries which you can upload on the store and thus reducing the size of the apk that a user would need to download.