40

I would be now publishing my first app on Google play store. I have already compressed images used in my app. And I have some questions regarding the app size.

If the size of app is less that 2 MB then there are less chances that user will uninstall the app, is my this statement true for apps belonging to education field ?

However, When I see my apk file in windows it shows 3.10 MB but when it gets installed in device as viewed from App info it shows:

Total............................8.68 MB
App..............................7.02 MB
USB storage app..................0.00 B
Data.............................1.66 MB (perhaps it is the size of sqlite db + ttf's)
SD card..........................0.00B

So, Why do I see that much increase in my app size, Can it be minimized ?

And, I am using 4 libraries (jars) in my project which are in Android Private Libraries, but these library have their copy outside this folder also.

Is it safer to delete them, & deleting them can help to decrease apk size?

Also I have visited many web pages describing what Proguard does, I truly understand how Proguard reduces size of apk, it shrinks, optimizes, and obfuscates our code by removing unused code and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer. But I do not know How should be my proguard-project.txt should look like ?
I am using 4 libraries in my app namely easyfacebookandroidsdk_2.3.jar,android-support-v7-appcompat.jar,google-play-services.jar & android-support-v4.jar.

Currently my proguard-properties.txt looks like this & also it do not uses WebView

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;  
#}
-keep class com.facebook.** { *; }
-keepattributes Signature

So, what lines should I add in my proguard-properties.txt so that it can remove unused references,classes,etc., in order to reduce the apk size ?

Using AndroidUnusedResources.jar available at https://code.google.com/p/android-unused-resources/ finds unused resources in android project.

I think this is the same thing which proguard has already done ? Or should it also be used after enabling proguard ?

Also you can mention in your answer anything that is missing which you feel should be shared.
Thanks in Advance...

Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
  • 4
    Where did you hear the 2MB claim? I would release the app, and always try to reduce the footprint of the app. If a user finds your app helpful, then they won't uninstall it. – Christopher Rucinski Aug 03 '14 at 04:26
  • 3
    You are right ,If a user finds app helpful, then they won't uninstall it. 2 MB was my personal opinion because When the user is running low on disk, he sometimes deletes the app that are taking utilises memory, Thanks though – Vivek Warde Aug 03 '14 at 05:12
  • 1
    If the user runs out of storage on his device, his choice of apps to remove will be the ones he doesn't use or need anymore. My advice would be similar to @ChristopherRucinski above. Make an app that's useful that users will not want to uninstall. Also 2-8 MB for an app storage is a drop in the bucket. – selbie Aug 03 '14 at 05:26
  • 2
    I did provide some other ideas that you might find useful in my answer below. Like I said, you might want to upload a few of the pictures so we can see how to decompress them more in ways you might now be aware of. Also, I did say that you can move the data to an SD card! That is probably a good solution to go with until you can find good ways of reducing the APK size – Christopher Rucinski Aug 03 '14 at 05:26
  • You can also try this: [https://stackoverflow.com/questions/40154269/reduce-apk-size-android/49472760#49472760](https://stackoverflow.com/questions/40154269/reduce-apk-size-android/49472760#49472760) – Aashish Kumar Mar 25 '18 at 05:20

16 Answers16

44

So, Why do I see that much increase in my app size, Can it be minimized?

Almost solely because of your res folder images! You should have around 4 copies of each image in the drawable-mdpi, -hdpi, xhdpi folders. The only difference is that theu are all different sizes.

Is it safer to delete them, & deleting them can help to decrease apk size?

Chances are there aren't multiple copies of those jar files! Just one copy with links to it!

Proguard

Yes, what you said was right about Proguard. However, realize that you are talking about text files here. They are really small. I mean really small in size (kilobyte-wise). Your problem is something in the megabyte (MB) size. That is images!!!!

Repeat. That is images!!!!

Enabling proguard will save a little bit size-wise.

Also you can mention in your answer anything that is missing which you feel should be shared?

Yes, these are all more important in your case. Please read and apply these. Good luck!

If a user finds your app helpful, then they won't uninstall it; however, there are several things you can do

  • Can you move images to the web and async them into your app?
  • Make sure your files are completely compressed as much as possible. Use pngquant or tinypng.
  • Check for any repetition in the images. You might be able to use 9-patch for it.
  • You might be able to make your own drawables for certain images.
  • Allow users to move data to an SD card.

  • [EDIT] After the app has been in the Play Store for a while, check the Developer Console and see which screen size/density is used the least. Remove that drawable folder from your app! This could reduce the size a good amount! But, check my edit below for the cons of this approach!!

  • [EDIT 2] In Windows Explorer, check the folder size for your whole res folder, and the whole java folder. If your java folder's size is small, then all your effor to reduce APK size should be what I state above.

You might want to show a few of the images, as that would allow us to see what you are dealing with

EDIT

@Ashwin N Bhanushali Has a great answer here for the part of the question I did not answer, but he basically told you that there was nothing you could really do except what I said, plus an idea or 2 more.

I did have another idea that was based on one of his ideas that would be even better, because Android Studio does not include the drawable-ldpi folder in the project anymore. So it would only be helpful if you use Eclipse, mine expands

  1. Release the APK into the Google Play Store after making the recommended fixes
  2. After some time has passed -- a week, a month, etc -- you can check the stats of your application in the Google Play Developer Console.
  3. Look for the screen size buckets(drawable-mdpi, -hdpi, -xhdpi, -xxhdpi) that are used the least! You could remove those buckets from your app altogether!

This works because pictures take up way more bytes than text files. And all those drawables have the same images in them (just different sizes). Removing the images from less used buckets can save a lot of space!!

Take note that removing those buckets from your app will mean that your app will have to do more work on those phones that use the removed buckets. The app might be slower for those users; however, the app size can be reduced more.

EDIT 2

Text files take up almost nothing. Take a look in the file system for your project!

In the picture below. Check the size of...

  • the java folder
  • the res folder

enter image description here

In your case, you will see that the java folder is probably really small in size (10 through a few hundred KBs); however, you will see that the res folder is really large in size (several MBs!).

That means that any compression, or reduction in your code base will be very small and not worth it!!

Therefore the best way to reduce your APK size is to follow my instructions above!

Edit 3

Here is a test that you can do right away and see the absolute maximum (plus more) that tools like Proguard, and AndroidUnusedResources.jar will reduce the size of the APK file!

What you have to do is remove ALL code from your project except for the class declarations. Do the same for Abstract classes, Interfaces, Enums, etc... Your project structure should stay the same, and you should not be using any external libraries at all then.

  1. So you will have something like this for all of your classes...

    public class CustomClass {
    
        private String variable; // List of variables
    
        public CustomClass() { ... } // List of constructors
    
        public getVariable() { ... } // List of Assessors
    
        public setVariable(String val) { ... } // List of Mutators
    
        private String helperMethods() { ... } // List of helper and other methods
    
    }
    
  2. You should remove all the code inside of the classes, interfaces, enums, etc to look like the following; however, leave all your images alone!! ...

    public CustomClass {
    
        // Everything is removed!! No libraries used, nothing. Unless certain classes 
        //     require some methods like Activity. Those methods should be empty though!
    
    }
    
  3. Now compile and build your project.

  4. Check the new APK size to your current APK.

    • This will be the smallest possible compressed size file you can achieve with your code. Yes, your program wont do anything, but that is besides the point. You care about size here, and this is ONLY a test.
  5. Install the app on your phone, and check the new app size to your current app size

    • This will be the smallest possible uncompressed size file your can achieve with your code. Yes, your program wont do anything, this is just a test.

If you notice that the 2 apps are still around the same size, then spending all your time with Proguard and AndroidUnusedResources.jar is meaningless at this stage. This is a test to see where you would spend your personal resources in app size reduction.

Community
  • 1
  • 1
Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58
  • 4
    Also - Eclipse will often add the android-support library to the project by default. If you aren't using any of these support classes in your code, simply take this jar file out of your project. It will reduce your app's foot print by 600KB. – selbie Aug 03 '14 at 05:34
  • ***AndroidUnusedResources.jar** available at https://code.google.com/p/android-unused-resources/ finds unused resources in android project. * **How is it different from proguard?** – Vivek Warde Aug 13 '14 at 16:18
  • As you said...`**Proguard** reduces size of apk, it **shrinks**, optimizes, and obfuscates our code **by removing unused code** and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer.` – Christopher Rucinski Aug 17 '14 at 08:49
  • I have not used AndroidUnusedResources.jar; however, it seems like `**AndroidUnusedResources.jar** reduces size of apk, it **shrinks** our code **by removing unused code**. The result is a smaller sized .apk file that does not have unneeded library code.` – Christopher Rucinski Aug 17 '14 at 08:51
  • I just added Edit 3. Check it out. This will be a simple test that you can do to see the size of your codebase + libraries vs. the image files. – Christopher Rucinski Aug 17 '14 at 09:28
  • It would actually be beneficial for you to post the size statistics for some of my suggestions - especially edit 3. That would be very beneficial for the community as a whole - I can edit and put specifics down for other people that see this question and my answer. Also, if you don't understand anything, or don't understand how it will help you make decisions, then let me know. – Christopher Rucinski Aug 17 '14 at 09:35
  • You can use [AndResGuard](https://github.com/shwenzhang/AndResGuard) to reduce the apk size. It's very easy to use. – Sim Sun Aug 03 '16 at 19:42
  • Just added Edit 4. i think it can be of used to decrease apk size. – Aman Verma May 22 '17 at 21:35
3

So, Why do I see that much increase in my app size, Can it be minimized ?

The .apk-file

An .apk-file is not magical at all. It's just a bundle of files which represent the content of an Android application. If you open it in a archive-tool (like 7Zip), you can browser and extract it's contents.

Now this apk file is extracted during installation hence there is increase in the size of the App.

Some more detail regarding compilation

What happens to the .java-files?

Well, first they are normally compiled by an installed JDK implementation. After they are compiled (to .class-files), the dx-tool from the Android SDK then cross-compiles those "normal" java-classes into Dalvik-Bytecode.

This "special" java-code is then interpreted by the DVM (Dalvik Virtual Machine), which is based on the opensource JRE-implementation Apache Harmony.

What happens to the resources i put into the /asset-directory?

Android offers the /assets-directory to add some binary raw-files (e.g. a SQLite Database). Files which are put into this directory are not compiled or optimized.

If you put your files into this directory, this is the kind of behavior you would expect from Android.

What happens to the resources i put into the /res/raw-directory?

Like the /assets-directory, you can also put binary (or other) raw-files in here (e.g. HTML-files for the Help-page). These files are compiled/optimized (if possible).

What happens to the Manifest and the other XML-files?

The Android-Manifest and also the other XML-files (Layouts, Strings, etc.) are stored and "compiled" into a binary XML-format. This is a speed-optimization.

So you got the answer why app size is increased after installation.

Now Is it safer to delete them(Android Libraries), & deleting them can help to decrease apk size?

You can remove google-play-services.jar since it is required only in development environment it's not required in runtime environment.Prompt user to install it from the play store.

Coming to Proguard :: It will remove the unused java classes and will do the code obfuscation.It will not remove unused resources like layout xmls,drawables etc. So my opinion is to run the tool to remove unused resources.

Below are some tips to reduce the size.

You can remove drawable-ldpi folder if you are using it.Since there are no more devices with ldpi screen resolution.

Reduce the use of image drawables(i.e .png or jpg files).Use xml drawables and 9-patch drawables.

Remove any debug info related code.

Avoid code duplication.Follow the principal of Don't Repeat yourself.

I think this is enough to reduce app size in your case.

Ashwin N Bhanushali
  • 3,872
  • 5
  • 39
  • 59
  • Can you please explain how to remove google-play-services.jar from release (Android Studio)? Can't find that, but play-services client is so big to include it in 0.8MB sized APK... Thanks. – RusArtM Dec 08 '14 at 14:43
  • add google-play-services as a provided dependencies in your gradle script. – Ashwin N Bhanushali Dec 09 '14 at 05:11
3

Following are the ways to reduce the app size. In detail is explained in the following link.

https://medium.com/@fahimsakri/put-your-apks-on-diet-cc3f40843c84#.m860q8s1u

  • Proguard
  • vector drawables
  • Apk splits
  • Optimize png images
  • Remove unused resources
  • 9-patch images
  • Compress Jpeg images
  • Remove debug information
  • Avoid duplications
  • Use lint extensively
  • Reuse resource whenever possible
  • Recommended Media formats
Fahim
  • 12,198
  • 5
  • 39
  • 57
2

For those of you doing much smaller projects, consider removing all the app-compat libraries. You may be better off making multiple versions of your app for specific versions of the API. I've seen a 47Kbyte app balloon to 3Mbytes just by adding compatibility libraries.

Summary: to save space, avoid unnecessary libraries, even ones provided by Google (and included in most templates!).

SMBiggs
  • 11,034
  • 6
  • 68
  • 83
1

Try enabling http://developer.android.com/tools/help/proguard.html , even without obfuscation. It will remove unused Java code from your dependencies. See if you have unneeded dependencies - JARs that you've added but aren't using Check your resources for large images or other files. You may want to change their format or resolution, or remove them if they're not in use.

Vaishali Sutariya
  • 5,093
  • 30
  • 32
1

In most of the cases app size is propotional to the resource file(images and other stuff) that you use to create an application.Always consider the following.

  • Try using minimal images.(In most cases ,there is not need to add images in the Ldpi folder) .
  • Use vector images instead of putting images for all screen resolutions .One way is to use SVG ,9 patch
  • Try to use xml drawable to create generic images like square,rectangle circle,borders etc instead of .png images Try some image compression techniques.
  • Try fetching large images from Api rather than putting it in the res folder.(Download only when needed).

Library imports

  • Try to remove unwanted libraries while creating a build for appstore.
  • If you need a particular functionality in a alibrary then try to extract the required class files and use it rather than importing the whole library jar.
  • Clean your class by removing unused import (although it won't have much effect on size but it will make things more simple and straight).In eclipe select the src folder->rightclick->source->organize imports
Abx
  • 2,852
  • 4
  • 30
  • 50
1

It is one option that you compress all your PNGs for that you can go to https://tinypng.com/ .

After that remove unnecessary library imports for example it is not require to import support library v4 if you already imported v7 for appcompat.

And finally after completion of signing procedure of apk use zipalign tool that is shipped with android-sdk for more details refer http://developer.android.com/tools/help/zipalign.html.

That's all i know.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Deep Shah
  • 1,334
  • 3
  • 20
  • 41
1

Generally, in ProGuard, you can use the following to shrink your APK (this is in your module's build.gradle file):

...

android {

    ...

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

    ...

 }
 ...

Specifically, look at minifyEnabled true and shrinkResources true:

minifyEnabled is responsible for obfuscating and shrinking your code files (e.g. Java). It is the main feature of ProGuard, and helps to shrink your APK as well as making it difficult to reverse engineer.

shrinkResources is used to remove unused resource files (such as images and other assets). For example, if you are compiling with an Android library, but you are not using some images in that Android library, they will not be included in the final build. There is a good Android Developers video about it here.

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
1

Now you can also use vector drawable in your app to remove multiple drawables for different screen sizes and reduce APK size. see: https://stackoverflow.com/a/33984100/5305331

Community
  • 1
  • 1
1

You can try another answers and they are very good but there is another small trick for reducing massive amount of apk size.
We use many libraries in our projects. some of them developed in Native language ( c ) and some of them developed for a specific version of architecture.

We have some architecture :

  • armeabi : Uncommon
  • armeabi-v7a : Common
  • arm64-v8a : Common
  • mips : Rare
  • mips64 : Rare
  • x86 : Common
  • x86_64 : Uncommon

You can filter target architectures and remove useless options. for example :

buildTypes {
    debug {
        ...
    }
    release {
        ...
        ndk {
            abiFilters "arm64-v8a", "armeabi-v7a", "x86"
        }
    }
}
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
0

First of all look in to your drawables.

  1. If you are using a PSD to get the images for the app, those images might not be optimized for web and devices. You Photoshops "Save for Web and Devices" to optimize the images. You can reduce lot of kbs from this.
  2. Next, see if you can replace images with XML drawables. For example, buttons and background can be done using XML
DanKodi
  • 3,550
  • 27
  • 26
0

ProGuard by default applies its shrinking, optimization, and obfuscation -- that's its main purpose. You do have to enable it in your Android build, by editing the documented ProGuard line in your project.properties. It enables ProGuard (with or without optimization) for release builds with Ant and Eclipse. With Ant, you'll see ProGuard output in the build log.

See the Android documentation > Tools > ProGuard.

You should then check your processed application. You may need additional -keep options, e.g. possibly for Google Play Services. The less you can keep, the smaller your application will be.

Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
0

The reason you see the increased file size is that APK's are compressed files. Use of Proguard can help - here's a good resource:

http://developer.sonymobile.com/2012/01/31/tips-for-reducing-apk-file-size/

However, I can assure you that you can get millions of downloads without being file-size sensitive. Both from my personal experience but also from looking at Google Play. There are thousands of apps with millions of downloads where the file size greatly exceeds 2MB. In fact, there are many that are around 20MB. In other words, do what you can to keep it small, but don't worry that it will impact use or downloads.

Jim
  • 10,172
  • 1
  • 27
  • 36
0

There are two things I can point out from experience.

If you use eclipse to create apk file then the resources are duplicated (atleast it happened in my case), you can try using ant and build.xml to create the apk and compare the size. Also look into aliasing the resources.

0

Re-compress APK/ZIP as high, it will save 30% more then normal apk build by Android SDK, useful if large number of text files but not if large number of images.

Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
kdm6389
  • 11
  • 3
  • How do I do that? I tried renaming apk to zip then decompressing into a folder then compress that folder back to zip and rename zip to apk but it wasn't installable on android device – Korayem Aug 28 '16 at 16:28
  • Found this http://stackoverflow.com/questions/25425172/reducing-apk-size-by-re-zipping-its-content – Korayem Aug 28 '16 at 16:30
0

Check my blog Different ways to reduce apk size

Main Points are :

  • android.enableR8=true // enable New R8 Code Shrinker
  • minifyEnabled true && Add Proguard Rules
  • Examine Your APK Using Android Studio’s APK Analyzer
  • Enable Resource Shrinking
  • Convert Your PNGs, JPEGs, and BMPs Into WebP
  • Avoid enumerations and use @IntDef annotation
  • Use aaptOptions { cruncherEnabled = false } to compress images
  • Use Remove Unused Resources android studio built-in function to remove all the unused resources in the application
  • Use Code Cleanup android studio built-in function to remove

Note: Go enable it! (*just double and triple check everything works afterwards)

Sai's Stack
  • 1,345
  • 2
  • 16
  • 29