“This is the seventh day of my participation in the Gwen Challenge in November. Check out the details: 2021 Last Gwen Challenge”

preface

This paper summarizes the app performance optimization schemes used in the work.

Flow optimization

  1. Network image replacement WebP
  2. Third-party library detection reports upload only in wifi environment
  3. Resource download prompt whether to use traffic download
  4. Merge HTTP requests to reduce the number of HTTP requests and avoid double requests
  5. Formulate a reasonable HTTP request data format and return data format, so that there are no redundant fields in the request data, and do data compression when necessary
  6. Urls are sent using short links
  7. Expand the local image cache
  8. Maps can be cached locally
  9. Use GZIP to download a large amount of data

Power optimization

  1. Reduce wake up device operation and reduce timer
  2. Reduce I/O operations. Such as database operation, SP operation (there is every page switch), file operation, etc
  3. A lot of calculations, consider moving to the background processing, such as pinyin analysis, sorting
  4. Charging status Processing power consuming tasks, such as data migration, processing, and data backup
  5. Reduce senser usage, such as shake, spin, recording, GPS usage scenarios
  6. Reduce animations and transparent views, and some low-end machines can even turn animations off
  7. Simplify broadcasting and simplify high-throughput algorithms
  8. Merge HTTP requests to reduce the number of HTTP requests and avoid double requests
  9. Formulate reasonable Http request data format and return data format, so that there are no redundant fields in the request data
  10. In the program that requires network connection, first check whether the network connection is normal. If there is no network connection, then there is no need to execute the corresponding program
  11. Use GZIP to download a large amount of data
  12. To reclaim Java objects, especially large Java objects, use the reset method
  13. If the positioning requirements are not too high, try not to use GPS positioning, and may use wifi or mobile network cell positioning
  14. Try not to use floating point arithmetic
  15. Information such as screen size can be obtained using caching techniques without multiple requests
  16. Use AlarmManager to start scheduled services instead of scheduled tasks (such as heartbeat services) using sleep mode
  17. Reduce high frequency flushes
  18. Use WakeLock with caution

Apk thin body

Resource file optimization

  1. Use a set of resources (e.g., take only 1080p resources and put them in xxhdpi directory)
  2. Enable shrinkResources to remove unwanted resources
android {
    buildTypes {
        release {
            shrinkResources true}}}Copy the code
  1. Delete unnecessary language resources
// There is no internationalization support plan
android {
    defaultConfig {
        resConfigs "zh"}}Copy the code
  1. Use Tinypng lossy compression
1.The Android packaging itself does lossless compression of PNG, which is very good and highly recommended. Tinypng's official website: < HTTP://tinypng.com/>
2.Automatic compression via tiny plugin: Download the plugin BiuCopy the code
  1. Overwrite large images in library 3 (some library 3 references large images that are not actually used by us, consider overwriting them with 1×1 transparent images)
  2. Emojis are downloaded after installation
  3. Use the wechat resource compression and packaging tool

Wechat resource compression and packaging tool achieves the goal of reducing APP by using 7ZIP and short resource names, which is highly recommended. Details: Android resource obfuscation tool instructions; Principle introduction: Install Package to reduce 1M – wechat Android resource confusion packaging tool

// It is recommended to enable 7zip, pay attention to the whitelist configuration, otherwise some resources will not be found, the official release AndResGuard to Gradle:

apply plugin: 'AndResGuard'
buildscript {
    dependencies {
        classpath 'com. Tencent. Mm: AndResGuard - gradle - plugin: 1.1.7'
    }
}
andResGuard {
    mappingFile = null
    use7zip = true
    useSign = true
    keepRoot = false
    // add <your_application_id>.R.drawable.icon into whitelist.
    // because the launcher will get thgge icon with his name
    def packageName = <your_application_id>
    whiteList = [
    //for your icon
    packageName + ".R.drawable.icon".//for fabric
    packageName + ".R.string.com.crashlytics.*".//for umeng update
    packageName + ".R.string.umeng*",
    packageName + ".R.string.UM*",
    packageName + ".R.string.tb_*",
    packageName + ".R.layout.umeng*",
    packageName + ".R.layout.tb_*",
    packageName + ".R.drawable.umeng*",
    packageName + ".R.drawable.tb_*",
    packageName + ".R.anim.umeng*",
    packageName + ".R.color.umeng*",
    packageName + ".R.color.tb_*",
    packageName + ".R.style.*UM*",
    packageName + ".R.style.umeng*",
    packageName + ".R.id.umeng*"
    ]
    compressFilePattern = [
    "*.png"."*.jpg"."*.jpeg"."*.gif"."resources.arsc"
    ]
    sevenzip {
    artifact = 'com. Tencent. Mm: SevenZip: 1.1.7'
    //path = "/usr/local/bin/7za"}}// An AndresGuard/resGuard Task is generated that automatically reads the Release signature for repackaging.
Copy the code

Code depends on library optimization

  1. Remove useless tripartite libraries
  2. Avoid selective copying of used function classes by importing an entire tripartite library to use a small number of functions
  3. Remove duplicate libraries with similar functionality
  4. Lint checks to remove useless files and invalid code

other

  1. If minifyEnabled is enabled, each SERVICE SDK confuses its own module independently and keeps the confused Mapping file
  2. Webp, VectorDrawable image replacement