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

Search This thread

Nedlinin

Senior Member
Apr 13, 2009
244
34
Huntsville, AL
Upon running the batch skip on a few test apks (aTrackDog, Abduction, and ShopSavvy), each one fails to install either through Astro or through ADB.

Code:
W/PackageParser(  154): java.lang.SecurityException: META-INF/MANIFEST.MF has invalid digest for assest/test_ad.png in /data/app/vmdl71294.tmp

The above is the error given by logcat when the installation of the optimized aTrackDog fails.

If I removed the optipng from the batch file, the zipalign process works fine. The optipng process definitely seems to be causing the install error. I dropped optipng from -o99 to -o50 then -o10 to try different levels of optimization and each give the installation error.

I am running Windows 7 (x64), newest SDK (adb/adb-server).

Any thoughts?
 

maxisma

Senior Member
May 1, 2009
1,212
43
29
Dorsten, Germany
Upon running the batch skip on a few test apks (aTrackDog, Abduction, and ShopSavvy), each one fails to install either through Astro or through ADB.

Code:
W/PackageParser(  154): java.lang.SecurityException: META-INF/MANIFEST.MF has invalid digest for assest/test_ad.png in /data/app/vmdl71294.tmp

The above is the error given by logcat when the installation of the optimized aTrackDog fails.

If I removed the optipng from the batch file, the zipalign process works fine. The optipng process definitely seems to be causing the install error. I dropped optipng from -o99 to -o50 then -o10 to try different levels of optimization and each give the installation error.

I am running Windows 7 (x64), newest SDK (adb/adb-server).

Any thoughts?
resign with test keys
 

jubeh

Senior Member
Mar 15, 2009
1,264
20
actually, even roptipng still has a maximum value of -o7, and anything above that will still default to -o7. Try it, it will produce the same file (md5 it)
 

Wysie

Senior Member
Jul 4, 2009
1,122
6
Some new info:

BTW for all who are repackaging apks: don't use zip or jar commands for app packaging. Android is compatible with them only on a first sight, but in some cases, as above, it isn't. Use aapt instead.

And I haven't see such warning in Android docs :-/

Hope it helps :). Taken from the Maps thread.
 

polyrhythmic

Senior Member
Oct 19, 2007
536
7
Seattle
www.doublerebel.com
Just want to say THANKS!! I just used this to trim down an Eclair build for Kaiser, saved a good 5+MB after optimization! I also made another copy of the batch files that only zipaligns the files, for the apps that I want to be update-able. I've attached these batch files, hope they help someone else.

Cheers and have a Happy New Year!
 

Attachments

  • zipopt_all.zip
    576 bytes · Views: 289

gidkid

Senior Member
Feb 28, 2010
268
6
Ah boo.. I feel like a fool now haha

Also, there used to be an auto-sign tool (JesusFreke I think) which added a right click context menu to resign apks/zips. Searched here but the link to the download file seems dead. Anyone got a copy?

I have it, but I don't think it works here. For some reason, when trying to use it on a file optimized with this, the file FC's after installation. The original file, when signed with the same tool, doesn't FC. Any suggestions?

Also, doesn't resigning the file undo a zipalignment? If this undoes a signing, and a signing undoes this, then isn't it impossible to get both on one file?
 

Daneshm90

Retired Recognized Developer
Jun 1, 2009
3,332
662
I have it, but I don't think it works here. For some reason, when trying to use it on a file optimized with this, the file FC's after installation. The original file, when signed with the same tool, doesn't FC. Any suggestions?

Also, doesn't resigning the file undo a zipalignment? If this undoes a signing, and a signing undoes this, then isn't it impossible to get both on one file?

The app force-closes ? Certain apps dont like to be zipaligned ive found, such as settings.apk. It cud be one of those. I've used this script and evolved it into apk manager (link in my signature). As per themer's request i will be adding the option to batch optimize apk's with either zipalign or png optimization, currently with my script u have no choice it runs roptipng and zipalign.
 

gidkid

Senior Member
Feb 28, 2010
268
6
The app force-closes ? Certain apps dont like to be zipaligned ive found, such as settings.apk. It cud be one of those. I've used this script and evolved it into apk manager (link in my signature). As per themer's request i will be adding the option to batch optimize apk's with either zipalign or png optimization, currently with my script u have no choice it runs roptipng and zipalign.

Does resigning really un-zipalign the package? If it does, then wouldn't the zipalign part of this script be useless if the roptipng part undoes a signing?

BTW, it was a Swype apk that I had themed.
 

Daneshm90

Retired Recognized Developer
Jun 1, 2009
3,332
662
Does resigning really un-zipalign the package? If it does, then wouldn't the zipalign part of this script be useless if the roptipng part undoes a signing?

BTW, it was a Swype apk that I had themed.

Thats why the script signs first then zipaligns. This is the order it does it.

Optimize Png -> Compress apk -> sign apk -> zipalign apk
 

Firerat

Senior Member
Feb 24, 2009
3,848
185
yawn

watch a logcat, apks get zipalined if they are not zipalined

honestly I used to laugh, now I facepalm
 

Daneshm90

Retired Recognized Developer
Jun 1, 2009
3,332
662
yawn

watch a logcat, apks get zipalined if they are not zipalined

honestly I used to laugh, now I facepalm

Correct but not all roms do. the dev adds in a check if he/she wants to zipalign apks that arent aligned at startup. Hence those on old builds such as cupcake, for them manually zipaligning is still valid.
 

Firerat

Senior Member
Feb 24, 2009
3,848
185
Correct but not all roms do. the dev adds in a check if he/she wants to zipalign apks that arent aligned at startup. Hence those on old builds such as cupcake, for them manually zipaligning is still valid.

ahh, I see your point

the guys with rooted phones running cupcake need to get all excited about zipalining stuff
 

Daneshm90

Retired Recognized Developer
Jun 1, 2009
3,332
662
ahh, I see your point

the guys with rooted phones running cupcake need to get all excited about zipalining stuff

Lol but its not JUST cupcake. All firmware uptil and including froyo does not auto-zipalign at startup unless the dev adds that in his rom. Most dev's find it a waste of resources to check the md5 of EACH apk at startup just to see if they're zipaligned hence they put out their roms with their apk's already zipaligned.
 

gidkid

Senior Member
Feb 28, 2010
268
6

Daneshm90

Retired Recognized Developer
Jun 1, 2009
3,332
662
Sorry, saw line above in OP, must have been left there from before zipalign was added or something.

Nope, its for system apks as their signatures aren't verified unlike the apps in /data. Hence u can modify a system app and repackage it w/o signing and just push it 2 ur phone.
 

Bo$s

Senior Member
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
 
Last edited:
  • Like
Reactions: futranbg

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: