Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[SCRIPT][LINUX][WINDOWS] APK Sign/Zipalign/Install

OP aureljared

19th April 2014, 12:41 PM   |  #1  
aureljared's Avatar
OP Senior Member
Flag Quezon City
Thanks Meter: 459
 
703 posts
Join Date:Joined: May 2013
More
Hello all!

Out of laziness, I made myself a Windows script for signing and zipaligning APKs I modded.
Then I thought, why not make it available to others?
So I added ADB support and user interaction, made a Linux version, and pushed my work to GitHub.

Features
  • User interaction
  • Colorful terminal output (Linux-only)
  • ADB installation support
  • Error output

What's included?
  • ADB 1.0.31
  • The zipalign binary
  • signapk.jar
  • The script, of course :P

Prerequisites
  • An APK to be signed
  • Java JDK in your path
  • Common sense

Downloads
Script content (v1.1)
You don't have to download the whole package to see the script!
Linux (zipalign.sh)

Code:
#!/bin/bash

# Zipalign/sign script for Linux
# by aureljared@XDA.

# Remove scroll buffer
echo -e '\0033\0143'

# Colourful terminal output (from AOSPA building script)
cya=$(tput setaf 6)             #  cyan
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) #  red
bldgrn=${txtbld}$(tput setaf 2) #  green
bldcya=${txtbld}$(tput setaf 6) #  cyan

# Phase 1
echo -e "${bldcya}Enter the name of the apk you want to sign."
echo -e "${bldcya}Example: ${cya}SystemUI"
echo -e ""
echo -e "${bldred}NOTES:"
echo -e "${cya}What you enter here will also be the filename of your final APK."
echo -e "${cya}APK must be in the same folder as this script."
echo -n "Filename: "
read appname

# Check existence
if [ -f $appname.apk ]
then
echo -e ""
echo -e "${bldgrn}APK exists."
else
echo -e "${bldred}$appname.apk does not exist. Exiting..."
exit 1
fi

# Phase 2
echo -e "${bldgrn}Signing APK..."
java -jar signapk.jar testkey.x509.pem testkey.pk8 $appname.apk temp.apk

# Check existence
if [ -f temp.apk ]
then
mv $appname.apk $appname-original.apk
clear
echo -e "${bldgrn}Zipaligning..."
else
echo -e "${bldred}FATAL: Temporary APK not found. Exiting..."
exit 1
fi

# Phase 3
chmod a+x ./zipalign
./zipalign -f -v 4 temp.apk $appname.apk

# Check existence
if [ -f $appname.apk ]
then
echo -e "${bldcya}Would you like to push to your device now?"
else
echo -e "${bldred}FATAL: Final APK does not exist. Exiting..."
exit 1
fi

# Push to device
read -p "y/n: " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e '\0033\0143'
chmod a+x ./adb
echo -e "${cya}Firing up ADB 1.0.31..."
./adb start-server
echo -e "${cya}Waiting for device - make sure device is connected in ${bldcya}debugging mode"
./adb wait-for-device
echo -e "${cya}Installing apk to device..."
./adb install $appname.apk
echo -e "${bldgrn}Done. Exiting..."
./adb kill-server
exit 0
else
echo -e ""
read -p "Press any key to exit."
exit 0
fi

Windows (zipalign.bat)

Code:
@echo off
cd "%~dp0"
set choice=C:\Windows\system32\choice.exe
cls

:get
echo [] Enter the name of the apk you want to sign.
echo [] Example: SystemUI
echo []
echo [] NOTE: What you enter here will also be the filename
echo []       of the final APK. Case-sensitive.
set /p input="Filename: " %=%
IF EXIST %input%.apk (
goto sign
) ELSE (
goto error
)

:sign
echo [] Signing APK...
java -jar signapk.jar testkey.x509.pem testkey.pk8 %input%.apk temp.apk
IF EXIST temp.apk (
goto zipalign
) ELSE (
goto error
)
goto sign

:error
echo [] Input / Temporary / Final APK not found.
pause
exit

:zipalign
cls
echo [] Zipaligning...
del %input%.apk
zipalign -f -v 4 temp.apk %input%.apk
IF EXIST %input%.apk (
goto choice
) ELSE (
goto error
)
goto zipalign

:choice
cls
choice /c yn /m "[] Do you want to install the APK on your device?"
IF %ERRORLEVEL%==1 goto push
IF %ERRORLEVEL%==2 goto last
goto choice

:push
echo [] Firing up ADB 1.0.31...
adb start-server
echo [] Waiting for device - make sure your device is connected in debugging mode.
adb wait-for-device
echo [] Installing...
adb install %input%.apk
echo [] Done.
goto last

:last
echo [] Removing temporary files...
del temp.apk
echo [] Done.
exit


Credits
  • the Android Open Source Project for ADB and zipalign
  • I don't know who created signapk.jar, but thanks to him anyway
  • the AOSPA team, because I adapted their colored terminal output located in build.sh

Changelog
Code:
v1.3 [COMING SOON]
- ability to process all APKs in folder if no APKs are specified (I have to figure this out first, I'm not that experienced in Bash scripting - but I think it's something along the lines of find .apk | ./zipaligner.sh ?)
- ability to choose name of final APK
- ask user whether to push or install to the device

v1.2 (Linux)
- Remove temp files on error/finish

v1.1 (Linux/Windows)
- Add ADB support

v1.0 (Linux/Windows)
- Initial release
Let me know what you think - and of course, press thanks!
Last edited by aureljared; 20th April 2014 at 05:48 AM.
The Following 4 Users Say Thank You to aureljared For This Useful Post: [ View ]
19th April 2014, 12:44 PM   |  #2  
aureljared's Avatar
OP Senior Member
Flag Quezon City
Thanks Meter: 459
 
703 posts
Join Date:Joined: May 2013
More

How to use:
APK to be signed and zipaligned must be in the same folder as the script.
For Linux:
Code:
$ chmod a+x ./zipaligner.sh
$ ./zipaligner.sh
(follow onscreen instructions)
For Windows
Doubleclick zipaligner.bat, then follow onscreen instructions.
Last edited by aureljared; 20th April 2014 at 05:40 AM.
The Following User Says Thank You to aureljared For This Useful Post: [ View ]
20th April 2014, 05:46 AM   |  #3  
aureljared's Avatar
OP Senior Member
Flag Quezon City
Thanks Meter: 459
 
703 posts
Join Date:Joined: May 2013
More
Reserved 1

Sent from my GT-I8730 using Tapatalk
20th April 2014, 05:47 AM   |  #4  
aureljared's Avatar
OP Senior Member
Flag Quezon City
Thanks Meter: 459
 
703 posts
Join Date:Joined: May 2013
More
Reserved 2

Sent from my GT-I8730 using Tapatalk
Post Reply Subscribe to Thread

Tags
adb, script, sign, tool, zipalign
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes