Paul Brower bio photo

Paul Brower

Android development for fun and profit.

Email Twitter Github Stackoverflow Codementor

Find further code and implementation here: https://github.com/browep/AndroidCustomFontWidgets

In order to put your Android application on a device you have to first sign it. To put it up on the Android market you need to sign it with your own key. The SDK tools use Apache Ant for it’s command line build processes and we are going to use it for ours as well. Don’t worry if you are unfamiliar with Ant, we will only use it sparingly. Let’s assume that you already have a project built and you are ready to release it to the Android Market. First let’s make sure the project build files are up to date. Always make sure you have the latest Android SDK as the build process changes often. http://developer.android.com/sdk/index.html . This tutorial is using version 16. You can check your version by typing

android

which should open the Android SDK window and the revision number is in “Tools>About”. Now let’s make sure the build files are up to date, navigate to the project directory ( the one that contains AndroidManifest.xml) and use:

android update project -p .

This should give you some output of what files were updated.

If you have a working application you should be able to build a debug version of the app with

ant clean debug

This command can be read as “ant, run task ‘clean’ and then run task ‘debug’”. This will clean output directories and then build an APK signed with a debug key. If everything went well you should have “BUILD SUCCESSFUL” at the end of the output. Put any errors in the comments if you get them.

In order to sign an application we need to make a keystore that we use for the lifetime of this application. It’s important to note that this keystore should be backed up in a secure place, losing it means you cannot install any updates on devices that have this app already installed. We are going to use the “keytool” program to create a keystore.

keytool -genkey -v -keystore release.keystore -alias key0 -keyalg RSA -keysize 2048 -validity 10000

This will create a keystore in the “release.keystore” file in the local directory with a key in it called key0 that will have good size to it as well as be valid for 10,000 days.

The keytool will ask for a password ( I have used “123456” in the example project but you should choose something much harder to guess). It will then ask you for some bio info, you can choose to leave it blank if you wish. It will then ask you for the password for the key as it did for the keystore, you can hit RETURN to just use the one you used for the keystore file.

We now need to tell ant where to find the keystore as well as which key within it use when signing the apk. It looks in “ant.properties” for the location of the keystore. I have modified my file to look like this:

key.store=release.keystore
key.alias=key0

We can now use ant to build the release apk:

ant clean release

This will prompt you for the password for the keystore file and then the “key0” key.

If everything was successful it will say “BUILD SUCCESSFUL” and put an apk named something like “MyProject-release.apk” in the /bin directory.

The problem with this approach is that it requires a manual password entry, making it difficult to script using a Continuous Integration system such as Jenkins. Let’s see if we can get it to work without input.

Let us modify the ant.properties file to look like this

key.store=release.keystore
key.alias=key0
key.store.password=123456
key.alias.password=123456

Then:

ant clean release

Ant should now use those passwords instead of prompting.

Now we have a fully automated build process that takes no user input. You can extend this to do things like push release builds to the downloads page on your github repo: https://github.com/github/upload .