How to reduce Android Apk size

2020-04-14 04:31发布

问题:

I am developing application which contains below dependency

Module level

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.google.android.gms:play-services:9.0.0' **// for GCM**
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.squareup.picasso:picasso:2.5.2' **//Rounding and displaying image from URL**
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.github.lecho:hellocharts-library:1.5.8@aar' **// For bar graph** 
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}
compile 'com.google.firebase:firebase-core:9.0.0' **// Analytics**

}

Project level

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.2'
    classpath 'com.google.gms:google-services:3.0.0'
    classpath 'io.realm:realm-gradle-plugin:1.0.1' // Database
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

my

/Assets /150kb using font aweasome file.

/java - 644 KB

/res - 740 KB (used high resolution image 1920*1280 size 220kb compress with tinypng)

Already applied ProGuard. delete unused file and codes.

Question

Still my app size is 7.03MB

How can i reduce app size? i don't know why this is 7.03MB

Is there any calculation which give size that used by dependency in apk?

Like google play service occupied around 200kb in apk.

回答1:

In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

Replace

compile 'com.google.android.gms:play-services:9.6.1' //or the version you are using

with specific api's you need:

com.google.android.gms:play-services-gcm:9.6.1
// and other if you need

List of dependencies for specific GMS apis can be found here.



回答2:

Instead of compiling full google services package, you can just call the packages that you need for compiling. There is a link below, kindly check the same https://developers.google.com/android/guides/setup

Before using the GCM, I suggest you to use Firebase Cloud Messaging, as google is shifting its base from gcm to firebase.

Link for Firebase Cloud messaging:-

https://firebase.google.com/docs/cloud-messaging/android/client

Points which can help you:-

You can use FontAwesome Icons in designs instead of images, that has a great impact on Application Size. Provide a specific size for each layout in dimen.xml for every resoultion.

If you are using image then you have to make sure that you are not inserting the same image for different screen. Try to use builtin icons of android as much as possible



回答3:

There are two useful techniques which you might be missing

  1. Optimizing Dex and Resources along with proguard

    This results in smaller code so should be used in release build only. Otherwise it will cause difficult during debugging. It can be achieved as follows:

    android {

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

You also need to focus on removing unused string from dependent SDKs.

  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.



标签: android apk