[DEVS] Optimize all APKs automatically, Reduce memory footprint! [Now with Zipalign]

Search This thread

coolbho3000

Retired Senior Recognized Developer
Dec 26, 2008
899
784
Update 9/28: Now contains the SDK 1.6 zipalign binary and routine to align each APK after optimization. This results in slightly larger file sizes but a smaller RAM footprint and faster performance. For more information about zipalign, see http://android-developers.blogspot.com/2009/09/zipalign-easy-optimization.html

Update 9/16: new zip defaults to mx5 zip compression, creates "original" and "optimized" folder for apks.

This is a very simple batch file that makes use of command line 7-zip and a special build of optipng (supporting recursive optimizing) to reduce the size of the PNGs in your APKs. This would reduce the overall memory footprint of your apps, and allow you to fit more on the phone's puny internal memory.

As for how much this helps, here's a test run on the latest JACxHeroSki:

Size of uncompressed folder of data/app_s before optimization: 57.3 MB
Size of uncompressed folder of data/app_s after optimization: 43.1 MB

To use it, simply copy all the APKs you want optimized into the batch files' directory and run "apkopt_all.bat" This will take a while but it's all automated! This will then create two folders, optimized and original.

To make the optimization process faster, reduce the "-o99" parameter in apkopt.bat - however, this will result in larger PNGs so best to leave it at o99 and wait. You can also tweak the compression level of the ZIP itself - I left it at "normal" (-mx5) because higher compression levels have reported problems with some APKs.

Once done, the APKs will not be signed but that should not be an issue if you're bundling the APK with a ROM or pushing it through adb.

Optimization has no negative effects, it just makes the PNGs smaller.

Windows only for now - the variant of optipng that this depends on only has a Windows binary available.

It definitely makes a difference!

This distribution includes 7-zip and a variant of optipng called roptipng. Sources can be found:
http://www.7-zip.org/
http://code.google.com/p/roptipng/

I apologize for my noobish Windows command line knowledge. Many devs (like Cyanogen) already seems to have been doing this long ago! His builds are so optimized you can't optimize them more. Any size difference will be due to different compression methods/levels on the ZIP. :eek:
 

Attachments

  • apkopt.zip
    762.9 KB · Views: 8,622
Last edited:

dwang

Senior Member
Nov 23, 2008
2,454
318
Is this even necessary when pretty much every ROM uses app2sd?

And wouldn't zipping the png slow things down because the OS now needs to unzip before running?
 

coolbho3000

Retired Senior Recognized Developer
Dec 26, 2008
899
784
sweet, will you do it so I can test it on jacheroskiv2.1 and 2.2? i have a mac w/ ubuntu. no windows!

I'm in the process of making/uploading a pack of APKs of javheroski 2.1.

Is this even necessary when pretty much every ROM uses app2sd?

And wouldn't zipping the png slow things down because the OS now needs to unzip before running?

No, this isn't actually zipping the PNGs themselves - the only purpose 7-zip serves is to unpack and repack the APKs.
 
Last edited:

dwang

Senior Member
Nov 23, 2008
2,454
318
I'm in the process of making/uploading a pack of APKs of javheroski 2.1.



No, this isn't actually zipping the PNGs themselves - the only purpose 7-zip serves is to unpack and repack the APKs.

Oh cool... I can see the usefulness of having smaller pngs..
 

jubeh

Senior Member
Mar 15, 2009
1,264
20
I have a similar script that I used for my build, but i used the regular optipng at -o7 for all files. This should help me cram more stuff on my rom, thanks bunches. I have a question, though, I've yet to test this, but is there any adverse effect from running 9-patched pngs through optipng? They're encoded with extra instructions for stretching and padding. In my build, I only ran regular pngs, but if I were able to run the whole thing... I have to try this but I'm at work... Oh, wait, I'm the boss!, I'll try this later today.
---edit---
Oh, cool, I guess I didn't read right. I was trying to modify the thing so that it would recursively optimize extracted pngs in windows folders, but I read the bats and it actually opens up the zips and does all this itself, great! I should have read the post more closely.
 
Last edited:

NguyenHuu

Senior Member
Nov 14, 2007
1,732
121
California
uhm i cant seen to get this to work. download your zip and extracted it to a folder. four files in it. placed all my apks into this same folder. ran the batch file but it just opened up and closed and outputted a 1kb file called "and"
 

NguyenHuu

Senior Member
Nov 14, 2007
1,732
121
California
Need to be on your c drive or local.

ok got it thanks! it was on my secondary drive not C drive. i tried it on cyanogen's system apps and it went from 14.2MB to 13.7MB, not a huge improvement but an improvement nonetheless. this app has a lot of potential for hero roms that really need those extra MBs haha.

if only the batch file can be edited to automatically create duplicate apk instead of overwriting just in case forget to backup. happens sometimes haha. maybe signing can be added to the batch script as well for market apps that need to be resigned since you cant just push them through like you can with system apps that are apart of the rom, they need to be installed.
 
Last edited:

jubeh

Senior Member
Mar 15, 2009
1,264
20
dont try this on your framework though >.<. I did and it wouldn't boot, seems compression was too aggressive (something about 2XXXXXX to 1XXXXX something, dont remember what it was, but I think bringing o-49 should probably fix it for framework)
 

jubeh

Senior Member
Mar 15, 2009
1,264
20
UNCOMPRESS_DATA_MAX (XXXXXXX vs 1048576). Causes errors on APKs that are over compressed. Any idea what setting it could be to keep it under that threshold?
 

jubeh

Senior Member
Mar 15, 2009
1,264
20
hrmm.... I saw what's going on.... For some reason, the script is also processing resources.arsc. This file has to be left alone or it causes problems. I used the script to optimize all pngs, then i extracted all apks, and then i used the optimized pngs and put them back on the regular apks and it worked perfectly, though the gain is much, much more modest, about ~300KB coming from my previous -o7 optimization, and I guess I can attribute that to the 9 patch pngs I hadn't processed the first time around. Am I wrong that resources.arsc should be left untouched? a 10 MB gain is just what I needed to make all of hero fit in death spl's /system (and light hero on a regular spl).
 
Last edited:

jubeh

Senior Member
Mar 15, 2009
1,264
20
ok, i see, the problem is the re-compression of the apk. I think it's not using the same zip format, so when dalvik tries to expand it causes a problem. roptipng should still save me A LOT of time though...
 

miketaylor00

Senior Member
Jul 17, 2009
1,404
0
First, thanks for doing this. I have a suggestion. I think you should create an "original" folder and an "optimized" folder (or whatever names you like). So that you can drop the files into the original folder rather than copying them into the same directory. It would just be cleaner that way. Also, you could keep the orginal files in that folder and put the new ones into the optimized folder so people don't need to back them up. They would have both the original and optimized files when the process is finished.
 

jubeh

Senior Member
Mar 15, 2009
1,264
20
Have you investigated into why some apks don't work after being compressed? or am I the only one having problems? Some apps compress at best compression and still work, but other apps have to be compressed less in order to work. I tried compressing the whole batch, and then trying to see which ones failed and then try compressing again with a lower setting, etc etc, but it's hard to do for services and non-launcher apps because you don't notice they don't work until you need them. I've seen other people use these scripts and produce working roms, so it might be something different with my setup (win7 64)
 

Top Liked Posts

  • There are no posts matching your filters.
  • 12
    Update 9/28: Now contains the SDK 1.6 zipalign binary and routine to align each APK after optimization. This results in slightly larger file sizes but a smaller RAM footprint and faster performance. For more information about zipalign, see http://android-developers.blogspot.com/2009/09/zipalign-easy-optimization.html

    Update 9/16: new zip defaults to mx5 zip compression, creates "original" and "optimized" folder for apks.

    This is a very simple batch file that makes use of command line 7-zip and a special build of optipng (supporting recursive optimizing) to reduce the size of the PNGs in your APKs. This would reduce the overall memory footprint of your apps, and allow you to fit more on the phone's puny internal memory.

    As for how much this helps, here's a test run on the latest JACxHeroSki:

    Size of uncompressed folder of data/app_s before optimization: 57.3 MB
    Size of uncompressed folder of data/app_s after optimization: 43.1 MB

    To use it, simply copy all the APKs you want optimized into the batch files' directory and run "apkopt_all.bat" This will take a while but it's all automated! This will then create two folders, optimized and original.

    To make the optimization process faster, reduce the "-o99" parameter in apkopt.bat - however, this will result in larger PNGs so best to leave it at o99 and wait. You can also tweak the compression level of the ZIP itself - I left it at "normal" (-mx5) because higher compression levels have reported problems with some APKs.

    Once done, the APKs will not be signed but that should not be an issue if you're bundling the APK with a ROM or pushing it through adb.

    Optimization has no negative effects, it just makes the PNGs smaller.

    Windows only for now - the variant of optipng that this depends on only has a Windows binary available.

    It definitely makes a difference!

    This distribution includes 7-zip and a variant of optipng called roptipng. Sources can be found:
    http://www.7-zip.org/
    http://code.google.com/p/roptipng/

    I apologize for my noobish Windows command line knowledge. Many devs (like Cyanogen) already seems to have been doing this long ago! His builds are so optimized you can't optimize them more. Any size difference will be due to different compression methods/levels on the ZIP. :eek:
    1
    Hello, coolbho3000. I tried using your script for framework-res files but phone didn't bot after it cause resources.arsc was compresed. I managed to edit your script to store only resources.arsc file in archive (without compressing it) so my phone can boot properly.

    Simply in apkopt.bat go to line number 20:
    Code:
    7za a -tzip %~dp0optimized\%~n1.unaligned.apk "%~dp0apkopt_temp_%~n1\*" -mx5
    and change it to:
    Code:
    7za a -tzip %~dp0optimized\%~n1.unaligned.apk "%~dp0apkopt_temp_%~n1\*" -x!"%~dp0apkopt_temp_%~n1\resources.arsc" -mx9 
    7za a -tzip %~dp0optimized\%~n1.unaligned.apk "%~dp0apkopt_temp_%~n1\resources.arsc" -mx0
    1
    Even though the last post is 2 years ago - Excellent work :good:
    After complete optimizing my system/app is smaller than 60mb vs 120mb on stock - without removing too many apps :good: