Yes, it is possible to reduce the size of an APK file by just changing its compression.
It is possible to use any ZIP utility such as Winzip, Winrar, or 7-Zip. I used 7-Zip.
To execute these commands, I created a script. BAT, but here I will explain step-by-step the lines of the script (my APK file is C:\Android\Teste.apk
).
First I make sure that the temporary folder used in the process is empty, in my case C:\Android\TempAPK
:
if exist C:\Android\TempAPK (
del /S /Q C:\Android\TempAPK
rd /S /Q C:\Android\TempAPK
)
md C:\Android\TempAPK
Then, I extract the contents of the APK to this temporary folder:
"C:\Program Files\7-Zip\7z.exe" x C:\Android\Teste.apk -oC:\Android\TempAPK
Then delete the folder META-INF
, together with its contents (I did not use the command deltree
, because I couldn’t find it in Windows 8):
del C:\Android\TempAPK\META-INF /S /Q
rd C:\Android\TempAPK\META-INF
Then compress the files again to a file called Teste.zip
using the method Deflate
(that is OBLIGATORY), and with compression settings to the maximum:
cd C:\Android
"C:\Program Files\7-Zip\7z.exe" a -tzip C:\Android\Teste.zip .\TempAPK\* -mm=Deflate -mpass=15 -mfb=258
Now you need to sign this ZIP using the tool jarsigner
, which comes with the JDK (in my case, it’s JDK 1.7.0_10):
cd "C:\Program Files\Java\jdk1.7.0_10\bin"
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore C:\Android\keystore\keystore.bin -storepass <SENHA DA KEYSTORE> -keypass <SENHA DA KEY> C:\Android\Teste.zip <ALIAS DA KEY>
Now just align the ZIP file using the tool zipalign
, creating the final APK, and clearing all temporary files (note the SDK path):
cd C:\Android\sdk\build-tools\android-4.4W
del C:\Android\Teste.apk
zipalign 4 C:\Android\Teste.zip C:\Android\Teste.apk
del /S /Q C:\Android\TempAPK
rd /S /Q C:\Android\TempAPK
del C:\Android\Teste.zip
This reduced the size of my APK from 330kB to 274kB.
More about subscription, on the Google website:
Signing Your Applications
Building and Running from the Command Line
Carlos, this information is very good! As I usually build manually, I will try to port to Linux and put here if allowed. + 1
– Wakim
@Wakim for sure! I haven’t tried the script on Linux yet! It’ll be great!!! D
– carlosrafaelgn
I managed to do the "port", can I post as a response? I had a 200kB gain in the process to generate a production apk. In a next build I will use this solution.
– Wakim
@Wakim Please, Wakim!!! Here will serve as a guide for the people who use Windows and Linux! o/
– carlosrafaelgn