Jump to content
  • Advertisement

How I halved apk size

Ruslan Sibgatullin

1836 views

1*u_5yDFEpoTD3ST_bn0dgfQ.jpegOriginally posted on Medium

You coded your game so hard for several months (or even years), your artist made a lot of high-quality assets, and the game is finally ready to be launched. Congratulation! You did a great job. Now take a look at the apk size and be prepared to be scared. What is the size — 60, 70 or even 80 megabytes? As it might be sounds strange to hear (in the era of 128GB smartphones) but I have some bad news — the size it too big.

That’s exactly what happened to me after I’ve finished the game Totem Spirits. In this article I want to share several advises about how to reduce the size of a release apk file and yet not lose the quality.

Please, note, that for development I used quite popular game development engine Libgdx, but tips below should be applicable for other frameworks as well. Moreover, my case is about rather simple 2D game with a lot of sprites (i.e. images), so it might be not that useful for large 3D products.

 

 

 

To keep you motivated to read this article further I want to share the final result:

I managed to halve the apk size — from 64MB to 32.36MB.

Memory management

The very first thing that needs to be done properly is a memory management. You should always have only necessary objects loaded into the memory and release resources once they are not in use. This topic requires a lot of details, so I’d rather cover it in a separate article.

Next, I want to analyze the size of current apk file. As for my game I have four different types of game resources:

1. Intro — the resources for intro screen.

1*sC1w5idpfpfhzGSIL9_E8Q.jpeg

Intro background

Loaded before the game starts, disposed immediately after the loading is done. (~0.5MB)

2. In menu resources — used in menu only (location backgrounds, buttons, etc). Loaded during the intro stage and when a player exits a game level. Disposed during “in game resources” loading. (~7.5MB images + ~5.4MB music)

3. In game resources — used on game levels only (objects, game backgrounds, etc.). Loaded during a game level loading, disposed when a player exits the game level. Note, that those resources are not disposed when a player navigates between levels (~4.5MB images + ~10MB music)

4. Common — used in all three above. Loaded during the intro stage, disposed only once the game is closed. This one also includes fonts. (~1.5MB).

The summed size of all resources is ~30MB, so we can conclude that the size of apk is basically the size of all its assets. The code base is only ~3MB. That’s why I want to focus on the assets in the first place (still, the code will be discussed too).

Images optimization

The first thing to do is to make the size of images smaller while not harming the quality. Fortunately, there are plenty services that offer exactly this. I used this one.

This resulted in 18MB reduction already! Compare the two images below:

1*9A23phO-TVVQN11IBzJYhA.png

Not optimized

1*kmzB4p8v9MNSujGpX1gEPg.png

Optimized

the sizes are 312KB and 76KB respectively, so the optimized image is 4 times smaller! But a human eye can’t notice the difference.

Images combination

You should combine the same images programmatically rather than having almost the same images (especially if they are quite big). Consider the following example:

1*vn0LQGYxKlyEv6GWENs18A.jpeg

Before

1*7q6CTo6Gr1z6CWHvkU_f2A.png

After

1*wqQS_j0RgQbpT7QqzHldNw.png

God of Fire

1*GC097jZ5LGWJIyzscAV8bA.png

God of Water

Rather than having four full-size images with different Gods but same background I have only one big background image and four smaller images of Gods that are then combined programmatically into one image. Although, the reduction is not so big (~2MB) for some cases it can make a difference.

Images format

I consider this as my biggest mistake so far. I had several images without transparency saved in PNG format. The JPG version of those images is 6 times more lightweight! Once I transformed all images without transparency into JPG the apk size became 5MB smaller.

Music optimization

At first the music quality was 256 kbps. Then I reduced it to 128 kbps and saved 5MB more. Still think that tracks can be compressed even more. Please, share in comments if you ever used 64 kbps in your games.

Texture Packs

This item might be a bit Libgdx-specific, although I think similar functionality should exist in other engines as well. Texture pack is a way to organize a bunch of images into one big pack. Then, in code you treat each pack as one unit, so it’s quite handy for memory management. But you should combine images wisely. As for my game, at first I had resources packed quite badly. Then, I separated all transparent and non-transparent images and gained about 5MB more.

Dependencies and Optimal code base

Now let’s see the other side of development process — coding. I will not dive into too many details about the code-writing here (since it deserves separate article as well). But still want to share some general rules that I believe could be applied to any project. The most important thing is to reduce the quantity of 3d party dependencies in the project. Do you really need to add Apache Commons if you use only one method from StringUtils? Or gson if you just don’t like the built-in json functionality? Well, you do not. I used Libgdx as a game development engine and quite happy with it. Quite sure that for the next game I’ll use this engine again. Oh, do I need to say that you should have the code to be written the most optimal way? :) Well, I mentioned it.

Although, the most of the tips I’ve shared here can be applied at the late development stage, some of them (especially, optimization of memory management) should be designed right from the very beginning of a project.

Stay tuned for more programming articles!



4 Comments


Recommended Comments

Always interesting to hear experiences! :) I guess the runtime footprint and the APK size are quite interrelated, did you have any problems on devices from runtime memory use which encouraged you to load resources only when required? Or was it more a pre-emptive measure to prevent problems on smaller devices (difficult to test there are so many!)?

Another good trick with the multiple similar images is to do a 'diff' on them, and then instead of saving the whole image each time, just save the difference. If a lot of the values are the same, the diff will be zero and this will compress well.

Share this comment


Link to comment

Hi lawnjelly! Actually, that way to organize resources loading is one of the "best practices" suggested by libgdx devs. So it is more a pre-emptive measure indeed. But I even tested the game on my old Acer Liquid MT (released 7 years ago) and everything works smoothly :) 

The trick about images diff sounds complicated, have you ever used it in a real project? 

Share this comment


Link to comment

All the time, saving the difference between values rather than an absolute value is one of the most fundamental compression methods. I've used in it images, video, audio, animation data, both as part of my own compression, or as a pre-process before applying other compression (like png). And nearly all compression formats you use everyday will use the idea. :)

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By Batcloud
      Hello, my name is Giovanni Bruno and I developed a game in a week that creates a casual experience of reflex and focus on the iOS platform. I'd love to hear your take on the game. Would you have 20 seconds to watch a little gameplay?   Gameplay link: https://www.youtube.com/watch?v=A7aGA9yVpEQ Download link: https://itunes.apple.com/us/app/levita/id1445334524?ls=1&mt=8   More about me: I've been an iOS developer since 2017 when I joined Apple Developer Academy Mackenzie in Brazil, I had a great opportunity to be a WWDC 2018 scholarship winner and attend the event. Now I'm venturing into the development of casual games for iOS, I hope to create ever better experiences that enchant people from all over the world. I know that the game is very simple, but I hope that someone play it!
    • By genieee
      We are giving flat 30% discount in any game development.
      A interactive HTML5 game in this holiday season can easily appeal a worthy number of visitors if it put on visually attractive design and fun oriented concept. Even the player who occasionally visit the site would come again and again. During the festival seasons it will be great if you add new html5 games in your website will create good impression and increase number of visitors.
      Christmas and new year is just about to come and it is an ideal time when you get special offers from the market. This Christmas offer comes just once in a year and we are going to offer discount in HTML5 game development, Action and arcade game development, Educational Game development, Multiplayer game development, Puzzle and Strategy Game Development, Sports game development and All kind of Unity base game development as well.
      These offers make your festival special to celebrate. We are giving flat 30% discount in any game development which will simply raise your business with in the month of December and January. These special offers avails you to groom your business, make money through giant profits and switch this festival a delighted occasion for you. Key thing to keep in mind that offers is valid from 10th to 5th of the January.
      Genieee is one of the Top Browser Game Development Studios worldwide that has been serving some of the best quality and concept-driven gaming strategies for more than 15 years. Whether you craze for the fast-paced racing games, heart-pounding first person shooters, or edge-of-your-seat tower defense games, Genieee has to offer quite a diverse collection of games that would keep you on the toes with the unforeseen twists and twirls along the way.
      Engaging and Intriguing – HTML 5 Game Development Studio
      If you are Seeking Christmas discount for your online game development project, just connect with Genieee and we will serve you with an amazing Christmas offer for your development essentials.
      So get up and catch our Christmas offers to make this carnival an exceptional involvement for you. We wish you all a Merry Christmas and Prosperous New Year.
      Website : Genieee.com

    • By LamePower448
      Hello everyone! 
      My name is Abdelfattah Radwan I'm a 16 years old dude from Egypt. 
       
       
      I have created a simple android game called Dodgy all by myself and published it on Google Play App Store. 
       
       
      As of now I have no budget but I'm actively working on the game and due to my non existent budget I couldn't market the game very well so I thought some people on this great community would be interested in trying my game and helping me out by giving feedback. 
       
      I would be really grateful if you go ahead and try it now and give me feedback. 
       
      I'm very open minded so if your feedback is good or bad I would be really interested in knowing it anyways! 
       
      Link: https://play.google.com/store/apps/details?id=com.winterboltgames.dodge
       
      Thanks for reading!
    • By Ridiculous Games
      If you’re looking to make a game that’s actually finishable and not another Open world MMORPG. Then our project might be the right one for you.



      Project name: Tower FPS (working title)
      Target Platform: Mobile (possibly also PC)
      Game Engine: UE4

      Roles required: 
           - Unreal Engine C++ and Blueprint programmer
           - 3D character artist and animator (low poly)
           - 2D concept and UI artist
           - Sound designer + music
      Team size: With all roles filled 7. Currently 3 (Game designer, Environment Artist and UE4 programmer)
      My skills: Game Design and Team Management 
      Previous projects: Some game jams and an uncompleted long term project after a few months of work.
      Project length: Around fall 2019 for a polished and final version
      Compensation: Rev Share (but we all do it more out of passion)

      Requirements:
           - fervor 
           - at least a year of experience or similar skill level
           - for the programmer some experience with mobile development
           - for the 3D artist experience with and desire to work with low poly models
      Time required: Approx, 4-8 hours per week depending on the role.


      But why should you join us?:
      A huge amount of projects fail because their scope is too big. Because of that our goal for this project is to maintain realistic expectations and a manageable scope in order to release a fully polished game.


      Game description:
      What is the most boring time in Tower Defense games? The time during the rounds, in which you can do nothing but watch and wait, to find out whether your defences were good enough. But what if you could take more direct control over the gameplay in real time by steering a main tower from the first person perspective, like in a shooter? That's what you can do in our game.
      The basis of the game is a typical tower defense game in a post-apocalyptic setting. Waves of twisted creatures will attempt to travel through branching pathways to attack and destroy your tower. There is a preparation time in which you can build and improve your defences and during the attack itself the player takes control of the main tower from the first person perspective (this is controlled as in classic shooters only without lateral movement.)

      Special emphasis is placed on the main tower. Outside the level, this can be equipped with additional skill points and new weapons. The level system is story-based. This means that there is a map to visualize, where there are multiple paths to take between levels which will drive storytelling decisions. Outside of the main level there are small quests consisting of mini-games which play out from the ego perspective of the tower.

      More information can be found in our current GDD: https://www.dropbox.com/s/6ibsa10uarpi7hl/Tower%20Defense%20GDD%20englisch.pdf?dl=0  (work in progress).

      Contact: ridiculous.games.apply@gmail.com

      Cheers,
      Finn
       
      Tower Defense GDD englisch.pdf
    • By MobilityWins
      Time to Marble is a game I have been working on and expect to have a product ready for release by the end of 2019, but in order to make sure I can reach that dream, passion, and goal, I will need a designer so that I can give more focus to the coding and mechanics aspect.
       
      Revenue Share is promised at the release of the game to the Designer that joins and helps me reach this release.
       
      I am Using Unity, and also have ProBuilder as an asset that helps bridge polygons between 3d modeling and unity.
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!