Directory:

  • Background:
    • FlutterPackage size analysis tool(App Size)introduce
    • AppEffects of excessive volume
  • The product of the Flutter App
    • 1.AndroidThe side of theFlutter
    • 2.IOSThe side of theFlutter
  • The officialDemo
    • Resource file optimizationDemo
    • Tripartite library optimizationDemo
  • Interesting experiment
    • Does a long variable name affect package size?
    • longStringDoes it affect package size?
    • Will unused methods be optimized away?
    • asDo keywords affect package size?
    • The three of the importAndroid pluginDoes it affect package size?
  • conclusion

Background:

Recently I looked at the source code of DevTools and found that there are two demos, through which we can learn how to use App Size.

App Size analysis tool for Flutter Package Size

The application volume tool allows you to analyze the total volume of your application. You can use the Analysis tag to view a single snapshot of Volume Information, or use the Diff tag to compare two different snapshots using volume Information.

More information can be found in the Analysis TAB, but not here.

Effects of App size too large

  • Lower download conversion rate
  • Performance:
    • Installation time:Add file copy,LibraryDecompression,odexCompile time
    • Runtime memory: ResourceResources,LibraryAs well asDexClass loading takes up a portion of the application’s memory
    • ROM space: There is not enough flash memory space and it is likely that “write magnification” will occur

The product of the Flutter App

1. Flutter on Android

product introduce
libflutter.so A C++ compilation of the Flutter engine
libapp.so The Business of Flutter is the product of compiling the Dart code of the framework and consists of four internal parts
flutter.jar Java code compilation of the Flutter engine
flutter_assets Static resources such as images, fonts, and LICENES are included

2. Flutter on IOS

product introduce
Flutter A C++ compilation of the Flutter engine
icudtl.dat Internationalization supports related data files
App Dart business code AOT compiled product
flutter_assets Static resources such as images, fonts, and LICENES are included

Official optimization Demo introduction

First, resource optimization Demo

In the first Demo, the official showed us the image optimization Demo.

Unoptimized Deme: There is a problem with reference image resource files being too large.

Optimized Demo: Optimized each image by image compression.

The Demo page is shown as follows:

Use the tool DIff analysis

Through the following figure:

As we can see, the tool analyzes the optimization effect of the resource file.

Second, third party library optimization Demo

Official for us to show the use of different libraries to achieve AES encryption function Demo

Unoptimized Demo: The introduction of ENCRYPT for AES encryption increases App package size by 500KB

Optimized Demo: Steel_crypt was introduced to achieve the same function, reduced by 400KB compared to non-optimized Demo

Use the DIff tool to analyze the following

I’m sure you have the same question as I do?

Why? Is there any difference between the two libraries.

What are the effects of encrypt and Steel_crypt libraries on package size?

What are the differences between the two libraries?

The difference between the two demos is the AES shown below:

Three party libraries AES contrast
encrypt
steel_crypt

Compare the two constructors above:

  • Encrypt: at least used in the encrypt constructorThree objects, all three objects arepackage:pointycastleThe package is defined.
  • Steel_crypt: very lightweight, only usedpackage:pointycastleAn object of

So the conclusion is that encrypt does something extra, which is to make a judgment at run time, even if you don’t need an object, but it will skip the compression optimization and go with the ENCRYPT library into your App package.

Interesting experiment

1. Do variable names affect package size?

Use the following variables in your code, respectively:

String s = '';
Copy the code
String ss... sss= ''; // omit several sCopy the code

Tested not!

2. Does long String affect package size?

Case 1: Long string

String s = '';
Copy the code
String s = 'ssss.. sss'; // omit several sCopy the code

After the test will!

Case 2: Same string

String s = 'sss.. sss'; String s1 = 'sss.. sss'; // omit several various stringsCopy the code

Tested not!

3. Will unused methods be optimized?

Define a Test class Test () method that references the DIO tripartite library. Only Test is initialized in the code without calling the Test () method

Definition:

import 'package:dio/dio.dart'; class Test { void test() { Dio(); }}Copy the code

How to use:

Widget build() { Test(); . }Copy the code

Tested not! Impact package size

So if I calltest()Change the way you use it?

Widget build() { Test().test(); . }Copy the code

The results are as follows:Will!Affects the package size.

4. asDo keywords affect package size?

Do not use the as

Widget build() { Test(); . Test() Test(); }Copy the code

Using the as

Widget build() { Test() as String; . Test() as String; Test() as String; }Copy the code

The results are as follows:Will!Affects the package size.

That adds up to 100 aS affecting 5.2KB,

Cause: An error in the execution of the AS keyword throws an exception, and I expect the error copy to be added to the code when Dart is compiled.

Suddenly it occurred to me that the jSON_serializable library uses the AS keyword heavily, which has some impact on package size.

5. Import three partiesAndroid pluginDoes it affect package size?

Permission_handler is a Flutter Plugin that handles native permissions.

Test the case referenced in Dependencies:

Don’t import permission_handler

# permission_handler: ^ 9.2.0Copy the code

Import permission_handler

Permission_handler: ^ 9.2.0Copy the code

It is concluded that the increase of classes.dex affects the package size.

It was also concluded that the App Size tool could not analyze native classes.dex files

Conclusion:

  1. The App Size can help us analyze the Size of the Flutter App installation package and compare it with other installation packages. Can be used for packet size monitoring.

  2. App Size is good at analyzing files in Dart AOT and Flutter Assets and analyzing code property data.

    The classes.dex file can only be analyzed to compare sizes.

  3. Several factors affecting package size :(not considering native effects)

    • Flutter_assets Size of the resource file

    • The AS keyword in the code affects package size

    • String affects package size.

      Note: Two identical strings are reused to reduce the impact on package size.

    • Introducing a native plug-in in Dependencies packages the plug-in into the classes.dex of the App, even if the Dart code is optimized.

    • Introduce a pure Dart library that will be analyzed at compile time to remove useless code.

      When choosing a tripartite library, try to choose a lightweight library. Reason: “Tripartite library Optimization Demo”