Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Theju's withlife

Androidcommandline

tryst

Home Tech Personal Photo About 20 Mar, 2012

Developing android applications from command line


Crosspostedfrommyworkblog.

Here at Agiliq, we also develop crossplatform HTML5 mobile applications. Using Eclipsetocreateanandroidproject(onetimetask)andedithtmlandjavascriptfiles foranandroidappisanoverkill.Wouldn'titbegreat,ifyoucoulduseyourfavourite text editor to edit html and js files and then fall back to the terminal to deploy the androidapp?Wearegoingtoseeexactlythisinthepost. First,let'sinstalltheandroidSDKstarterpackage(Iassumethatyouhaveinstalled JDK and ant). Head over here and install the SDK for your platform. Since I use ubuntu,IdownloadtheLinuxplatformSDKtomyhomedirectory.Uncompressthe downloadandsetthepathtoloadtheexecutables:
$ t a r x v f a n d r o i d s d k _ r 1 6 l i n u x . t g z $ e x p o r t P A T H = $ P A T H : ~ / a n d r o i d s d k l i n u x / t o o l s : ~ / r e q s / a n d r o i d s d k l i n u x / p l a t f o r m t o o l s $ a n d r o i d s d k

Ifyoudon'twishtokeepexportingthePATHvariableeverytime,youcansetitinyour ~/.bashrcor~/.bash_profilefile. TheandroidsdkcommandopenstheAndroidSDKmanager.Downloadtherequisite SDKpackagesfortheandroidversionsthatyouplantotarget.Iplantodevelopthe app for Gingerbread and Icecream Sandwich phones and hence download the Android4.0.3(API15)andAndroid2.3.3(API10)components. Let'scheckwhichversionsoftheSDKareinstalledfromtheterminal:
$ a n d r o i d l i s t t a r g e t s

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 throughtheemulator:
$ a n d r o i d a v d

Create as many AVDs as the android SDK versions that you downloaded in the previousstep. ToviewalistofAVDswejustcreatedontheterminal,usethefollowingcommand:
$ a n d r o i d l i s t a v d

Let'stestiftheAVDswecreatedrunontheemulator.IwanttoruntheAVDnamed Gingerbreadontheemulator:
$ e m u l a t o r @ G i n g e r b r e a d

We'vesetupthebasicinfrastructureforbeingabletodevelopandroidapps.

Creatinganandroidproject
Let'screateanandroidproject(forcreatingaphonegapbasedandroidproject,skipto thenextsection):: $ android create project n TestAndroidProj t 'android15' p ~/android_proj k com.exampleaTestProjActivity The value (TestAndroidProj) to the n switch is the name of the project, the value (android15)tothetswitchistheandroidSDKversiontheapptargets.Thevalueto thepswitchmentionsthepathoftheandroidproject.Thekswitchrequiresavalid

(java)packagenameandtheaswitchtakesthenameoftheinitialActivity. Afterrunningtheabovecommand,anandroidprojectshouldhavegotsuccessfully created.Checkifeverything'sasexpected:


$ c d a n d r o i d _ p r o j / $ l s s r c / c o m / e x a m p l e / T e s t P r o j A c t i v i t y . j a v a s r c / c o m / e x a m p l e / T e s t P r o j A c t i v i t y . j a v a

(BONUS) Installing and creating a Phonegapapp


If you don't plan to create an HTML5 app or don't plan to use Phonegap, you can safelyskipthissection.Seeyoulater... 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 directoryunderthecloneddirectorytoyourPATH:
$ g i t c l o n e g i t : / / g i t . a p a c h e . o r g / i n c u b a t o r c o r d o v a a n d r o i d . g i t $ e x p o r t P A T H = $ P A T H : ~ / i n c u b a t o r c o r d o v a a n d r o i d / b i n

Let'screateaCordovaproject,whichisawrapperaround a n d r o i d c r e a t e p r o j e c t butadditionallysetsupphonegapdependencies(thejarandthejsfiles)foryou:
$ c d ~ / i n c u b a t o r c o r d o v a a n d r o i d $ . / b i n / c r e a t e ~ / p h o n e g a p _ a n d r o i d _ p r o j c o m . e x a m p l e T e s t P r o j A c t i v i t y 3

Thefirstargumentisthepathoftheproject,thesecondisthe(java)packagename, 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 a n d r o i d l i s t t a r g e t s , in my case 'android15'idis'3'). Let'scheckiftheprojecthasbeensuccessfullycreated:
$ c d ~ / p h o n e g a p _ a n d r o i d _ p r o j / $ l s a s s e t s / w w w / c o r d o v a 1 . 5 . 0 . j s i n d e x . h t m l m a i n . j s m a s t e r . c s s $ l s l i b s / c o r d o v a 1 . 5 . 0 . j a r $ l s r e s / x m l / c o r d o v a . x m l p l u g i n s . x m l

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

Antcommands
Nowthatwearedonewiththeprojectandwriteallthecodefortheapp,weneedto beabletotestitintheemulator.Solet'sstarttheemulator(mentionedabove)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 permissionstoaccessit:
$ s u d o ~ / a n d r o i d s d k l i n u x / p l a t f o r m t o o l s / a d b s t a r t s e r v e r

Checkiftheadbserverprocessisrunningbeforeyouproceedfurther:

$ p s a u x | g r e p a d b r o o t 1 2 5 8 9 0 . 0 0 . 0 2 0 1 8 4 9 4 8 p t s / 3 S l 1 6 : 4 3 0 : 0 0 a d b f o r k s e r v e r s e r v e r

Deployingtheapptotheemulator
Inyourprojectroot,runtheantcommandstodeploytheapp(signedwiththedebug key)totheemulator:
$ c d ~ / a n d r o i d _ p r o j $ a n t c l e a n d e b u g i n s t a l l

Theappshouldshowuponyouremulatorandyoucantestitout.Ifyouplantouse 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.TodebugandviewlogmessagesusetheDalvikDebugMonitorServer (ddms):
$ d d m s

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

Signingtheapptodeploytomarketplace
First,let'sgenerateaprivatekeythatwillbeusedtosigntheapplication:
$ k e y t o o l g e n k e y v k e y s t o r e a p p _ s i g n i n g . k e y s t o r e a l i a s r e l e a s e \ k e y a l g R S A k e y s i z e 2 0 4 8 v a l i d i t y 1 0 0 0 0

ThekeytoolthatispartoftheJDKisusedtocreatetheprivatekey.Thekeystore argument'svalueisthenameoftheoutputfilewherethekeysarestored.Thealias is a human readable name for the key (as multiple keys may be stored) in the keystorewhichcanbeusedtoreferthekeylateron.Theencryptionalgorithmisset toRSAwithakeysizeof2048bitsandavalidityof10000days.Keepthegenerated keystorefileverysafeasthisidentifiesyouontheGooglePlaystore. Afteryouhavesuccessfullygeneratedyourprivatekey,let'scompiletheappinthe releasemode:
$ a n t c l e a n r e l e a s e

You will notice under the bin directory of your project, a file of the format <project_name>releaseunsigned.apk (in our case, 'TestAndroidProjrelease unsigned.apk').Afterwearedonewiththis,wehavetosigntheappwiththeprivate keywecreatedpreviously:

$ j a r s i g n e r k e y s t o r e a p p _ s i g n i n g . k e y s t o r e d i g e s t a l g S H A 1 s i g a l g M D 5 w i t h R S A b i n / T e s t A n d r o i d P r o j r e l e a s e u n s i g n e d . a p k

Thejarsignerutilityusesthekeystore(createdpreviously)whilespecifyingthedigest algorithm,signaturealgorithm,thereleaseandroidapplicationandthenameofthe aliastobeusedfromthekeystore. Let'sverifyifeverythingwentfine:


$ j a r s i g n e r v e r i f y b i n / T e s t A n d r o i d P r o j r e l e a s e u n s i g n e d . a p k

Youshouldgeta'jarverified'messageandmightwanttorepeatthesigningprocess ifyoudidn't.

Finally,letusalignthegeneratedapplicationfile(apk)beforesubmittingtoGoogle Playstore. $zipalignv4bin/TestAndroidProjreleaseunsigned.apkbin/TestAndroidProj.apk '4'specifiesthatthefilesintheapkshouldbealignedtothe4byteboundary.The nextargumentistheinputsignedapplicationfileandthelastargumentistheoutput filethatisusedtouploadtotheGooglePlaystore. The only command that we'll use frequently from the terminal is a n t c l e a n d e b u g
i n s t a l l and by doing so we can avoid running Eclipse (which hogs memory like

there'snotomorrow). Bonus:IfyouareanEmacsuser,youmightwanttouseandroidmodewhichhaskey bindingsformostoftheabovementionedcommandsandothergoodies.

PostComment

You might also like