Reducing android app (apk) size

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.


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.


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

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!).