Guide to create, build and sign Android applications (using command)
Today, I’d like to take you to a tour guide of how-to create, build and sign Android applications.
I – Setup the environment
– For building an Android app, you will need the help of Ant, for example, I use Apache Ant 1.8.4 for this tutorial, which is the current version at the time of this post written.
Download this package: http://apache.cs.utah.edu//ant/binaries/apache-ant-1.8.4-bin.zip
Extract it to [C:\Program Files\Ant]
It’s for Windows users, if you’re on Linux or Mac, just get and install it from Software Package Manager, it will install automatically for you, and you don’t need to do the following step of adding command to environment.
– For Windows users, now you need to add some commands to the PATH.
Open [Control Panel > System > tab Advanced -> button Environment Variables],
On the group [System variables], find the [Path] and add the following path to it:
+) path to JDK binary: for example, [C:\Program Files\Java\jdk1.6.0_18\bin]
+) path to Android Tools: for example, [C:\Program Files\Android\android-sdk\tools]
+) path to Android Platform-Tools: for example, [C:\Program Files\Android\android-sdk\platform-tools]
+) path to Apache Ant binary: for example, [C:\Program Files\Ant\bin]
Now, the value of my [Path] variable now is like:
C:\Perl\site\bin;C:\Perl\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Android\android-sdk\platform-tools;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Android\android-sdk\platform-tools;C:\Program Files\Android\android-sdk\tools;C:\Program Files\Ant\bin;C:\Program Files\Java\jdk1.6.0_18\bin;
Yours will be similar to mine.
II. Create, Build and Sign
1. Now, let’s go to create one project from command-line.
First, check for which Android platform SDK has been installed on your computer.
> android list targets
The output will list all platforms installed, for example:
Available Android targets: ---------- id: 1 or "android-15" Name: Android 4.0.3 Type: Platform API level: 15 Revision: 3 Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, W XGA720, WXGA800 ABIs : no ABIs.
Your result might have more than mine.
It also means that I can create an Android project for platform “android-15” or id = 1. This value is required to create project.
Ok, I’m creating a sample project:
> android create project --target android-15 --name PeteApp --path ./PeteAppProject --activity MainActivity --package pete.apps.samples.peteapp
[–target] : is the target platform that your project will be running on
[–name] : indicate the name of your application, the APK created will have this name. Anyway, it’s optional, if you don’t specify this parameter, then the either project name or your first activity name will be chosen to be the name of your application. (you might wanna test this!)
[–path] : specify the directory of the project, it will use the existing directory, otherwise, create new.
[–activity] : the first screen or the entrance of your Droid app, it will be registered into [AndroidManifest.xml].
[–package] : your default package name.
That’s all, not that hard right?
The output would be…
Created project directory: E:\AndroidSamples\PeteAppProject Created directory E:\AndroidSamples\PeteAppProject\src\pete\apps\samples\peteapp Added file E:\AndroidSamples\PeteAppProject\src\pete\apps\samples\peteapp\MainAc tivity.java Created directory E:\AndroidSamples\PeteAppProject\res Created directory E:\AndroidSamples\PeteAppProject\bin Created directory E:\AndroidSamples\PeteAppProject\libs Created directory E:\AndroidSamples\PeteAppProject\res\values Added file E:\AndroidSamples\PeteAppProject\res\values\strings.xml Created directory E:\AndroidSamples\PeteAppProject\res\layout Added file E:\AndroidSamples\PeteAppProject\res\layout\main.xml Created directory E:\AndroidSamples\PeteAppProject\res\drawable-hdpi Created directory E:\AndroidSamples\PeteAppProject\res\drawable-mdpi Created directory E:\AndroidSamples\PeteAppProject\res\drawable-ldpi Added file E:\AndroidSamples\PeteAppProject\AndroidManifest.xml Added file E:\AndroidSamples\PeteAppProject\build.xml Added file E:\AndroidSamples\PeteAppProject\proguard-project.txt
Just add source files or whatever stuffs you need…that’s done for the creation.
2. Build it!!!
– There are two build mode in development, [Debug] and [Release]
– You can use these command to build depending on your mode.
> ant debug > ant release
The [bin] directory will be created including your [***-debug-unaligned.apk] or [***-release-unaligned.apk].
– If you ever change anything in [res] (resources) directory, you should remove all stuffs in [bin] and [gen] directory, otherwise, it might cause bug in run-time, due to “fail to load resources”. This is one of very common mistake, so you should be aware of this. However, if you don’t want to work handy, you can use this robo-command:
> ant clean
– In order to install your application, there are many ways, but I show you two common methods:
+) use [Ant] for work, this depend on your mode:
> ant debug install > ant release install
+) use [ADB], navigate to your [bin] directory:
> adb install AppName.apk
3. Release It!!!
– Got your APK work now, but it’s not ready for distribution on Market/Google Play. What you need to do next is to sign and align your release APK. The step is: create keystore > sign APK with key > align final package.
+) Create Key:
Look at my command:
> keytool -genkey -v -keystore peteapp.keystore -alias peteapp -keyalg RSA -keysize 2048 -validity 10000
Here the output
Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: Pete Houston What is the name of your organizational unit? [Unknown]: Nowhere What is the name of your organization? [Unknown]: Noname What is the name of your City or Locality? [Unknown]: Seoul What is the name of your State or Province? [Unknown]: Kangnam What is the two-letter country code for this unit? [Unknown]: KR Is CN=Pete Houston, OU=Nowhere, O=Noname, L=Seoul, ST=Kangnam, C=KR correct? [no]: yes Generating 2,048 bit RSA key pair and self-signed certificate (SHA1withRSA) with a validity of 10,000 days for: CN=Pete Houston, OU=Nowhere, O=Noname, L=Seoul, ST=Kangnam, C=KR Enter key password for <peteapp> (RETURN if same as keystore password): [Storing peteapp.keystore]
For details, please visit this link: Obtain a key
+) Sign the APK:
– Remember, you must create the APK in [Release] mode before doing this.
> jarsigner -verbose -sigalg MD5withRSA -digestal g SHA1 -keystore peteapp.keystore bin\PeteApp-release-unsigned.apk peteapp
The result:
Enter Passphrase for keystore: adding: META-INF/MANIFEST.MF adding: META-INF/PETEAPP.SF adding: META-INF/PETEAPP.RSA signing: res/layout/main.xml signing: AndroidManifest.xml signing: resources.arsc signing: res/drawable-hdpi/ic_launcher.png signing: res/drawable-ldpi/ic_launcher.png signing: res/drawable-mdpi/ic_launcher.png signing: classes.dex
– Now verify whether the signing process was OK.
> jarsigner -verify -verbose -certs bin\PeteApp-release-unsigned.apk
[-certs] : will display the validation of each and every signed file.
[-verbose] : to display the output, there you can check.
+) Align the final:
> zipalign -v 4 bin\PeteApp-release-unsigned.apk PeteApp.apk
The final signed and aligned package [PeteApp.apk] will be created under the main directory of the project.
Now you can use this APK to distribute on Market/Google Play 🙂
III – References
If you do a search over Internet, there are some good articles about this work, I also do refer from them as well. IF you feel any hard part, try to refer to them all and get the idea how it works.
Android Developers – Building and Running from Command Line
Android Developers – Sign Your Application
Android Engineer – Using Ant to Automate Building Android
IV – Final Words
Well, enjoy and have fun!
Cheers,
Pete Houston