Toutiao APK slimming road

Toutiao APK slimming road

With the iteration of the version, the volume of the installation package will gradually increase as the function increases.

Today's headline 576 version APK has reached 25M. Through a series of optimizations, the current 607 version is 12M. This article mainly introduces some methods used in Toutiao APK slimming.

APK Analysis

Since we want to optimize the size of the APK, we must first look at the composition of the APK file.

Android Studio added the APK Analyzer function in version 2.2, which can directly open the apk file, as shown in the following figure 
image

The APK file mainly consists of the following parts:

       res主要是存放图片资源

       lib主要是存放so库,各个cpu架构

       classes.dex是java源码编译后生成的java字节码文件,因方法数限制拆分了多个dex

       assets主要存放不需要编译处理的文件

       resources.arsc是编译后的二进制资源文件,包括图片、文本索引等

       META-INF 签名信息

       AndroidManifest.xml 描述配置文件

From the composition of APK, it can be seen that there are several parts that account for a large proportion, and we can focus on optimizing them.

optimization

res folder

Image resource compression

1、ImageOptim

The corresponding client is provided, which supports batch processing through the client. You can use the following command to open it on the mac:

find . -name '*.png' | xargs open -a ImageOptim

2、TinyPng

No client is provided, and it supports dragging and dropping to web pages for processing; HTTP API is provided for batch processing, but there is a limit on the number, and each key can be compressed 500 per month. A gradle plugin has been developed for batch operations, and there are some similar plugins online: TinyPng Gradle plugin

Remove useless resources

1. Use Lint to detect and delete useless resources. When some business codes are deleted, the corresponding resources are missed. You can write a script to detect and remove the resources that are no longer used.

2. Add shrinkResources setting item ( official description ), there is 0.18M optimization space, but this setting is risky if you want to use it, you need to do a good job of testing

3、选择支持合适的图片,目前有ldpi mdpi hdpi xhdpi xxhdpi xxxhdpi资源文件夹,可根据自己app的用户设备选择支持2-3种即可(当然一套也行) 
高版本的gradle已不再支持通过resConfigs "nodpi", "hdpi", "xhdpi", "xxhdpi"配置支持的资源,只能人肉删除。如果你只想打包某一种屏幕密度的资源,可以使用分包策略,添加如下density配置可以只支持打包xhdpi资源(如果出现某些资源xhdpi没有,而其他文件夹包含的情况也不用担心,gradle会保留相应资源),这种配置最终会出多个apk包,具体介绍可参看官方说明

       splits {
    density {
        enable true
        reset()
        include "xhdpi"
        compatibleScreens 'small', 'normal', 'large', 'xlarge'
    }
}

4、如果想整体移除res下某个文件夹可以添加如下aaptOptions配置,而不用打包时手工删除,多个文件夹用:隔开

aaptOptions {
    ignoreAssetsPattern 'color-night-v8:drawable-night-v8'
}

arsc文件

resource.arsc文件记录了资源id和资源的对应关系(字符串的内容,图片的相对路径等) 

减少语言支持

目前包括各种语言(v7包引入),点击resources.arsc可以看到支持80种image可以通过修改gradle配置,去除不需要部分,这里我们保留4种

defaultConfig {
    resConfigs "zh-rCN", "zh-rHK", "zh-rTW", "en"
}

只保留"zh-rCN", "zh-rHK", "zh-rTW", "en" 减少不必要的语言(80种减到5种,有一个default)apk可减少0.61Mimage

资源混淆

开源解决方案AndResGuard可以看下,通过使用段路径和压缩可以减小apk,需要注意的是你的项目中某些资源需要keep,减少了1.5M。

lib文件夹

架构支持

Android系统目前支持以下七种不同的CPU架构:ARMv5,ARMv7 (从2010年起),x86 (从2011年起),MIPS (从2012年起),ARMv8,MIPS64和x86_64 (从2014年起)

每一个CPU架构对应一个ABI:armeabi,armeabi-v7a,x86,mips,arm64-v8a,mips64,x86_64

所有的x86、x8664、armeabi-v7a、arm64-v8a设备都支持armeabi架构的.so文件,x86设备能够很好的运行ARM类型函数库,但并不保证100%不发生crash,特别是对旧设备。64位设备(arm64-v8a, x8664, mips64)能够运行32位的函数库,但是以32位模式运行,在64位平台上运行32位版本的ART和Android组件,将丢失专为64位优化过的性能(ART,webview,media等等)。所以一般的应用完全可以根据自己业务需求选择使用armeabi或者armeabi-v7a一种支持就行。

可以通过gradle配置

       defaultConfig {
    ndk {
        abiFilter "armeabi"
    }
}

比如:微信、微博、QQ只保留了armeabi,Facebook、Twitter、Instagram只保留了armeabi-v7a

假设只支持了armeabi,如果有特殊要求(比如视频应用)需要用到部分armeabi-v7a的so,可以通过改名放到armeabi文件夹中,根据手机实际情况选择加载。

动态下发

比较大的so可以选择动态下发的形式延迟加载,代码上需要加一些判断逻辑。

dex文件

1、添加设置minifyEnabled true,混淆、压缩代码,这个设置现在app应该都已经添加了。

2、删除一些无用库,早期为了兼容低版本手机,添加了一些兼容库,随着时间推移APP支持的最低版本也在升高,之前的一些无用库就可以移除。

3、插件下发业务模块 添加插架框架,将部分代码延迟下发加载,这部分效果显著。

其他

In extreme cases, the following two methods can be considered, which can optimize the space of about 1M

debug information

Generally, we will configure Proguard to retain line numbers and other information for online log analysis. In extreme cases, we can also consider removing this part. There will be a 5%-10% effect, which can reduce 0.5M, but for convenience Not removed.

-keepattributes SourceFile,LineNumberTable

supportv7 package

If you don't rely on the supportv7 package much, you can directly copy the content you use and deal with it separately. After all, the package will increase the volume by at least 0.4M. After the business is complicated, this part is not easy to operate and maintain. Toutiao has not been used for the time being. .

EVERYTHING

The functions are constantly iterated, and the slimming business is endless.

To maintain and continue to reduce the apk package, we must continue to optimize, and now the following ideas have not been implemented, you can look at

1. The new version of Google's support-v4 package has been split, and version 24.2.0 has been split into 5 modules: support-compat, support-core-utils, support-core-ui, support-media-compat, support-fragment, you can refer to the corresponding module separately according to your needs. 
The v7 package also depends on v4. The maven dependency has the advantage that the corresponding dependencies can be eliminated separately by exclude, as follows:

       compile ('com.android.support:appcompat-v7:24.2.0') {
       exclude module: 'support-v4'
       }
       compile 'com.android.support:support-fragment:24.2.0'

However, at present, various APPs use the support package deeply, and each module of the support package will also have related dependencies. Whether it can be used depends on the actual situation.

2. Use ReDex optimization, which is a tool open sourced by Facebook to reduce the size of Android app to improve performance. If it is integrated, it is risky and requires more testing and tutorials .

3. Reduce the hidden overhead of java, such as some automatically generated functions.


Reprinted from: https://techblog.toutiao.com/2017/05/16/apk/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648378&siteId=291194637