Automating Android Builds
- If you started your project from Eclipse, you may not have the Ant build file and supporting properties file in your project
- If you want to sign your application with a key, the default build keeps prompting you for the keystore password
Both of these are relatively easy to over come and here is a great article on StackOverflow. My only goal here is capture it all in one place.
Environment SetupThe first thing to do, is to make sure that your android SDK folder is on your path. In order to do that, just update your local .bash_profile in your home folder to include the following paths:
#!/bin/sh export PATH=$PATH:$HOME:$HOME/Documents/Development/android-sdk-macosx/tools:\ $HOME/Documents/Development/android-sdk-macosx/platform-tools export ANDROID_HOME=/Users/Kouroche/Documents/Development/android-sdk-macosx
Note the \ in the first export command. It is there to split the command over two lines, but you don’t need that in there. This will not only add the Android SDK to your path, but also define ANDROID_HOME which is used by the build file (more on that later).At this point, you will either have to logout and login again, or source .bash_profile to make sure the changes took effect. To verify, run the following command from your home folder:
$ android list targets
Build filesIn order to add the build files to your project, you can either create a new project from the command line and copy those build files to your project, or you could update you project from the command line to add in the build files.
Creating a new projectIn order to create a new project from the command line, run the following command:
$ android create project --target
--name MyFirstApp \ --path /MyApp --activity MainActivity \ --package com.acme.myapp
The app name nor the package matter. The only thing you may want to match to your current application is the <target-id>.Once the project is created, copy over the following files to the root of your own project and then delete this app folder.
- ant.properties – This is where you store your project specific properties for Ant
- build.xml – This is the generic build file provided by the Android SDK and will most likely not need changing
- local.properties – This file contains the path to your SDK, but I prefer not to copy it over since it may get checked in by mistake.
- progaurd-project.txt – This file contains ProGaurd rules if you choose to obfuscate your code, and more can be found here about that.
- project.properties – This file contains the target for your project and is most likely already part of your project.
Updating your existing projectThis is probably your best bet since it is less of a hack and should add the same set of files to your project.
$ android update project --name
--target \ --path
Once again, make sure to use the same <target-id>
Customizing the buildNow that you can use Ant to build your project, you may want to customize the build process to meet your needs. For example, I always include a build ID somewhere in the application so users can report which build they are on when reporting issues, or you may want your release build to point to a different backend server than your debug build. In any case, the Android SDK makes this also very easy. Looking at the build file, it optionally imports a custom_rules.xml file that you can create in your project root folder to hook into the build process. The build file even lists out the targets for your to include. Here is a sample one I created:
What this does, is to update the build ID in my constants class, and also update the backend server my app talks to depending on whether it is a release build or not.
Signing the appOne of the issues I mentioned early on, for the need to have build automation, was having to provide a password each time the app needed to be signed. Well, the key to solving this problem is the ant.properties file which includes your project specific properties. Unfortunately, i have not found a way around having to include your password in plaintext in some form or fashion so that it can be used, but if your an individual or have a designated build person or machine, you can lock the file down pretty good. So, to get back to the issue at hand, include the following four lines in your ant.properties file and you are good to go:
key.store.password= key.alias= key.alias.password=
If you have been building with Eclipse for while and don’t recall your keystore alias, an easy way is to go through the export process in Eclipse to release the app and see what the keystore alias is.
The build commandOK, so now that it is all set, the easy part is building the application. You can run either of the following commands to build the app for debug or release respectively.
$ ant debug or $ ant releaseGood luck. read more
How to find the key an Android app was signed with
keytool -printcert -file ANDROID_.RSAYou will get certificate fingerprints like this:
MD5: B3:4F:BE:07:AA:78:24:DC:CA:92:36:FF:AE:8C:17:DB SHA1: 16:59:E7:E3:0C:AA:7A:0D:F2:0D:05:20:12:A8:85:0B:32:C5:4F:68 Signature algorithm name: SHA1withRSAThen use the keytool again to print out all the aliases of your signing keystore:
keytool -list -keystore my-signing-key.keystoreYou will get a list of aliases and their certificate fingerprint:
android_key, Jan 23, 2010, PrivateKeyEntry, Certificate fingerprint (MD5): B3:4F:BE:07:AA:78:24:DC:CA:92:36:FF:AE:8C:17:DBVoila! we can now determined the apk has been signed with this keystore, and with the alias ‘android_key’. Keytool is part of Java, so make sure your PATH has Java installation dir in it. read more
Installing your Android App on your Phone
The some-what technical way
- Install the Android SDK. For the sake of this example we will assume it is installed in ~/AndroidSDK. (Note: For Windows you may need to installed the USB drivers as well)
- Make sure that USB storage is turned off. This is typically off by default and you can get to the screen once you have connected you phone to your PC via USB. Swipe down from the top of the screen (just like you do to see your notifications) and choose the USB Connected option.
- Make sure that you can install apps from unknown sources. To do this, go to ‘Settings’, click ‘Security’ and make sure ‘Unknown sources’ is checked.
- Now open a terminal (or command) window and run the following commands to make sure that the SDk can see your device.
> cd ~/AndroidSDK/platform-tools > ./adb devices List of devices attached XXXXXXXX5B3900XX device
- Now run the following commands to install your .apk file. For the sake of this example, our file is located at ~/dev/myApp/bin/myApp.apk.
cd ~/AndroidSDK/platform-tools ./adb install ~/dev/myApp/bin/myApp.apkYou can optionally provide a -s parameter to install the app on the SD card.
- You should now see the app in the list of your installed apps
The really easy wayFor folks that are not technical, there is an easier way.
- Go to the Google Play store and install AppInstaller (free app).
- Copy the .apk file to the SD card of your phone.
this can be done in two ways:
- If your phone has an SD card that can be taken out, you can insert the SD card into your PC and copy the file
- If your phone does not have an insertable SD card (like the Nexus S), then connect your phone to your PC via USB, turn on USB storage, copy the .apk file to your phone’s SD card and disconnect the phone
- Run the AppInstaller app
- The app will list all the apk files on your SD card. Select your .apk file to install it Note: In this case, you may still have to check the option to be able to install apps from ‘Unknown sources’