In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article is a detailed introduction to "how to realize Flutter slimming". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to realize Flutter slimming" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of the small editor.
Let's take ios as an example to analyze the size of flutter products in the project (ipa package slimming needs are more urgent).
The ios project has the following dependencies on Flutter:
Flutter.framework : Flutter library and engine
App. framework: dart Business source code related files
Flutter Plugin: framework of compiled plugins
Flutter_assets: Static assets that Flutter depends on, such as fonts, images, etc.
After introducing the flutter version modification details page for the first time, the ipa package size increased by nearly 20M, including the flutter engine code + the modified business code. After continuing to release the page flutter modification, the ipa increased by 4M +. After further analysis and decompression of ipa file, it is found that Flutter.framework has remained stable at 20M+. After adding a new Flutter Service--Release Page, App.framework has increased by nearly 10M!
Flutter.framework is Flutter library and engine code, we can do optimization space is limited, first target dart business-related file App.framework.
Flutter Product Size Analysis
Execute the following command to compile an App.framework in release mode, and print out the specific size of the product using the print-snapshot-sizes parameter.
flutter build aot --release --extra-gen-snapshot-options=--print-snapshot-sizes
The results were as follows:
Building AOT snapshot in release mode (android-arm-release)... VMIsolate(CodeSize): 4660Isolate(CodeSize): 2585632ReadOnlyData(CodeSize): 2693576Instructions(CodeSize): 8064816Total(CodeSize): 13348684Built to build/aot/.
Instructions: Represents the size of binary code generated after AOT compilation
ReadOnlyData: metadata representing the binary code generated (e.g. PcDescriptor, StackMap, CodeSourceMap, etc.) and string size
VMIsolate/Isolate: Represents the sum of the sizes of the remaining objects (e.g. constants defined in code and VM-specific metadata)
Specific to the business layer, what should I do if I want to analyze the size occupied by each business module?
Execute the following command to compile an App.framework of arm64 architecture, and put its package composition structure into the specified directory build/aot.json file
flutter --suppress-analytics build aot --output-dir=build/aot --target-platform=ios --target=lib/main.dart --release --ios-arch=arm64 --extra-gen-snapshot-options="--dwarf_stack_traces,--print-snapshot-sizes,--print_instructions_sizes_to=build/aot.json"
Use the dart command to convert the aot.json file generated in the previous step into a structured visual web page
dart ./ bin/run_binary_size_analysis.dart build/aot.json path_to_webpage_dir
run_binary_size_analysis.dart is an analysis tool provided by dart. The path in the source code of the flutter engine is as follows:
Open index.html in the generated folder to analyze the size occupied by specific businesses. The Large Symbols and Large Files buttons in the upper right corner can directly locate methods/files with a volume ratio from large to small.
As an example, the analysis above shows that the PInfoItemInternal.fromJson method takes up a lot of volume, and traces show that the main operation of this method is to convert Map data into objects
PItemInfoInternal.fromJson(Map map) { id = map['id'] as String; attributes = map['attributes'] as String; title = map['title'] as String; ......}
From this we can infer that this type conversion operation will result in compilation generating some bulky code.
optimization measures
Reduce display type conversion operations
According to the above analysis, it is found that the displayed type conversion as String/Bool/Int will lead to a significant increase in the size of App.framework, mainly because it will increase the type checking and exception handling logic:
if (x.classId
< A && x.classId >B) throw "x is not subtype of String";
The 400k+ volume can be successfully reduced by extracting static common methods.
Reduce the size of generated code by compiling parameters--dwarf_stack_trace and--obfuscate
dwarf_stack_trace indicates that stack trace symbols are not used in the generated dynamic library file
obfuscate represents obfuscation, reducing code size by reducing variable/method names
//Compile release package and print sizeflute build aot --release --extra-gen-snapshot-options=--print-snapshot-sizes/--dwarf_stack_traces, --> reduce size by 6.2% flutter build aot --release --extra-gen-snapshot-options="--dwarf_stack_traces,--print-snapshot-sizes"//-obsfusion, --> reduce size by 2.5% flutter build aot --release --extra-gen-snapshot-options="--dwarf_stack_traces, --print-snapshot-sizes,--obfuscate"//Total size reduced 8.7%
By modifying the ios package script xcode_backend.sh and deleting the dSYM symbol table information file, App.framework successfully reduced its size by 20%. dSYM is a transit file that stores the address mapping information of hexadecimal functions. It contains the symbols we debug. It is used to analyze the crash report file and parse out the correct error function information.
Stripping dSYM out of the framework using the xcrun command can significantly reduce the size of App.framework.
RunCommand xcrun dsymutil -o "${build_dir}/aot/App.dSYM">
Reduce volume increase caused by duplication of flutter and native resources
By bridging, flutter uses platform-side resource files directly to avoid the problem of increasing package size due to duplication of resource files.
The main way is to pass information on the Flutter and Platform side through BasicMessageChannel. The Flutter side passes the resource name AssetName to the Platform side. After receiving the AssetName, the Platform side locates the resource file according to the name, and passes the file back to the Flutter side in binary data format through BasicMessageChannel.
Read here, this article "Flutter slimming how to achieve" article has been introduced, want to master the knowledge points of this article still need to practice to understand, if you want to know more related content articles, welcome to pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Third memory failure at cpu2 location
© 2024 shulou.com SLNews company. All rights reserved.