Developing android applications from command line

By : Thejaswi Puthraya

Here at Agiliq, we also develop cross-platform HTML5 mobile applications. Using Eclipse to create an android project (one time task) and edit html and javascript files for an android app is an overkill. Wouldn't it be great, if you could use your favourite text editor to edit html and js files and then fall back to the terminal to deploy the android app? We are going to see exactly this in the post.

First, let's install the android SDK starter package(I assume that you have installed JDK and ant). Head over here and install the SDK for your platform. Since I use ubuntu, I download the Linux platform SDK to my home directory. Uncompress the download and set the path to load the executables:

$ tar xvf android-sdk_r16-linux.tgz
$ export PATH=$PATH:~/android-sdk-linux/tools:~/reqs/android-sdk-linux/platform-tools
$ android sdk
Android SDK manager

If you don't wish to keep exporting the PATH variable everytime, you can set it in your ~/.bashrc or ~/.bash_profile file.

The android sdk command opens the Android SDK manager. Download the requisite SDK packages for the android versions that you plan to target. I plan to develop the app for Gingerbread and Icecream Sandwich phones and hence download the Android 4.0.3 (API 15) and Android 2.3.3 (API 10) components.

Let's check which versions of the SDK are installed from the terminal:

$ android list targets

While developing the app, we need a way to test the app and hence let's create Android Virtual Devices (AVD) that will create virtual devices that we'll run later through the emulator:

$ android avd
Android AVD manager

Create as many AVDs as the android SDK versions that you downloaded in the previous step.

To view a list of AVDs we just created on the terminal, use the following command:

$ android list avd

Let's test if the AVDs we created run on the emulator. I want to run the AVD named Gingerbread on the emulator:

$ emulator @Gingerbread
Android Emulator

We've setup the basic infrastructure for being able to develop android apps.

Creating an android project

Let's create an android project (for creating a phonegap based android project, skip to the next section):

$ android create project -n TestAndroidProj -t 'android-15' -p ~/android_proj -k com.example -a TestProjActivity

The value (TestAndroidProj) to the -n switch is the name of the project, the value (android-15) to the -t switch is the android SDK version the app targets. The value to the -p switch mentions the path of the android project. The -k switch requires a valid (java) package name and the -a switch takes the name of the initial Activity.

After running the above command, an android project should have got successfully created. Check if everything's as expected:

$ cd android_proj/
$ ls src/com/example/TestProjActivity.java
src/com/example/TestProjActivity.java

(BONUS) Installing and creating a Phonegap app

If you don't plan to create an HTML5 app or don't plan to use Phonegap, you can safely skip this section. See you later...

Immediately after installing the android SDK, clone Cordova (formerly Phonegap) from this location (or clone it from a github tag for a stable version). Add the bin directory under the cloned directory to your PATH:

$ git clone git://git.apache.org/incubator-cordova-android.git
$ export PATH=$PATH:~/incubator-cordova-android/bin

Let's create a Cordova project, which is a wrapper around android create project but additionally sets up phonegap dependencies (the jar and the js files) for you:

$ cd ~/incubator-cordova-android
$ ./bin/create ~/phonegap_android_proj com.example TestProjActivity 3

The first argument is the path of the project, the second is the (java) package name, the third is the name of the initial activity that loads your index.html and the fourth argument is the android sdk id (taken from android list targets, in my case 'android-15' id is '3').

Let's check if the project has been successfully created:

$ cd ~/phonegap_android_proj/
$ ls assets/www/
cordova-1.5.0.js  index.html  main.js  master.css
$ ls libs/
cordova-1.5.0.jar
$ ls res/xml/
cordova.xml  plugins.xml

The following sections are common to both native android projects and phonegap based android projects.

Ant commands

Now that we are done with the project and write all the code for the app, we need to be able to test it in the emulator. So let's start the emulator (mentioned above) and then start the Android Debug Bridge (adb). It is recommended you start adb as a superuser because if you later connect your phone, it will have the requisite permissions to access it:

$ sudo ~/android-sdk-linux/platform-tools/adb start-server

Check if the adb server process is running before you proceed further:

$ ps aux|grep adb
root     12589  0.0  0.0  20184   948 pts/3    Sl   16:43   0:00 adb fork-server server

Deploying the app to the emulator

In your project root, run the ant commands to deploy the app (signed with the debug key) to the emulator:

$ cd ~/android_proj
$ ant clean debug install

The app should show up on your emulator and you can test it out. If you plan to use the phone, the same set of commands will work. You might want to close the emulator then or adb will complain that both the emulator and the device are connected. To debug and view log messages use the Dalvik Debug Monitor Server (ddms):

$ ddms

Once you are satisfied with the app, you will want to sign it and publish it to the market (Google Play).

Signing the app to deploy to marketplace

First, let's generate a private key that will be used to sign the application:

$ keytool -genkey -v -keystore app_signing.keystore -alias release \
  -keyalg RSA -keysize 2048 -validity 10000

The keytool that is part of the JDK is used to create the private key. The -keystore argument's value is the name of the output file where the keys are stored. The -alias is a human readable name for the key (as multiple keys may be stored) in the keystore which can be used to refer the key later on. The encryption algorithm is set to RSA with a keysize of 2048 bits and a validity of 10000 days. Keep the generated keystore file very safe as this identifies you on the Google Play store.

After you have successfully generated your private key, let's compile the app in the release mode:

$ ant clean release

You will notice under the bin directory of your project, a file of the format <project_name>-release-unsigned.apk (in our case, 'TestAndroidProj-release-unsigned.apk'). After we are done with this, we have to sign the app with the private key we created previously:

$ jarsigner -keystore app_signing.keystore -digestalg SHA1 -sigalg MD5withRSA bin/TestAndroidProj-release-unsigned.apk release

The jarsigner utility uses the keystore (created previously) while specifying the digest algorithm, signature algorithm, the release android application and the name of the alias to be used from the keystore.

Let's verify if everything went fine:

$ jarsigner -verify bin/TestAndroidProj-release-unsigned.apk

You should get a 'jar verified' message and might want to repeat the signing process if you didn't.

Finally, let us align the generated application file (apk) before submitting to Google Play store.:

$ zipalign -v 4 bin/TestAndroidProj-release-unsigned.apk bin/TestAndroidProj.apk

'4' specifies that the files in the apk should be aligned to the 4-byte boundary. The next argument is the input signed application file and the last argument is the output file that is used to upload to the Google Play store.

The only command that we'll use frequently from the terminal is ant clean debug install and by doing so we can avoid running Eclipse (which hogs memory like there's no tomorrow).

Bonus: If you are an Emacs user, you might want to use android-mode which has key bindings for most of the above mentioned commands and other goodies.


Related Posts


Can we help you build amazing apps? Contact us today.

Comments

MiamiSoft 21st March, 2012

Hi, great information, thanks alot!

One question/comment after a first reading: you may want to indicate the separation between testing for the Android Java project setup and then the PhoneGap project.

What I mean is that some people might think that just because the two steps occur one before the other and because there are similar names that the project files are somehow linked/mixed.

Great tutorial/info.

commmenttor
Thejaswi Puthraya

@MiamiSoft
Thanks for the suggestion and glad you liked it. I have updated the post slightly to make the difference between the phonegap project and the android project clear.

commmenttor
Lee Wright

Brilliant list, has made sourcing good content for reading and blogging much better, also some great inspiration in there.

commmenttor
Aadam Gibson

We can easily aware that how to develop android SDK apps and its very skillful collected information to us.

commmenttor
Parveen Joshi

Wonderful! Any ideas if there are plans to extend to other areas of the country?

commmenttor
Lee Wright

I’ve put together a daily update that recaps the content from each of these blogs plus a few others. It will keep you up to date with what’s going on with iPhone Development. You can view online or subscribe via email at:

commmenttor
jackalbert

Mooie post over android applicaties echt in dit bericht heb je aandeel zeer interessant en informatica de belangrijkste punten voor ipad deveopers.thanks voor het delen van uw ervaringen.

commmenttor
David Wilson

This website has got some really useful info on it! I was looking for this. It was excellent and very informative. PLEASE keep it up!

commmenttor
android application development

Magnificent items from you, man. I’ve remember your stuff prior to and you are just extremely magnificent. I really
like what you’ve acquired here, certainly like what you’re saying and the way in which by which you assert it. You’re
making it entertaining and you continue to care for to stay it sensible. I can’t wait to learn much more from you. This
is really a tremendous site.

commmenttor
android application development


Magnificent items from you, man. I’ve remember your stuff prior to and you are just extremely magnificent. I really
like what you’ve acquired here, certainly like what you’re saying and the way in which by which you assert it. You’re
making it entertaining and you continue to care for to stay it sensible. I can’t wait to learn much more from you. This
is really a tremendous site.

commmenttor
David Wilson

I've recently started a site, and the information you offer on this site has helped me greatly. Also it has excellent and very informative. After going through this great content i came to know lots of things which will help me to enrich my knowledge.

commmenttor
iOS app development

This web site has some very helpful data on it! i used to be searching for this. it absolutely was glorious and extremely informative. PLEASE keep it up!

commmenttor
test

Yay google is my queen assisted me to find this great website!

commmenttor
sumit kher 25th March, 2013

This is very inspiring, as getting from the start & Having no real background in programming (aside from making some adventures on ZX-81 and MSX), I want to get started on developing something for my own Android based eBook reader and android app development training even this online course seems to be interesting http://www.wiziq.com/course/13599-professional-android-app-development-training-1-on-1-sessions. Has anyone tried any online courses so far. Please do provide a light on this also.

commmenttor
Castor Cummings

Descent step by step information of app development. Your work is really appreciable.

commmenttor
Lee Wright

Wauuuu! Cool list! Thanks!

commmenttor
Houssem Bensalem

Very helpful ! saved my day Thank you sir :)

commmenttor
Amarbhaw

This website is very helpful to me I learn a lot of it.It has good content and this website has given information in detail.

commmenttor
Amarbhaw

This website is very helpful to me I learn a lot of it.It has good content and this website has given information in detail.

commmenttor
Silvester Miller 21st Jan., 2014

Thanks to share it with us i be like it ...
Android application development

commmenttor
Silvester Miller

Android is the best smart phone...

commmenttor
Louis Vuitton Outlet

I simply couldn't go away your website prior to suggesting that I really loved the usual information an individual supply in your guests? Is gonna be again often in order to inspect new posts

commmenttor
Natan 10th July, 2014

Excellent tutorial! Thank you

commmenttor
http://www.guilhermo.com/barbour/

recognize our apology in this error the best web address is definitely:

commmenttor
© Agiliq, 2009-2012