Performance optimization of Android APK limit compression (more resources, the effect is more significant)

Brief introduction

With the continuous iteration of the project, the amount of code with the resource file continues to grow. APK file then there will be more and more packaged, if suddenly one day you call your boss or leader to optimize APK size, you do not know how to optimize it would not be justified, we will go together to analyze and optimize this article APK volume size of it.

Effect after the first optimization FIG look

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

APK resource consumption analysis

Performance optimization of Android APK limit compression (more resources, the effect is more significant)
note:

I was looking for a GitHub relatively high popularity of open-source projects, if required, you can click to download , try something yourself.

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

AS Build analysis tools directly used / Analyze APK

Draw assets> classes.dex> res> From the above figure lib where the resource files are taking up the most.

Here we take a look at how to reduce the size of it APK,

APK eight big volume optimization

1. convert the picture format webp

Webp concept

WebP is a lossy compression while providing lossless compression of image file format, derived from VP8 video encoding format. WebP originally released in 2010, aims to reduce the file size, but to achieve the same quality JEPG format images, hoping to reduce the transmission time of image files on the network. November 8, 2011, Google started to make WebP support lossless compression and transparent color function.

According to Google earlier test, WebP lossless compression less than 45 percent of the file size of the PNG file found on the network, even if they use PNG files in PNGCRUSH and PNGOUT treated, WebP still can reduce file size by 28%. For now, Webp can make an average 70% reduction in the size of the picture. WebP image format is the future trend of.

PNG / JPG to Webp

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

Click on the image or folder, right-select Convert to Webp format, png / jpg picture compression format is webp picture.

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

Finally, we only reduced by about less than 200 kb, it is possible to project images resources already not much, just too much due to the small picture.

Scenarios and advantages

  • 客户端软件,内嵌了基于 Chromium 的 webview,这类浏览器中应用的网页是可以完全使用WebP 格式,提升加载渲染速度,不考虑兼容。
  • 用 node-webkit 开发的程序,用 WebP 可以减少文件包的体积。
  • 移动应用 或 网页game ,界面需要大量图片,可以嵌入 WebP 的解码包,能够节省用户流量,提升访问速度优势:
  • 对于 PNG 图片,WebP 比 PNG 小了45%。

2. 去除多语言

在 app/build.gradle 添加

android{
    ...
    defaultConfig{
        ...
        //只保留英语
        resConfigs "en"
    }
}

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

这里我们发现减少了大概 200 kb

3. 去除不必要 so 库

通过反编译 Android 微信版本 得知,微信也只适配了 armeabi-v7a 架构,那么我们删掉其它库的支持吧。

android{
    ...
    defaultConfig{
        ...
            ndk {
            //设置支持的SO库架构
            abiFilters "armeabi-v7a"
     }
}
}

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

又优化了差不多 600 kb ,继续。

4. 去除无用资源 Link 检查(谨慎删除)

概念

Lint 是 Android Studio 提供的 代码扫描分析工具,它可以帮助我们发现代码结构 / 质量问题,同时提供一些解决方案,而且这个过程不需要我们手写测试用例。代码迭代版本一多,很容易会遗留一些无用的代码、资源文件,我们可以使用 Lint 进行清除。

怎么使用 Link 检查

打开 AS 工具,找到 Analyze > Run Inspection By Name > unused resources

优化

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

发现我们 link 大概优化了 700 kb继续。

注意

因为 link 是检查有没有引用来做的判断是否使用了资源,那么如果是这种方式勒,所以在删除的时候一定要谨慎。

//动态获取资源 id , 未直接使用 R.xx.xx ,则这个 id 代表的资源会被认为没有使用过(类似不能混淆反射类)
int indetifier =getResources().getIdentifier("img_bubble_receive", "drawable", getPackageName()); getResources().getDrawable(indetifier);

5. 开启混淆

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

优化了大概 1.7M 继续。

6.移除无用资源 shinkResource

  • 开启 shinkResource = true

        buildTypes {
            release {
                minifyEnabled true
                shrinkResources = true
    
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debug {
                shrinkResources = true
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

这个有可能 link 删除了无用资源,所以没有在优化了

7.开启删除无用资源 (严格模式和普通模式) - 这个我这里就不可测试,你们下来可以测试下效果

普通模式也就是自定义模式

如果您有想要保留或舍弃的特定资源,请在您的项目中创建一个包含 <resources> 标记的 XML 文件,并在 tools:keep 属性中指定每个要保留的资源,在 tools:discard 属性中指定每个要舍弃的资源。这两个属性都接受逗号分隔的资源名称列表。您可以使用星号字符作为通配符。

例如:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
    tools:discard="@layout/unused2" />

将该文件保存在项目资源中,例如,保存在 res/raw/keep.xml。构建不会将该文件打包到 APK 之中。

指定要舍弃的资源可能看似愚蠢,因为您本可将它们删除,但在使用构建变体时,这样做可能很有用。例如,如果您明知给定资源表面上会在代码中使用(并因此不会被压缩器移除),但实际不会用于给定构建变体,就可以将所有资源放入公用项目目录,然后为每个构建变体创建一个不同的 keep.xml 文件。构建工具也可能无法根据需要正确识别资源,这是因为编译器会添加内联资源 ID,而资源分析器可能不知道真正引用的资源和恰巧具有相同值的代码中的整数值之间的差别。

严格模式

正常情况下,资源压缩器可准确判定系统是否使用了资源。不过,如果您的代码调用 Resources.getIdentifier()(或您的任何库进行了这一调用 - AppCompat 库会执行该调用),这就表示您的代码将根据动态生成的字符串查询资源名称。当您执行这一调用时,默认情况下资源压缩器会采取防御性行为,将所有具有匹配名称格式的资源标记为可能已使用,无法移除。

例如,以下代码会使所有带 img_ 前缀的资源标记为已使用。

String name = String.format("img_%1d", angle + 1);
res = getResources().getIdentifier(name, "drawable", getPackageName());

资源压缩器还会浏览代码以及各种 res/raw/ 资源中的所有字符串常量,寻找格式类似于 file:///android_res/drawable//ic_plus_anim_016.png 的资源网址。如果它找到与其类似的字符串,或找到其他看似可用来构建与其类似的网址的字符串,则不会将它们移除。

These are examples of security enabled by default compression mode. However, you can disable this "be prepared" approach, and specified resource compressor retaining only the resources that have been used to determine. To perform this, in keep.xmlthe files shrinkModeset as strictfollows:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="strict" />

If you are indeed stringent compressed mode is enabled, and also refers to the code resource (as shown above) comprising dynamically generated string must use tools:keepthe attribute manually retain these resources.

8. The AndResGuard micro channel resource compression scheme

What is AndResGuard

AndResGuard is a tool to reduce the size of APK, its principle is similar to Java Proguard, but only for resources. It will otherwise redundant resource path is shortened, for example res / drawable / wechat becomes r / d / a.

Why AndResGuard

In the past development, we usually only confuse code, resource file was exposed in front of others, readability res folder all file names too strong.

The effect of after use

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

AndResGuard configuration

  • Build.gradle project root directory, add the dependent plug-ins:

     dependencies {
            classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.16'
        }
  • In the app directory, create and_res_guard.gradle file
apply plugin: 'AndResGuard'

andResGuard {
    mappingFile = null
    use7zip = true
    useSign = true
    keepRoot = false
    compressFilePattern = [
            "*.png",
            "*.jpg",
            "*.jpeg",
            "*.gif",
            "resources.arsc"
    ]
    whiteList = [
            // your icon
            "R.drawable.icon",
            // for fabric
            "R.string.com.crashlytics.*",
            // for umeng update
            "R.string.tb_*",
            "R.layout.tb_*",
            "R.drawable.tb_*",
            "R.drawable.u1*",
            "R.drawable.u2*",
            "R.color.tb_*",
            // umeng share for sina
            "R.drawable.sina*",
            // for google-services.json
            "R.string.google_app_id",
            "R.string.gcm_defaultSenderId",
            "R.string.default_web_client_id",
            "R.string.ga_trackingId",
            "R.string.firebase_database_url",
            "R.string.google_api_key",
            "R.string.google_crash_reporting_api_key",

            //友盟
            "R.string.umeng*",
            "R.string.UM*",
            "R.layout.umeng*",
            "R.drawable.umeng*",
            "R.id.umeng*",
            "R.anim.umeng*",
            "R.color.umeng*",
            "R.style.*UM*",
            "R.style.umeng*",

            //融云
            "R.drawable.u*",
            "R.drawable.rc_*",
            "R.string.rc_*",
            "R.layout.rc_*",
            "R.color.rc_*",
            "R.id.rc_*",
            "R.style.rc_*",
            "R.dimen.rc_*",
            "R.array.rc_*"
    ]

    sevenzip {
        artifact = 'com.tencent.mm:SevenZip:1.2.10'
    }
}
  • build.gradle file in the app module added

    apply from: 'and_res_guard.gradle'
  • After finished packing renderings

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

Resources compressed about 1M

to sum up

  1. The larger volume projects, more resources, more noticeable results.
  2. Link deleted using the resource, we must be cautious, ahead of the backup.
  3. Because here just over 10 M project itself, finally optimizes the 4.5 M down. It is still not easy.

Well, the article to end here if you feel the article fairly useful, may wish to recommend them to your friends.

Finally, here is the Android learning about myself, interview documents, organize large video collection, interested partners can be added to the group: 887 084 983 to receive a free look ~

Performance optimization of Android APK limit compression (more resources, the effect is more significant)

Guess you like

Origin blog.51cto.com/14332859/2443443