Monday, July 10, 2017

How to reduce a Unity Android game size by 10MB

I'd been working on my Blobby Volley game remake for a while and I'd been wondering why the output apk file was so large. The game used to have a size of around 3MB when it was written using AndEngine. So how did it grow to more than 25MB in Unity? I might have added some additional code but the graphic assets and sounds were all the same. I decided to investigate it a bit further.

I took the generated apk file and I put it in the apk analyzer tool that's built into Android Studio. You can find it in "Build/Analyze APK...". The results were quite interesting and led me to some further steps. BTW, if you don't know how to handle Android Studio, you can just change the apk file's extension to zip, unzip the file and analyze its contents manually. It won't be as efficient but it'll do.


What I soon found out was that what took the most space in the apk file were not my graphic assets nor my code but the native libraries in the lib folder. They were split into two subfolders: x86 and armeabi-v7a each of roughly 10MB.


I started thinking if I could get rid of some of them and it turned out that I could. The answer was to build the project for each architecture (arm and x86) separately and then publish them both on Google Play. This way each version would be around 15MB in size and it would be distributed to appropriate, compatible devices automatically. Here's how I did it.

I went to the Build Settings (File/Build Settings...) and I found Other Settings/Configuration/Device Filter. It was set to FAT (ARMv7 + x86). I switched it to ARMv7, built it and then did the same for x86, saving it in a different file and incrementing the version name and version code beforehand (Other Settings/Identification/Version and Other Settings/Identification/BundleVersionCode).


Then I followed the official Multiple APK Support guideline to publish both apk files on Google Play. It was quite simple. I just published the first file the normal way. With the second file, I removed the first version form APKs to deactivate, so it moved to the APKs to retain section.


This way, I have two active apk files now, both with a different version name and version code and both targeted at different device architectures. When someone installs my game from Google Play, they get only the version compatible with their device, which is now 10MB lighter. Yes, it costs me a bit more work to create two separate apk files and then publish them on Google Play but I think it's worth the effort. I created a script that builds the two versions from the command line but it's a subject for a different post perhaps.


I hope my investigation will help you make your Unity Android game leaner too. If something is not clear for you, make sure to post your questions in the comments and I'll do my best to answer them.


No comments:

Post a Comment