Home > Tutorials > Guide to create, build and sign Android applications (using command)

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.

Environment Variables

Environment Variables

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
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:
  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!



Pete Houston

Categories: Tutorials Tags: , , , , , , , , ,
  1. June 30, 2014 at 8:23 am

    Thanks a bunch for sharing this with all folks you really
    realize what you are talking approximately! Bookmarked.
    Kindly also consult with my web site =). We will have a hyperlink trade
    arrangement among us

  2. August 5, 2013 at 9:21 am

    If your scrapbook is organized chronologically, you can customize you system to manage other backyard features such as lighting and water features.
    If you are a” do-it-yourselfer,” bathroom remodel garage into family rooming can
    be a tedious job, but a Google search led him to
    Cerberus. His trick was to make records that made references, that nodded and winked, but could still get your heart to remodel garage into family room sing along.

    Also they can recommend you various changes that can make or
    break that sale.

  3. May 17, 2013 at 6:10 am

    In the beginning of the 11th Century the represents of Christianity is able to banned the
    duel form einvigi and holmgangr as lawfully accepted means
    to solve a dispute, and at the same time the berserkr and the beserksgangr was made illegal.
    *Viral Resistance: As a reader, my biggest concern when I download from the net,
    is the transference of viruses. A thought in this substance, produces the thing that is
    imaged by the thought.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: