Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android support library increases APK size a lot

Tags:

I'm using AppCompat support library in my Android project. AppCompat has plenty of drawables and resources which I don't use in my app. That unnecessary files increases my 900K app to above 2M, which I don't like.
Is there any way to exclude those files when creating the APK file? Or I should obfuscate the library in my code instead of making a dependency? I'm using Gradle in Android Studio.

Thanks

EDIT 1 I am using proguard already. but proguard can't know I don't want to have drawable-xxxhdpi or values-it for example.

EDIT 2 I am also using Android Lint, which can't help me, beacuse I don't access to lib's code directly, and android adds them when building the APK file.

like image 985
Keyhan Asghari Avatar asked Jan 09 '14 10:01

Keyhan Asghari


People also ask

Why is my APK size so big?

One of the simple ways to make your APK smaller is to reduce the number and size of the resources it contains. In particular, you can remove resources that your app no longer uses, and you can use scalable Drawable objects in place of image files.

Why does app size increase after installation?

Since apps are compressed when they're downloaded, it can make install sizes larger than download sizes. When an app has a larger install size, more space is required on a user's device to complete the installation.

What is the max size of APK?

Each expansion file can be up to 2GB in size. Users must run Play Store version 5.2 or higher to install 100MB APKs.


2 Answers

Starting from version 24.2.0, the v4 Support Library has been split into several smaller modules.

So, apart from using shrinkResources and proguardFiles, also make sure that you are using only the specific modules that your app needs. e.g.

If your app only uses Compat utils like NotificationCompat, ContextCompat or ResourcesCompat etc., use only the compat module as:

compile 'com.android.support:support-compat:24.2.0'
like image 186
User31689 Avatar answered Oct 03 '22 01:10

User31689


From Android Gradle Build System, since version 0.7.0:

New option on product Flavor (and defaultConfig) allow filtering of resources through the -c option of aapt You can pass single value (resConfig) or multiple values (resConfigs) through the DSL. All values from the default config and flavors get combined and passed to aapt. See "basic" sample. In the "basic" sample:

defaultConfig {
    ...
    resConfig "en"
    resConfigs "nodpi", "hdpi"
}

So, try the following to achieve what you asked for:

productFlavors {
    ...
    frOnly {
        resConfig "fr"
    }
    ...
}

Note that you might also want to include *dpi, port, land, etc.. as well.

Answer is from: Android Studio exports strings from support library to APK, thanks to Primoz990

like image 28
Keyhan Asghari Avatar answered Oct 03 '22 01:10

Keyhan Asghari