dmathieu - Deploy an android application

Deploy an android application

Wednesday, 18 August 2010 in Development Articles by Damien Mathieu Creative Commons License

This article follows Getting started in Android programming. We’re gonna take back the application we’ve programmed previously, build it, test it in the SDK and deploy it on a real phone.

If you haven’t followed the previous article, you can always use it from the github repository. However it’s not the best way to do it ;-)

Sign our applications

Android requires us to sign every application we wish to install, whether it’s on the SDK or on a real phone. That system uses a signature in order to check who’s the author of the application and establish trust links between all of your applications.

The signature isn’t used to check which applications the user can install. It’s useless to have a signature certified by an authorized entity. It’s perfectly allowed and not atypical for applications to use auto signed certificats.

Some features of the signatures can influence the strategy you’ll adopt. Mainly if you plan to deploy several appplciations. The recommanded strategy is one signature for all your applications, for the following reasons :

  • Updates - All versions of one application should be signed with the same certificate. Otherwise, the system won’t update the application.
  • Modularity - The system allows us to share the same process between several applications with the same signature, so they would be considered as the same one. With this, you can have several modules for one applications, allowing the user to install one or an other.

One other important thing when you create your signature is the validity period of this one. Indeed when we hit the key’s expiration date, your users won’t be able to update their applications to newer versions.

Moreover if you plan to publish your application on the Android Market, the key will have to expire after octobre 2033 the 22st.

Development deployment and execution in the SDK

If you’ve built your project the “standard” way, using the android create project command then you have a local.properties file at the top of it. If you’ve checked out an other project then you must create it.

This file must contain the path to the Android SDK on your disk. On my current machine this content is the following :

sdk.dir=/home/damien/android-sdk-linux

If it does not exists yet, create it with the appropriate folder.

Then we will build the application’s package. For that we’ll use ant. This project, is a tool allowing you to build java packages.

For a dev build, enter this in your console :

ant debug

The console display should end with the following :

(master) damien@pcdamien ~/projects/translator $ ant debug

BUILD SUCCESSFUL
Total time: 3 seconds

If you have the BUILD SUCCESSFUL message it means your project has been successfully built.

Now let’s execute the virtual machine and install the application on the cellphone. In our previous article, we’ve seen how to install the Android SDK. Now, let’s create a virtual phone.

android create avd -n translator -t android-7

We’ll create a new virtual machine which we’ll name “translator” and which will have the android-7 target (which is Android 2.1).

Let’s execute that virtual machine.

emulator -avd translator &

Now you should see a window with a cellphone starting. Let it start, it can take a few minutes. Once that’s done, unlock it. Your new virtual machine is started !

installed It’s virtually linked in USB to your machine. So you can access it distantly. That’s what we’re gonna do to install the application.

adb install bin/translator-debug.apk

This command will connect us to the cellphone and install the application on it. The console display should be the following :

2216 KB/s (34777 bytes in 0.015s)
    pkg: /data/local/tmp/translator-debug.apk
Success

If, however, you have the following message :

error: device offline

Try to manipulate the cellphone (open the menu for example). It hasn’t been detected as active.

Your application has now been installed. Go to the phone’s menu, you’ll see it’s icon.

Production build and installation on a real cellphone

In order to release our application on the market, we must create a signature like we talked in the first part. For that, java offers us a command line tool to manage our keys. It’s keytool.

This is how I’ve generated my own key :

keytool -genkey -v -keystore dmathieu.keystore -alias dmathieu -keyalg RSA -validity 10000

The parameters are the following :

  • -genkey - Tells that we must generate a pair of keys (private and public).
  • -v - Activated the full console render when generating the key.
  • -keystore dmathieu.keystore - The path to the keystore file which will be generated.
  • -storepass - The keystore’s password. Don’t transmit it that way. If you don’t put anything here, it’ll be asked when generating the key.
  • -alias dmathieu - An alias for the key. Only the first 8 characters will be taken into account.
  • -keyalg RSA - The key encryption algorithme. The available algorithms are RSA and DSA.
  • -dname - A name describing who has created the key.
  • -validity 10000 - The number of days the key will be valid.
  • -keypass - Your key’s password. Don’t put it here. It’ll be asked.

When executing that command, a file dmathieu.keystore is created. It contains your key. Save it in a place where you’re sure it won’t be lost but it’ll remain safe (I’ve personnaly stored it in my Google Docs account).

Now, open the file build.properties. In that file, we have to provide the path to your keystore and your key’s alias.

key.store=/home/damien.projects/dmathieu.keystore
key.alias=dmathieu

We can now build our application for a release with the key we’ve just created. In the commande line, let’s use to do to.

ant release

This does several things :

  • You application is built, like when doing an ant debug.
  • Your keystore is added to the application in order to authenticate it.
  • The package is optimized in order to speed it’s installation.

phone Your project is now built and ready to be installed on your cellphone. It’s available at the path bin/translator-release.apk. You only have to clip your cellphone in USB and install the application on it. You can also, of course, publish it on the Android Market.

Try it by yourself : go to the Android Market, search for the Translator application and install it. That’s the application we’ve been programming in those two articles.

Conclusion

We have, in those two articles on Android, seen how to develop an application, test and install it on a phone. However we’re very far from having seen everything in Android app development. We could, for example, add a widget. And many other things !

It’s your turn to play now. What applications have you programmed after reading this article ?