47

Help!

When I install my app on the phone to test, it is showing up to be a HUGE size, 11.35 MB. It is a very simple app that lets user browse through fun-facts. The only reason I can think of is that there are 14 JPEG files in the drawables which serve as background images of the fun-facts. The average size of these is about 500 KB.

I'd like to trim the size of my app, so as not to use up the precious resources on the user's device. Other than just getting rid of the pictures, are there ways to optimize the size of apk file?

EDIT: The pictures are photos taken by me using the Android phone itself.

OceanBlue
  • 9,142
  • 21
  • 62
  • 84
  • 5
    try to ONLY use PNG format images in your android applications! – james Sep 28 '10 at 17:34
  • 4
    binnyb, I tried to save the jpeg file in png format. The png file was much bigger. So not sure how using png will be better here. – OceanBlue Sep 28 '10 at 19:51
  • 2
    This is a very old thread, I know, but it came up on my search and I wanted add this: When you first convert a .jpeg to a .png, the png will probably be much larger. But png's are very 'squishy' and easily compressed. Easy to use services like TinyPNG.com can dramatically reduce image size, especially if your image has large blocks of similar colors. – TBridges42 May 28 '15 at 13:35
  • 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:21

23 Answers23

36

Other answers mention shrinking images. You might also consider trying ProGuard to shrink your bytecode. Here's an article on applying ProGuard to an Android app.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • Shrinking the bytecode would have a barely noticable effect on APK size, especially in comparison to cutting down the size of the 10MB+ of JPEGs. – Christopher Orr Sep 28 '10 at 18:09
  • 11
    @Christopher: other answers have already mentioned shrinking images, but nobody has mentioned shrinking code, so I'm giving the OP another tool to consider. – Mauricio Scheffer Sep 28 '10 at 18:20
  • 1
    I see, but I guess nobody mentioned optimising using ProGuard because it would have virtually no benefit for an app like this. – Christopher Orr Sep 28 '10 at 18:33
  • Thanks for the tip! The code in this app is pretty small & straightforward, but I'll keep this in mind for future applications. – OceanBlue Sep 29 '10 at 14:24
24

I would recommend that you compress the .jpg files as much as possible, this should greatly reduce the size of your .apk file. A tool such as Paint.NET which is free should help you do this. It has great resizing options.

Nate
  • 30,286
  • 23
  • 113
  • 184
  • 2
    I would say *reduce* the JPEG size (i.e. JPEG quality) rather than *compress* them. JPEGs aren't very squishy. – Christopher Orr Sep 28 '10 at 18:11
  • Good point, but depending on the level of compression in the images, there could be room for additional compression in addition to reducing the size. – Nate Sep 28 '10 at 18:14
  • Nate Bross, Christopher, thanks. I installed Paint.NET. (great tool!) Both reducing the quality while saving OR Resizing option, reduce the size while not significantly altering the picture. I'm not sure about the *compress* option, how to do that (I'm assuming you don't mean compress to a .7z or something). But this will work for me. – OceanBlue Sep 28 '10 at 19:55
  • 1
    Most people see a "quality" setting in JPEG images, some correctly call this compression. It has to do with how much information you are willing to loose. When you reduce the "quality" you are simply letting the JPEG algorithm decide what pixels it can guess the color of. More guesses, smaller size. – Cycles McHurtz Sep 28 '10 at 20:03
  • @Tom Slick. OK, that makes sense. Good info. Thanks! – OceanBlue Sep 29 '10 at 16:54
  • See [Tools for JPEG optimization?](http://stackoverflow.com/questions/985725/tools-for-jpeg-optimization) for some other suggested tools. – Joe Oct 06 '13 at 01:15
13

Make sure that your jpg's aren't stored in any higher resolution than necessary. A nice Android phone has a screen resolution of 1920x1200 or 2560x1440 (circa 2015)800 x 480 , so your backgrounds shouldn't contain any more pixels than that (unless your app supports some kind of zooming). Also, are the backgrounds photographs? If not, you may find that using a vector based image format like svg, or one with a dynamic palette like gif, will reduce the file size even more.

Dana the Sane
  • 14,762
  • 8
  • 58
  • 80
  • *voteup* The backgrounds are photographs indeed. I did follow your advice while using the the tool another poster suggested & brought down the pixel sizes to about 800 X 480 . Thanks for the answer. – OceanBlue Sep 28 '10 at 20:00
  • Yes, you're right. I've updated the question to be more accurate. – Dana the Sane Sep 29 '10 at 21:06
12

UPDATE

With the introduction of Support Library 23.2, now you can replace many of your Image assets with SVG ( w/ or w/o animations) to reduce the apk size further.

ORIGINAL

This could be a late reply but might be helpful for future users.

Refer this link. To summarise the page, it mentions below key points that will help reduce the apk size in totality:

  • Use ProGaurd
  • Remove any debug info you have in the app ( statements such as Log.i()). They can be wrapped in a condition which is only enabled while testing out the application.
  • Use recommended media formats
    • Image: PNG orJPG
    • Audio: AAC
    • Video: H264 AVC
  • Compress images using OptiPNG or PNGCrush
  • Use 9patch to scale images
  • Find unused resources using this tool and remove them.
  • Avoid using multiple resources to achieve the same functionality. The resources do not only limit to images but extend to APIs. At times a singular API can provide multiple results instead of using two or three different APIs. Duplicated strings and assets are also a waste of space.
Vaibhav Singhal
  • 888
  • 9
  • 13
8

Convert to WebP which will significantly reduce the app size

It reduced the app size of my app which was 40 mb to 25 mb

Steps:

  • Right click on res
  • convert to WebP

What it does: It reduces the size to png to WebP format. Quality is also not destroyed


enter image description here

Devrath
  • 42,072
  • 54
  • 195
  • 297
6

Beyond optimizing images, I also found it useful to verify the Support Libraries you use. I have a relatively simple application targeting platforms with API >=10, still my APK ended up being 2.2M, or after using ProGuard, 1.4M. When I looked into the APK, the code was 1.5M and there were a lot of additional resources included (abc_*) I knew nothing about.

Then I found this: https://developer.android.com/tools/support-library/features.html

Turns out I did not need appcompat-v7, only support-v4, and making this change to my dependencies reduced the APK size to 1.7M (0.9M with ProGuard). Understandably, the Support Libraries carry a lot of extra (code and resource), so making sure you use only the ones you need might help.

(Even though -0.5M is not significant for a 11M app, I am posting this here because I kept ending up on this page while searching for a solution...)

sfThomas
  • 1,895
  • 2
  • 18
  • 26
4
  • Use tinypng compress your project's png or jpg format image files, It can greatly reduce the size of the image without loss in image quality;
  • Some images use tinypng compress may distortion, you can convert
    these images to webP format, use 智图 or iSparta can convert other format to webP;
  • Use Lint check no use resources and delete it;
UperOne
  • 195
  • 10
4

Four Ways and your app size will become from 11.35 MB to near about 5 MB. First, check the app components via App Analyzer (an inbuilt feature of Android studio), Simply click on Build on the top and click Analyze APK. It will show you everything what components apk is using.

  1. Enable Proguard
  2. Enable ShrinkResource
  3. resConfigs (Name it, like if your app is in only one language eg English then simply specify resConfigs "en")
  4. Convert all Images from .png or.jpg to webp (Most Important).

Steps to achieve these steps.

1 & 2. For Proguard and ShrinkResource

In gradle, simply add

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

        defaultConfig {
        applicationId "com.goparties.gpuser"
        minSdkVersion 19
        targetSdkVersion 27
        resConfigs "en"
    

    }

  2. TO convert all Images from .png or.jpg to webp

Very easy process: Just right click on the drawable folder(drawable,drawable-hdpi, drawable-xhdpi etc) and click on convertToWebP (last option).

Min SDK version should be 18.

Here we go: Now analyze your app size.

Bingo !!!!!!

Minkoo
  • 439
  • 1
  • 3
  • 16
3

One more thing to add on image file size - different algorithms can have a significant effect on the final size. I found that Yahoo's www.smushit.com is a lot more effective (at least for .png) than compressors and codecs I have on my computer right now.

ProjectJourneyman
  • 3,566
  • 1
  • 27
  • 37
  • Interesting. Thanks for the info! – OceanBlue Sep 19 '11 at 14:19
  • Unfortunately Yahoo shut down the hosting for Smushit with little notice. There are a number of alternatives, such as https://kraken.io/web-interface, https://tinypng.com/, http://nitrosmush.com/. I haven't done a comparison, but did see one site favoring TinyPNG over Kraken for final file size. – ProjectJourneyman Feb 19 '16 at 07:23
2

There are two things I can point out from experience.

  1. 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.
  2. Also look into aliasing the resources. The link for it is here.
omermuhammed
  • 7,365
  • 4
  • 27
  • 40
  • Can you elaborate on resources being "duplicated"? – Christopher Orr Sep 28 '10 at 18:12
  • In my case I had unzipped my apk and found the contents of res folder (drawable, layout folders etc) in top level directory, AND the res folder containing all the assets, thus they were being packaged in twice. I thought that this must be something unique to my Eclipse workspace. And when I started doing commandline builds with Ant, it all worked out and the res folder was packaged in once. Thats the reason I asked you to look into it. – omermuhammed Sep 28 '10 at 18:28
  • @omermuhammed: make sure you do not have assets folder or resource folder in project settings as the source folder. – Timo Dec 19 '11 at 07:18
2

I think this post would give you a better idea on all the possible methods to use to reduce your apk size by a huge margin.

I will give you an excerpt of the same.

  1. Use only the required libraries from Google Play Services.
  2. Apply ProGuard to your app.
  3. Minify and shrink.
  4. Adopt vector drawables.
  5. Use the android lint.
  6. Split your apk based on the architectures
  7. Adding resConfigs in your gradle files to specify localization languages.

    This strips away all other string files that could’ve been added by other libraries in languages you don’t even support.

Capt
  • 261
  • 4
  • 11
2

I know i am late here to answer this question but i reduce my app size using below techniques so i want to share these things with all.

1)- Use WebP images instead of Jpeg images, it will provide huge impact on apk size if you are using multiple images.

2)-Use VectorDrawables if you are using simple small icons in your app.

3)- Use View's tint property in xml to avoid multiple same icons but Different in color.

search DrawableTint and TintableImageview

4)- Use 9-patch images and avoid duplication of image or anything in the app code.

Below are the links to refer to reduce APK size.

https://developer.android.com/topic/performance/reduce-apk-size.html

https://developer.android.com/studio/build/configure-apk-splits.html#configure-split

https://developer.android.com/training/multiple-apks/index.html

anddevmanu
  • 1,459
  • 14
  • 19
2

WebP image format: provides lossy compression (like JPEG) as well as transparency (like PNG) but can provide better compression than either JPEG or PNG

Convert images to WebP

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
1

Along with the above most upvoted answers, I would like to answer with latest tools from Google's Android Studio.

Google recently has introduced the Android App Bundle in which playstore automatically delivered optimized version of apk to end devices. Now developers just has to generate Signed Bundle, and upload to play store, dev job is done. Don't forget always use SVG images.

Also please have look into Deliver Features On-Demand with Dynamic Features

enter image description here

Suresh Maidaragi
  • 2,173
  • 18
  • 25
0

Here is what you can do for reduce build size by Images (Also can work for iOS) Here I am sharing the great tool called “OPTIPNG ” (you can download from here )which will help us to reduced the build specially by using Images, It will reduced the Image size for PNGs we are using without degrading quality (Resolution and color) of the image.

Example – If your image size is off 698 KB then It will simpley reduced size to 564 KB

Here is the execution steps for OPTIPNG 0.7.5

1) Terminal -> CD /YourLocal path of OPTIPNG

2) type “./configure”

3) type ”sudo make install”

Intallation should be done now

4) type ” optipng /your image path

You will get result in byte and can also check your size

Kirtikumar A.
  • 4,140
  • 43
  • 43
0

You could also try http://www.webresizer.com/resizer/ Its an online tool, Did a pretty good job for me.

Ash
  • 1,391
  • 15
  • 20
0

I used Trimage image compressor for compressing images and reducing the size of apk.It has good compression rate,easy to use,retains the quality of image and is also available in Ubuntu.Beside this i enabled ProGuard and worked on Lint issues to reduce APK size.

Android Developer
  • 9,157
  • 18
  • 82
  • 139
0

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
0

You can reduce apk size by del R.class and replaces the reference to constant.

See this: https://github.com/mogujie/ThinRPlugin

farmerjohn
  • 11
  • 4
0

APK contains a zip file of all the things that your mobile application has including Java class files, resource files among others.

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.

hnrindani
  • 9
  • 5
0

Use MinifyEnable true and ShrinkResources true

MinifyEnabled will reduce your code as it compresses it. While shrinkResources shrink the resources.

To use ShrinkResources you have to set minifyEnbled true.

Maaz Patel
  • 756
  • 2
  • 7
  • 20
0

There are a few techniques:

  1. Proguard
  2. Unnecessary Resources
  3. Webp images
  4. Signed apk
  5. Android App Bundle

For detailed explanation: https://stackoverflow.com/a/63988744/13928327

Tejas Mane
  • 131
  • 1
  • 5
-1

use SVG images instead of jpg in order to reduce the apk size. put your image files in the database .. so that the users of your app can download it from the database and it will reduce the app size dramatically..