Obfuscating The Flutter App
In-app development, it is important to manage its performance. Performance covers different aspects like speed and responsiveness and it depends on things like app size and rendering performance.
if we talk about issues related to performance there are two types of issues some are space-related issues and some are time-related issues.
Time-related issues occur when the app starts consuming too much time on any tasks or when it forces the device to run at a faster pace.
Here we are going to address the problem of app size, as you well know when we build our app it consists of too many things and in the end, we get an app bigger in size so here we are going to learn how do we get rid of this issue.
App Size
App size is an issue that can bother anyone whether they are developers or users. as you know that the APK, app bundle, or IPA all of them contain everything like code and assets and run itself with them and if it’s size is heavy then it may create trouble because if it is large in size it will take more space and it may break the limit of useful features like Android instant apps.
when you launch your app using the IDE button or by running flutter run
the command, it generates a debug build and large in size but it allows to hot reload and source-level debugging.
Now we would see how we check and estimate the size of a flutter app…
The release APK generally we create usingflutter build apk
or flutter build ios
provide the uploaded package to the play store or app store but they do not represent the end user’s download size, the app store generally reprocesses and split the uploaded package to in order to make suitable for the downloader’s device by filtering the things like native’s library, phone’s DPI.
Reducing the app size
Size reduction becomes one of the vital processes when it comes to making your app available for users.
Starting in Flutter version 3.22 and DevTools version 3.4, a size analysis tool is included to help developers understand the breakdown of the release build of their application.
The size analysis tool is invoked by passing the --analyze-size
flag when building:
flutter build apk --analyze-size
flutter build appbundle --analyze-size
flutter build ios --analyze-size
flutter build linux --analyze-size
flutter build macos --analyze-size
flutter build windows --analyze-size
In the case of android APK or bundle, you also need to mention the target platform and one of the android arm so the commands will be like
flutter build apk --target-platform android-arm --analyze-size
flutter build apk --target-platform android-arm64 --analyze-size
flutter build apk --target-platform android-x64 --analyze-size
flutter build appbundle --target-platform android-arm --analyze-size
flutter build appbundle --target-platform android-arm64 --analyze-size
flutter build appbundle --target-platform android-x64 --analyze-size
and the result will be like
and build will be different in two ways
- The tool compiles Dart in a way that records code size usage of Dart packages.
- The tool displays a high-level summary of the size breakdown in the terminal and leaves a
*-code-size-analysis_*.json
file for more detailed analysis in DevTools.
As you can see in the picture, how much space every item is acquiring, and if you want any kind of deep analysis you can use Devtools.
If you don’t have any idea how to use dev tools you can see my blog here.
Now let’s focus on how to reduce the size?
When you are building the release version of an app you need to run this command --split-debug-info
this will automatically reduce the size of the app. However, you can see Obfuscating the dart code.
Code obfuscation is the process of modifying an app’s binary to make it harder for humans to understand. Obfuscation hides function and class names in your compiled Dart code, making it difficult for an attacker to reverse engineer your proprietary app.
So if you want to obfuscate your app then you need to check version for different platforms
For android/iOS: you need a minimum of Flutter version 1.16.2 to do this, To obfuscate an app built against an earlier version of Flutter, use the process mentioned above.
For macOS: macOS (in alpha as of Flutter 1.13), supports obfuscation as of Flutter 1.16.2.
Linux/Windows: Not Available
web: Not Available
Flutter’s code obfuscation, when supported, works only on a release build.
Note: You need to run flutter upgrde and the latest version will be available.
You can also use these steps in order to reduce the app size
- Remove unused resources
- Minimize resource imported from libraries
- Compress PNG and JPEG files
Obfuscation of an app:
There is a simple procedure for obfuscation of the flutter app, you only need to run the build command for the release version using the --obfuscate
flag combined with the --split-debug-info
flag.
This --split-debug-info
refers to the location or directory where flutter outputs the debug files. This command generates a symbol map. The apk
, appbundle
, ios
and ios-framework
targets are currently supported. (macos
and aar
are supported on the master and dev channels.) for example
flutter build apk --obfuscate --split-debug-info=/<project-name>/<directory>
Once you’ve obfuscated your binary, save the symbols file. You need this if you later want to de-obfuscate a stack trace.
Once you’ve obfuscated your binary, save the symbols file. You need this if you later want to de-obfuscate a stack trace.
For the detail information on these commands, you can run flutter build apk -h
and if you are not getting any info then you need to check your flutter version and if it does not support then run the flutter upgrade
.
Reading the Obfuscating stack trace
To debug a stack trace created by an obfuscated app, use the following steps to make it human-readable:
- Find the matching symbols file. For example, a crash from an Android arm64 device would need
app.android-arm64.symbols
. - Provide both the stack trace (stored in a file) and the symbols file to the
flutter symbolize
command. For example:
flutter symbolize -i <stack trace file> -d /out/android/app.android-arm64.symbols
For more information on the symbolize
command, run flutter symbolize -h
.
Conclusion
After doing all the pieces of stuff explained in the article finally we have reduced the total app size which helps users as well as developers to work faster with there respective work but that was only one issue which has been addressed in order to make apps responsive and faster another issue related to rendering will be addressed in an upcoming blog.
Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.💙
If this article has helped you a bit and found interesting please clap!👏
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.