Android performance optimization application slimming (APK slimming)

About the author: CSDN content partner and technical expert, built an APP with tens of millions of daily users from scratch.
Focus on sharing original series of articles in various fields, specializing in java backend, mobile development, artificial intelligence, etc. I hope everyone will support me.

Insert image description here

1. Introduction

We continue to summarize and learn Android knowledge , review the past and learn new things.

lint check code

2. Overview

The priority of APK slimming optimization is relatively low, because there is no obvious benefit after optimization. This area mainly affects the conversion rate. The smaller the application, the higher the user's willingness to download. In addition
, When pre-installing, there are also size requirements.

Generally speaking, we can optimize in the following ways:

  • Resource files: compressed; dynamically downloaded.
  • so file: dynamic so; different architecture support (v7a, v8a).
  • Source code: Use code analysis tools to remove code that is no longer used; plug-in.
  • assets: dynamic download; compression.
  • proguard obfuscated code

2.1 apk composition

Drag the apk directly to the android studio window, or after we unzip the apk, you can see the following files

document describe
classes (there are multiple).dex Java Class, a file format that can be understood by the Dalvik/ART virtual machine after being compiled by DEX
lib To store so files, there may be multiple structures
assets Program resources, use AssetManager to call
res Resource files, such as: drawable, layout
AndroidManifest AndroidManifest
resources.arsc Compiled binary resource file
META-INF Contains signature summaries and other information for all files in the APK

3. Optimization direction

Our optimization plan corresponds to the composition of this apk

3.1 Source code

3.1.1 Code obfuscation

3.1.2 Remove useless code

  1. Tools that come with the studio
    Insert image description here

  2. Using CPD/PMD Tools
    PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, etc.
    PMD Website
    PMD GUI Website:

  3. Use aspect programming to perform statistics (AOP) to identify useless code.

3.1.3 Processing of third-party libraries

Unify basic libraries,
unify libraries called by three parties, etc.

The principle is to use the same function and do not have multiple sets of codes.

3.2 Resource files

  1. Delete redundant resources

  2. Resource obfuscation

  3. Resource compression

    • jpg: adopts compression algorithm and does not support transparent channels
    • png: lossless compression, supports transparency
    • webp: lossless compressed image format, which can significantly reduce file size compared to JPG

Although png is lossless compression. But there are also some websites that can perform lossy compression of png images, such as tinypng website

  1. Resources are online and dynamically downloaded.
    For some pictures that are relatively large and not needed immediately, you can consider downloading them from the server.

You can use the following three methods to find useless resources

3.2.1 Method 1 Inspect Code

  1. Via Android Studio menu bar → code → Inspect Code
  2. Select configure in the pop-up box (lower right button)
  3. Enter unused resources in the pop-up box (in the upper left corner) and check the corresponding option
  4. Click to start

As shown below
Insert image description here

There are really many options here. We only find unused resource files. It is best to cancel all other selections first, and then check unused resources.
Insert image description here

At this time, we will start analyzing the project. We will wait patiently for a while.
Insert image description here

3.2.2 Method 2

Menu bar → Refactor → Remove Unused Resources
Insert image description here

This will start deleting it directly. Of course, you have to check it and don’t delete it by mistake.

3.2.3 Method 3 Tencent’s Matrix

Matrix is ​​an APM (Application Performance Management) system self-developed and currently used by WeChat terminal.
Matrix-ApkChecker, as part of the Matrix system, is an analysis and detection tool for android installation packages. It detects whether apk has specific problems according to a series of set rules, and outputs a more detailed detection result
report, which is used to analyze and troubleshoot problems. Version tracking. Matrix-ApkChecker is provided in the form of a jar package, which
can be run by executing java -jar ApkChecker.jar on the command line.

You can also configure it

java -jar matrix-apk-canary-2.1.0.jar --config CONFIG-FILE-PATH

Search matrix git for APK Checker. There is a download link here. Click to download the jar package.

Matrix apkchecker documentation

CONFIG.json file, you can also use the official website. In the end, two files will be generated, just check them.

{
    
    
  "--apk":"E:\\release.apk",
  "--mappingTxt":"E:\\mapping.txt",
  "--output":"E:\\",
  "--format":"mm.html,mm.json",
  "--formatConfig":
  [
    {
    
    
      "name":"-countMethod",
      "group":
      [
        {
    
    
          "name":"Android System",
          "package":"android"
        },
        {
    
    
          "name":"java system",
          "package":"java"
        },
        {
    
    
          "name":"com.tencent.test.$",
          "package":"com.tencent.test.$"
        }
      ]
    }
  ],
  "options": [
    {
    
    
      "name":"-manifest"
    },
    {
    
    
      "name":"-fileSize",
      "--min":"10",
      "--order":"desc",
      "--suffix":"png, jpg, jpeg, gif, arsc"
    },
    {
    
    
      "name":"-countMethod",
      "--group":"package"
    },
    {
    
    
      "name":"-checkResProguard"
    },
    {
    
    
      "name":"-findNonAlphaPng",
      "--min":"10"
    },
    {
    
    
      "name":"-checkMultiLibrary"
    },
    {
    
    
      "name":"-uncompressedFile",
      "--suffix":"png, jpg, jpeg, gif, arsc"
    },
    {
    
    
      "name":"-countR"
    },
    {
    
    
      "name":"-duplicatedFile"
    },
    {
    
    
      "name":"-unusedResources",
      "--rTxt":"E:\\R.txt",
      "--ignoreResources"
      :["R.raw.*",
        "R.style.*",
        "R.attr.*",
        "R.id.*",
        "R.string.ignore_*",
        "R.dimen.*",
        "R.string.*",
        "R.color.*"
      ]
    },
    {
    
    
      "name":"-unusedAssets",
      "--ignoreAssets":["*.so" ]
    }
  ]
}

In this way, we get back an HTML analysis result table, but all we see are obfuscated resource files. At this time, we can use aapt2 to output the contents of the apk resource table (resources.arsc file):

aapt2 dump resources xxx.apk > logx.txt

3.2.4 Resource confusion

3.3 so lose weight

Dynamic so;
specified architecture support (such as v7a, v8a);

In addition, you can only keep one CPU architecture, and other CPU architectures will automatically adapt. During the running of the program, the appropriate CPU architecture will be selected by judging the CPU architecture.

if(v7a) {
    
    
    xxx
} else {
    
    
    xxx
}

There is also plug-in, which is a topic and we will talk about it in another article.

4. Recommended reading

Java column

SQL Column

Data Structures and Algorithms

Android learning column

ddd

Guess you like

Origin blog.csdn.net/fumeidonga/article/details/132765927