Android APK weight-loss practice

This article documents some of the apk own weight-loss measures before, are generic online search finishing

Based on official documents were thin

Reference article:
compressed code and resources

  • Enabling code compression
    build.gradle add the file similar to the following fragment:
android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

Every file will output the following table ProGuard building. They are stored at <module-name> / build / outputs / mapping / release / directory.

file name effect
dump.txt APK internal structure of all class files
mapping.txt To translate the original and obfuscated class, method, and field names
seeds.txt It lists the classes and members not be confused with the
usage.txt List tags removed from the APK
  • Compression resource
    compression resources shrinkResources property is set to true. gradle fragments as follows:
android {
    ...
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
}

In the above manner, apk reduced to 35967969KB, reduces the size of approximately 1M.

PS.
Before the project through the use of the way, but found open shrinkResources, there has been an increase in the OOM phenomenon. Although not explicitly associated with shrinkResources, but it is presumed relevance great. After subsequent reopening shrinkResources, need to do the test, a small portion of the user first gradation observed feedback log. Whether fully open in the decision.

Slimming use AndResGuard

Reference article:

  1. Android resources confuse AndResGuard instructions
  2. Installation package, minus 1M-- micro channel resources Android confusion packaging tools
    that program from the micro-channel, micro-channel configuration Gradle also provided, as follows:
apply plugin: 'AndResGuard'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.10'
    }
}


andResGuard {
    // mappingFile = file("./resource_mapping.txt")
    mappingFile = null
    use7zip = true
    useSign = true
    // 打开这个开关,会keep住所有资源的原始路径,只混淆资源的名字
    keepRoot = false
    whiteList = [
        // for your icon
        "R.drawable.icon",
        // for fabric
        "R.string.com.crashlytics.*",
        // for google-services
        "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"
    ]
    compressFilePattern = [
        "*.png",
        "*.jpg",
        "*.jpeg",
        "*.gif",
        "resources.arsc"
    ]
    sevenzip {
         artifact = 'com.tencent.mm:SevenZip:1.2.10'
         //path = "/usr/local/bin/7za"
    }

    /**
    * 可选: 如果不设置则会默认覆盖assemble输出的apk
    **/
    // finalApkBackupPath = "${project.rootDir}/final.apk"

    /**
    * 可选: 指定v1签名时生成jar文件的摘要算法
    * 默认值为“SHA-1”
    **/
    // digestalg = "SHA-256"
}

, Carried by the official document on ways to reduce weight-loss basis apk size to 33552298KB, about 32M. Reducing 2M, the effect is obvious.

so thin separation library

Reference article:
Build Multiple APKs

Android has a variety of chip architecture, common, such as x86, arm, arm64 and so on. If the apk itself so dependent libraries, these libraries so is bound to fit a variety of chip architecture, which supports different chips so libraries are often packaged together to release out the apk. This leads to the library so redundant. So do so separate library package is apk a solution to lose weight.
gradle fragments as follows:

android{
...
    splits {
        abi {
            enable true
            reset()
            include "x86", "armeabi-v7a", "arm64-v8a"
            // Specifies that we do not want to also generate a universal APK that includes all ABIs.
            universalApk false
        }
    }
}

If so this way the library itself is relatively large, weight-loss effect is obvious. Reduce application to the 26929590kb, about 26M.

Compress Pictures Resources

Reference article:
the Create WebP ImagesRF Royalty Free
PNG picture compression comparative analysis
android package itself will png lossless compression, and image resources is apk "obesity" is a big reason.
WebP image compression based on official guidelines can be very convenient to compress image manipulation in the AS.
FIG following steps:

2912789-b9e9bca9ae9a2988.png

The final will be the original AS png picture removed, leaving webp compressed.
Reduced to 26284239kb through the program apk, about 25M, before on the basis of reducing the size of 1M.

Another tinypng compression scheme with pngquant, but they need to provide their own packages call the method when batch processing. If you feel webp AS compression scheme presented does not meet the requirements, you can choose these two options.

Reproduced in: https: //www.jianshu.com/p/0fdf44f81263

Guess you like

Origin blog.csdn.net/weixin_34038293/article/details/91162838