Sunday 30 October 2011

How to sign Application using Apache Ant

,
Before starting, it is assumed that, you already have an android project which is ready to build. The android project can be created using either Android tool or Eclipse IDE. Also it is assumed that you are configured with latest JDK version (jdk1.6.0_24 or above) in your machine.

Now we can start with how to build an android application using Apache Ant tool.

Step 1: Create build.xml 

The first step is to create a build.xml file in the base directory of your project. This can be done using android tool using following command in the command prompt.
%android_installed_path% \tools>android update project -p "your project path".
Figure 1
First navigate to the Android installed folder and update the path to include tools folder. Then, using android tool inside tools folder, give the command for creating your build.xml as above. Now you will have a working ant build script in build.xml. 

Step 2: Downloading and configuring Apache Ant tool.

If you have already configured Ant tool in your machine you can skip this step. You can get the latest ant tool from following link.
Just download the latest binary release and unzip to your directory. Here I downloaded ant version 1.8.2 and unziped to folder "C:\Program Files\Apache-ant-1.8.2".
Next, you need to set a few environment variables so that your command prompt knows how to find Ant executables and Ant knows where to find java. Make sure that below environment variables are set in your system properties.
ANT_HOME: must define the root directory of the ant installation. In my case, it is C:\Program Files\Apache-ant-1.8.2.
JAVA_HOME: must define the root directory of the Java SDK installation that Ant must use.

Also you must edit the environment variable:
PATH: here you must add the path to the bin directory of Ant. In my case, it is C:\Program Files\Apache-ant-1.8.2\bin;

Now, you can test your settings by navigating to your project base folder where you created your build.xml from your command prompt, and type “ant”. If ant is properly configured, you will get an output like figure 2.

Figure 2

Note: If you get an error like, ant command is not found, then check path in command prompt or check your ANT_HOME environment variable created is correct.


If you get error like, the Android Ant-based build system requires Ant 1.8 .0 or later, current version is 1.7.1, make sure that your JAVA _HOME environment variable created is correct or you are using JDK 1.6.24 or above.

At this point you should be able to type “ant release” at the command prompt, which will build the project, placing the unsigned .apk file inside the bin directory of your project’s base directory.
Step 3: Creating a private key for signing the .apk file

Before an application can be delivered to a device, the package must be signed using a private key. To generate a self-signed key with Keytool, use the keytool command.

You can find the keytool.exe in JDK installed folder under bin folder.

Navigate to this folder in your command prompt and type the following command to generate a password protected keystore file.

%JAVA_HOME%\bin>keytool -genkey -v -keystore private_key.keystore -alias alias_private_key -keyalg RSA -keysize 2048 -validity 10000

 Running the example command above, Keytool prompts you to provide passwords for the keystore and alias, and to provide the Distinguished Name fields for your key. It then generates the keystore as a file called private_key.keystore. The keystore and alias are protected by the passwords you entered. The keystore contains a single key, valid for 10000 days. The alias is a name that you that you will use later, to refer to this keystore when signing your application.

On Successful creation of keystore, you could find like this in the command prompt and a private_key.keystore will be generated in the bin folder of your JAVA_HOME path.
Here I created a keystore using password “android” to both keystore file and alias. Now, copy this private_key.keystore to your project’s base folder.
Step 4: Signing your application using Ant tool
Now we must tell the build script about our keystore. Create a file called build.properties in your project's base directory, in the same directory as build.xml and the other properties files. Add the following lines:  

key.store= private_key.keystore
key.alias=alias_private_key

Where, private_key is the name of your keystore and alias_private_key is the name of your alias.

Now, when you run the previous command “ant release” from command prompt, after navigating to project’s base folder, it will prompt for keystore and alias password. On typing correct passwords ont the command prompt, signed apk file will be created in the bin folder of your project base folder, which can be successfully  used to run on the device.


Step 5: Creating signed apk in one step using ant tool
You can create a signed apk file using ant tool in one step. In the above procedure, you need to enter both keystore and alias password after typing ant release command. You can avoid this second step and create a signed apk using a single ant release command by editing build.properties file as below.
key.store= private_key.keystore
key.alias=alias_private_key
key.store.password=android
key.alias.password= android


Cheers,
Have a nice day. View Complete List of Tips

2 comments to “How to sign Application using Apache Ant”

  • 23 June 2013 at 11:10

    What is this error ? SDK does not have any Build Tools installed.

    delete
  • 24 June 2013 at 08:43

    Hi Catalin, it seems like there is some issue with the build tools. In SDK 22, the build tools and platform tools are split up. So, if you have tried an upgrade to SDK 22 from older versions, make sure to install the build tools and platform tools. If the problem still persists, I strongly recommend to start with a fresh installation by downloading the ADT bundle from http://developer.android.com/sdk/index.html

    delete

Post a Comment

 

Tips for Android Developers Copyright © 2011 -- Powered by Blogger