Technology | Android installation package optimization

Copyright

1. This article belongs to original author, reprinted must be marked with the original source and author information.

2. The author: Zhao Yu (vimerzhao), Permanent link: https://github.com/vimerzhao/vimerzhao.github.io/blob/master/android/2020-02-11-apk-size-opt.md .

3. The number of public: V Masters in Line . E-mail:[email protected] .


background

Why the installation package expansion

Increase the product of the evolution of business is to increase the size of the installation package essential reason. But on the way of evolution, as some so-called technical debt, such as:

  • Use of resources without cutting (such as all of the font file, the resolution is too large picture)
  • Unreasonably large resources (such as large video, audio, online pulled)
  • Business has been off the assembly line is not cleared up the relevant code, resources
  • and many more

It is because of these negligent installation package with unnecessary increase, which is why we need to optimize the part.

Installation package optimized value

Some people may feel that now is basically 4G, WiFi network environment, the performance of mobile devices and storage space is adequate, so users should not be sensitive to the size of the installation package. However, this is actually an illusion, more time should see their target market, if it is a second-tier cities, of course, no problem, but if it is a four-tier cities and rural areas, sinking market or India, Brazil and other overseas markets, obviously these assumptions are not established.

In general, the installation package size affects the following indicators:

  • Download conversion rate
  • Installation success rate
  • Promotion costs
  • Running memory, footprint

In summary, for a "spare capacity" of the team, to optimize the installation package size is still very necessary.

Routine optimization of the installation package

By the above reasons, from a business perspective, the packet size is optimized to install "the trouble should end", that is:

  • Compression resources
  • Online resources turn into big pull
  • Investigation useless and offline business

These methods are more conventional, like a remedy, some technical following carding universal method , independent of the business, in the method of those described above have tried, it is also possible to install the packet size "more lower floor."

The installation package is mainly composed of code and resources, but also to optimize these two aspects.

Code

Confusion

Generally through ProGuard, but with a lot of bad, and there is a management problem. Finally, a look at the project, many classes are Keep live, do not know the historical background is valid.

Dex optimization

  1. Remove the line number information tools, the problem is not brought back Crash position, but can be resolved
  2. When multi-Dex, by rearrangement index to avoid cross-Dex, implement more complex
  3. Dex compression, the actual compression Dex, for the first time by a shell starts to load, it will affect the startup speed, but can be resolved, but also more complicated

so Optimization

so optimization tools and Java code is actually quite similar, or to cut through the core mechanism of the means of compression.

Resources

Resource itself

  • By scanning tool useless resources, some projects due to their own reasons, you may not have an explicit reference, but external compile script to modify the project's own script, which is very pit
  • Optimize resource format, such as PNG turn JPG (removing the alpha channel), transfer format webp
  • Font files retain only use its own secondary resource optimization

Resources index

By AndResGuardmeans compressing the resource name, thus reducing the size of the index file.

Sentiment

main idea

  • Remove unused (obvious)
  • Compression / transfer useful (such as images, font files, such as the transfer of large resources, confusing map table information is also a transfer)
  • Streamline the system generated (DEX rearrangement)

Conventional means to easily reach the bottleneck, means there are profound complexity, compatibility and many other issues, but together, or two:

  • A deep understanding of the underlying : apk compilation process, dex format, resource references, start the process, such as loading process principles
  • Deep understanding of the tools : ProGuard (a lot of people are Copy ProGuard configuration modification), ReDex, AndResGuard etc.

Weigh Road

In many cases, the optimization is a trade-off, it is important to choose:

  • Compressed Dex, increases the startup speed
  • Removed the line number, it is more difficult to restore the stack Crash
  • Obfuscates the code, compile it increases the complexity of the script
  • and many more

The fundamental way

  • Establish monitoring : once a year the team will optimize the installation package size, especially delete, compress time resources, always confused, do not understand the purpose of the introduction of this resource, we did not dare to act rashly. Finally have a system, so that when there is an incremental recording, there is a reminder when let off the assembly line and timely solutions, not every end is also a technical debt.
  • The development of standards : In a sense, the installation package size optimization is not a personal thing, but a team thing, the best practice guidelines or relevant CodeReview standards. When invalid logic was unreasonably disregarded after the introduction of the resource, or the business off the assembly line, to have rules to restrain such behavior.

reference

the above


Welcome to sweep the attention of the public code number, timely access to the latest information.

Guess you like

Origin www.cnblogs.com/zhaoyu1995/p/12319360.html