How to reduce apk size when using play services gc

2020-04-14 08:43发布

问题:

My generated signed apk size is 30kB, when I add play services gcm to gradle file the apk size increased to 800kB. by shrinking resource the apk size become 600kB.

How could I reduce app size further?

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.test.mytest"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            zipAlignEnabled true
            shrinkResources true
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])      
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
}

回答1:

from http://proguard.sourceforge.net/manual/examples.html#application

-optimizationpasses 3

-overloadaggressively

-repackageclasses ''

-allowaccessmodification

these might help. (in particular the 3 passses)



回答2:

At the moment that is the size of that dependency. The reason that the .apk gets an increase in size for every dependency is because your application requires those to function, and cannot depend on accessing it from a remote location, incase a device does not have an internet connection.

According to this, (and IntelliJ's comment), if you configure your proguard you can try to reduce the size a little.

Also consider the following: Shrink Google Play Services library for use with Google Analytics only.



回答3:

Looking at your build.gradle, you have already taken of Source code and resource optimization. The things you are missing is:

  1. Removing unused configuration with ResConfigs

    Many libraries like support/Google play services comes with string resources translate in too many languages. You might not be intending to support all of them. Using following way, you can control the size:

    android {

    defaultConfig {
        ...
        resConfigs "en"
    }}
    

    This will remove all the resources that are not meant for English.

  2. Sparse Configurations

    This is usually applicable for applications with large number of strings. In case of some features, you might be tempted to create strings under values-22/strings.xml. The problem with this approach is reserves space for pointers on every possible resource position. So even though the string is not present in new file, it still take space for pointer which is around 4 bytes of null data. You can use tool named ArscBlamer from Google to locate this sparse configurations.

  3. Image Optimization

    You can perform some image optimizations using WebP and Zopfli-compressed PNGs. You have to carefully weigh the downsides of using them. The details are available here

More details on these approaches is available here