Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

introduce

Package volume optimization is essential for practical application development. Mobile phone memory is limited after all, if the package volume is too large a lot of users will directly give up (mobile phone memory small before, this is really important), now because of the large mobile phone memory (512 g has been able to resist the), users now focus on fluency and beautiful as a reference, but the some optimization is to optimize, after all to perfection.

This paper is mainly divided into two parts: installation package monitoring, installation package size optimization.

Installation package Monitoring

APK Analyser for Android Studio

This is an APK detection tool provided by Android Studio. It allows you to view the size of the contents of an APK file and display them in order of size. So it’s easy to see which part of the APK takes up the most space. Using APK Analyzer is as simple as dragging the APK file you want to analyze into Android Studio (or simply clicking APK in your project) and displaying something similar to the following:

As you can see from the above figure, classes.dex is not a very good code file, and it takes up a lot of space to view the image, so you can compress and optimize it accordingly. My project is a demo, so it takes up a lot of code. In actual projects, images must take a relatively large proportion of resources.)

As you can see from the figure above, APK Analyzer is actually used not only to look at THE size of APK, but also to analyze APK from its name, so you can use it to analyze the directory structure of some good APKs, code specifications, and even what dynamic library technologies are used.

ApkChecker(transfer) in Matrix

Matrix is a set of Application Performance Management (APM) system developed and being used by wechat terminal. Matrix-apkchecker, as part of Matrix system, is an analysis and detection tool for Android installation packages. According to a series of set rules, it detects whether apK has specific problems and outputs detailed detection results for analysis, troubleshooting and version tracking.

Installation package optimization practice

Lint Finds useless files

Use Lint to view unreferenced resources. Lint is a static scanning tool that identifies resource files in a project that are not referenced by any code. It’s easy to use, just click Analyze -> Inspect Code in Android Studio, as shown below:

Select the entire project as shown below

If there are unused resources in the project, Lint will display them in the Inspection Result window as follows:

Inefficient layout weights: Provide optimization solutions.

This is the unused resource: it makes the application bigger and slows down the build time.

There are many will not introduce, interested can go to play.

Enable compression, obfuscation, and optimization

When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 or higher, R8 is the default compiler for converting your project’s Java bytecode to DEX format to run on the Android platform. However, reduction, obfuscation handling, and code optimization are disabled by default when you create a new project using Android Studio.

        debug{
            // Enable code shrinkage, obfuscation, and optimization
            minifyEnabled true
            // Resource reduction
            shrinkResources true
            // Includes the default ProGuard rules file packaged with it
            //R8 configuration file.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
Copy the code

Is not enabled

After the commissioning of

File optimization

Image optimization

Lower image bit

You don’t need a fancy image to get the 32 bits down to 16 or 8 bits,

Use VectorDrawable images

UI little sister can provide the best can not provide, we make their own.

In Android Studio, go to File > New > Vector Asset

Local File (SVG,PSD) is what we need to convert.

Use tint shaders

For solid-color pictures, only changing the color requires importing another picture, which takes up resources. In this case, tint is used to avoid wasting resources and time.

<ImageView
            android:layout_width="200dp"
            android:src="@drawable/ic_vector"
            android:layout_marginTop="@dimen/dimen_20"
            android:layout_gravity="center_horizontal"
            android:layout_height="200dp"/> <ImageView ... Other attributes are the same as above android:tint="@color/color_188FFF"
            tools:ignore="UseAppTint" />
Copy the code

Tools: Igore is used to ignore specified errors or suppress warnings.

Use webP format images

Webp is an excellent, Google-recommended image format that reduces disk size while maintaining clarity.

Select image > Right click > Select

The image is changed from.png to.webp

Tripartite library optimization

Various tripartite “wheels” will be introduced in the App, but before introducing it, it is best to weigh whether it is necessary to introduce all of its code, causing unnecessary code or resources to be packaged into the APK.

For example, with Facebook bucket, you can’t use all of them and only import parts, such as login and sharing

dependencies {
    // Facebook Core only (Analytics)
    implementation 'com. Facebook. Android: facebook - core: 11.1.0'
    // Facebook Login only
    implementation 'com. Facebook. Android: facebook - login: 11.1.0'
    // Facebook Share only
    implementation 'com. Facebook. Android: facebook - share: 11.1.0'
    // Facebook Messenger only
    implementation 'com. Facebook. Android: facebook - messenger: 11.1.0'
    // Facebook App Links only
    implementation 'com. Facebook. Android: facebook - applinks: 11.1.0'
    // Facebook Android SDK (everything)
    implementation 'com. Facebook. Android: facebook -- android SDK: 11.1.0'} just import dependencies {implementation'com. Facebook. Android: facebook - login: 11.1.0'
    implementation 'com. Facebook. Android: facebook - share: 11.1.0'
}
Copy the code

For example XRecyclerView a RecyclerView implements pullRefresh, loadingMore, and Header Featrues. You may only use loadingMore, then you can intercept the section about loadingMore. Instead of importing the entire package.

About the App Bundle

This function is the same as ios, they just type all the resources into the project, and then the App Store, according to the properties of the installed device, select the corresponding resources to package for download.

Google’s Dynamic Delivery feature naturally solves this problem. When APK is installed through the Google Play Store, only the corresponding resources are selected and packaged into the APK file based on the properties of the installed device.

As shown in the picture below, you upload 700MB aAB, but if you download it, there will be two sets of resources typed in APK, but the user downloads only one set of resources 700MB.

However, App Bundle is only suitable for projects released on Google Play Store at present, and dynamic deployment is still implemented through various plug-in schemes in China, which can also be regarded as a scheme to reduce the size of installation packages to a certain extent.

There is also a SAO operation, that is, early resource packaging, follow-up resource users play while downloading, shortcomings may cause stuck and waste traffic, for reference only.