devworx March 2013

100
To A 9.9 Media Publication YOUR GUIDE TO MOBILE APPLICATION DEVELOPMENT Intro to Android | The Android SDK | Developing for Android | GUI Development | The Android NDK | Developing for Android x86 | Debugging on Android | Intel Hardware Accelerated Execution Manager Android on Intel Architecture

description

devworx March 2013

Transcript of devworx March 2013

Page 1: devworx March 2013

To

A 9.9 Media Publication

Your guide to mobile application development

Intro to Android | The Android SDK | Developing for Android |GUI Development | The Android NDK | Developing for Android x86 |

Debugging on Android | Intel Hardware AcceleratedExecution Manager

Android on Intel Architecture

Page 2: devworx March 2013
Page 3: devworx March 2013

Android

Intelon

Architecture

Page 4: devworx March 2013

05 Android ndKGetting under the hood of Android and refining your app would need you to be familiar with native code.

C o n t e n tsi

nte

rme

dia

teA

dv

an

ce

d

03 develop for Android

All you need to know while developing for Android

07 debugging on Android

With every product comes troubleshooting.

01 introduCtionThrough the course of this booklet, you’d be able to develop an app for the Android platform and find relevant details to cater to the Intel x86 Android portfolio.b

eg

inn

er

Page 5: devworx March 2013

02 getting stArtedThe Android SDK when paired with an IDE and API libraries, platform tools gives you a complete environment.

06 developing for x86

x86 is no longer restricted to PC. Here’s quick way to get started with Android for Intel devices.

04 gui developmentUser interface is a driving factor in the success of your app. Here are tips to stand out of the crowd.

Page 6: devworx March 2013

Editorial

Assistant Editor

nash david

Writers

Ankit mathur & soham mondal

with inputs from intel

dEsign

Sr. Creative Director

Jayan K narayanan

Sr. Art Director

Anil vK

Associate Art Directors

Atul deshmukh & Anil t

Sr. Visualisers

manav sachdev & shokeen saifi

Visualiser

baiju nv

Consulting Designer

vijay padaya

brand

Product Manager

Arun Yadav

Manager Online

shauvik Kumar

Asst. Product Manager

Kshitij garg

Cover Design

vijay padaya

© 9.9 Mediaworx Pvt. Ltd.no part of this book may be reproduced, stored, or transmitted in any form

or by any means without the prior written permission of the publisher.

March 2013free with digit. if you have paid to buy this book from any

source other than 9.9 mediaworx pvt. ltd., please write to

[email protected] with details

android for X86> Your guide to mobile application development >

Page 7: devworx March 2013

Chapter 1

Introduction

Through the course of this booklet, you'd be able to develop

an app for the Android platform and also find the relevant details

to cater to the Intel x86 segment in the Android portfolio.

Page 8: devworx March 2013

6 androId for X86

As most would believe –

Android is a mobile oper-

ating system that is open

source and free for anyone

to use, modify, develop

and distribute. Nonetheless, there are some who rather prefer calling it

an application stack where components are easily switchable suitable and

exciting alternatives, thereby increasing variety. The only restriction you

could think of would be modification of the core platform (such as not bun-

dling the Play Store, Gmail or Calendar). Including these requires a licence

and ensure that you comply with Android specifications while marketing

your application. While this has made it difficult for manufacturers to dif-

ferentiate their products in the market, it has enabled Google to keep the

concerns of fragmentation at a minimum and made it easier to consolidate

the Android market.

This has however never stopped hardware manufacturers from selling

products after putting on custom skins (Sense UI by HTC) and enabling

proprietary device specific features (Multiwindow and the floating video

player by Samsung) on their respective devices.

Open Handset AllianceAndroid is led by an entity known as the Open Handset Alliance, the organi-

sation that made such an open platform possible. Though it is led by Google,

there are a total of 84 members (as of today) who have made some major

Page 9: devworx March 2013

7androId for X86

contributions towards the development of the Android platform. Of these,

34 of the member companies are founding members which include Mobile

operators such as NTT DoCoMo, and T-Mobile; semiconductor companies

such as Intel, Nvidia, Qualcomm and Synaptics; handset manufacturers such

as Samsung, HTC, Sony, LG and Motorola; and software companies such

as eBay and Nuance Communications. Since then, most major companies

related to electronics, communications and software have joined the effort

of making Android better day by day.

These are those companies that not only support the ecosystem by

bringing in devices that run Android, but also invest a significant amount of

money and resources into it. These members who produce Android devices

have to sign a contract that they will always support the community by not

selling incompatible versions of Android. This not only results in much

faster development of the platform, but an eventual merging of most of the

features into the mainline, that a particular manufacturer develops. This

reduces effort and develops trust among the competing manufacturers in

the market, which are also benefits the community which has grown with

a huge number of modders and hackers who continue to make their own

innovations on the software.

Developer concernsFrom a developer's perspective, however, this shouldn't cause many prob-

lems because Android apps are promised to run similarly from the ground

up, as long as you take into consideration the varying screen sizes and

resolutions. This might be an advantage for many, but it is also the reason

why many developers choose iOS first over Android, though that gap is

narrowing day by day. The very fact that iOS is restricted to one mobile

handset and tablet which is equally popular makes development and testing

very easy, due to which developers and designers are able to offer a much

better overall finish and user experience to their apps. Another reason

might be the perception that iOS users are more likely to actually make a

purchase and invest in an app than their Android counterparts who often

use cheaper handsets which are often outdated and are not even available

in the market. The main problem with such devices firstly is that they are

not even available for testing by the developer and moreover they run much

older versions of Android because the carrier or device manufacturer haven't

released an update to the latest Android version. Most popular developers

have often stated that they have to test the app for hundreds of handsets

Page 10: devworx March 2013

8 androId for X86

before actually releasing it in the app store.

Keeping aside the downsides though, Android app development is

actually much easier and offers much more flexibility. Android apps are

allowed to change almost any component of even the base system, ranging

right from the launcher to even the messaging and music application. This

is the kind of customisability that iOS will never be able to provide. What's

more apps are able to share data among themselves much more easily

using intents, which is a great way to touch the boundaries of freedom.

Android development also runs at a much faster pace than its competitor,

so most of the quirks or irritations that you might have usually tend to get

addressed in the next release or so. This might also mean that you have to

make sure your app works for users right from Android 2.3 Gingerbread

to JellyBean 4.1/4.2.

A brief historyAndroid has grown a lot through the past few years, when it first came out

in November 2007 as a beta, it already had an SDK to go with it. The first

official release was however only a year later in September 2008 with fea-

tures such as the Android Market (now known as the Play Store), camera,

Wi-Fi, Bluetooth and sync support in addition to instant messaging. The

first major release that turned out to be quite popular with its dessert name

was Android 1.5 “Cupcake”. This release brought along with it some major

features and refinements like screen rotation, on screen keyboard with text

prediction, app widgets, Bluetooth audio streaming with A2DP and a newer

Page 11: devworx March 2013

9androId for X86

Linux kernel 2.6.27. Eventually development sped up with release of more

candies like Donut, Eclair and Froyo. Each of which brought about a slew

of features like a new Text to Speech Engine, a new search framework along

with gestures and accessibility support for Donut. Android 2.0 Eclair, as

expected turned out to be a big release from a user's perspective, which

also introduced the much popular WebView support for developers. But

the next release i.e. Froyo, turned out to be a bounty for developers which

featured cloud to device messaging for two way push notifications, crash/

bug reporting and allowed apps to be installed on the external SD card.

The most popular platform till date however, which still holds a sizeable

portion in the amount of active Android devices has been Gingerbread or

Platform version API level Version Code

Android 4.2 17 JELLY_BEAN_MR1

Android 4.1, 4.1.1 16 JELLY_BEAN

Android 4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1

Android 4.0, 4.0.1, 4.0.2 14 ICE_CREAM_SANDWICH

Android 3.2 13 HONEYCOMB_MR2

Android 3.1.x 12 HONEYCOMB_MR1

Android 3.0.x 11 HONEYCOMB

Android 2.3.4 Android 2.3.3

10 GINGERBREAD_MR1

Android 2.3.2 Android 2.3.1 Android 2.3

9 GINGERBREAD

Android 2.2.x 8 FROYO

Android 2.1.x 7 ECLAIR MR1

Android 2.0.1 6 ECLAIR_0_1

Android 2.0 5 ECLAIR

Android 1.6 4 DONUT

Android 1.5 3 CUPCAKE

Android 1.1 2 BASE 1_1 (Internally known as “Petit Four”)

Android 1.0 1 BASE

Page 12: devworx March 2013

10 androId for X86

Android 2.3. This release added some major features like NFC, Gyroscope

and SIP protocol support. It also added several improvements to the UI and

available controls. Later came Honeycomb with major focus for tablets and

a UI overhaul which was to be integrated in Ice Cream Sandwich later. ICS

came with support for Android Beam, unified social API, VPN client API,

Text to Speech API and most importantly unified framework for phones

and tablets. Jelly Bean, the most recent revision has added features for

developers like USB Audio output, app encryption, app verification, script

intrinsics and filterscript support.

The API levels mentioned in the table represent a unique integer value

that specifies the corresponding revision of the framework API. It is used to

identify the minimum, maximum and target API level at which the applica-

tion can work. Hence, it is used to manage the compatibility requirements

of various Android apps, who need to specify their API requirements in

their manifest. The version code on the other hand have always represented

the official name by which newer Android versions are known as. These

have been named alphabetically after desserts ever since the Android 1.5

version which was called “Cupcake”. There have been no official names

starting with A and B, though there have been rumours that these had

some internal references within the company.

Android ArchitectureNothing beats the way architecture has been explained in the official docs,

still the overall software stack consists of overall five components that

make the whole platform work. These are a set of loose software layers that

work with each other like any other computer operating system, just that

they are heavily optimised specifically for mobile environments. The fact

that it is based on the Linux kernel results in the fact that it is even more

robust, community tested and portable to run on almost any other device.

The dalvik virtual machine provides a layer of abstraction, just like a Java

VM, so that apps are able to run without worrying about the underlying

platform of the mobile or tablet device. This is precisely the reason why

Android has been easily ported to unconventional devices like watches,

refrigerators and in-car entertainment systems.

KernelAs, we discussed before, Android is based entirely on the Linux kernel

Page 13: devworx March 2013

11androId for X86

which directly interfaces with the

mobile hardware to handle drivers

for each functionality and handles

memory, power, network and process

management. This is the layer on which

the Dalvik Virtual Machine and core

libraries run. The kernel being open

source has prompted a number of hob-

byist hackers and modders to release

their own popular versions of kernels

for specific devices. It has often been an

important topic of discussion for people

who want to tweak their CPU gover-

nors, I/O schedulers and custom kernel

modules. This has given rise to an inter-

esting community that customises their

Page 14: devworx March 2013

12 androId for X86

devices to suit their preferences. There have been instances when Android

has been ported to run even on phones that do not officially bundle it.

Dalvik Dalvik is the most important component that runs Android applications.

It resembles the Java Virtual Machine in the sense that code written in Java

is converted to bytecode and then into a Dalvik executable (.dex) which is

done with the dx tool. This particular format has been used because it is

very beneficial for memory and power limited devices and runs without

any swap space available, as opposed to Java which has considerably higher

requirements.

LibrariesLibraries are exactly what they sound like – low level software bundles

that handle basic device functions such as 3D hardware acceleration with

Page 15: devworx March 2013

13androId for X86

OpenGL, Surface Manager as the display subsystem to handle multiple

layers of software applications. This layer includes an implementation of

the standard C system library (libc). The SQLite database used for storing

information is also an important component that is used extensively.

ApplicationsApplications are the components that app developers like you or me develop.

These can be system apps like SMS messaging, E-Mail, Home Launcher,

Contacts, Dialer, Settings etc. Basically, anything that is presented in front

of you is part of app that has a specific lifecycle.

App Frameworks App frameworks are a set of services that expose an API and provide access

to device functions like displaying views, accessing data, pushing notifica-

tions to the main notification bar, application lifecycle management etc.

Page 16: devworx March 2013

14 GettinG StartedChapter 2

Getting Started

while The Android SdK allows you to get started with

app development, when paired with an ide and APi libraries,

platform tools, AdT plugin and the Android system image it gives

you a complete environment to ease and supplement the pro-

cess of development, testing and debugging your application.

Page 17: devworx March 2013

15GettinG Started

Basic PrerequisitesThe basic system requirements listed by the Android developer website

are as follows:

Ω windows XP (32-bit), Vista (32- or 64-bit), or windows 7 (32- or 64-bit)

Ω Mac oS X 10.5.8 or later (x86 only)

Ω linux

• GNU C Library (glibc) 2.7 minimum.

• Ubuntu version 8.04 or later.

• 64-bit distributions should run 32-bit applications.

The SdK and other tools procured from the official website have been

tested for Linux only on the Ubuntu distribution. Though other distros

might have their own packages in their official repositories which are com-

munity tested. we would recommend using those instead, because setting

up and updating them would be much more easier and streamlined.

other than these platform requirements, Android SdK requires JdK 6

(Java development Kit) to be installed on your operating system. it should

be stressed specifically that Gnu compiler for Java (GCJ) is not supported

and older versions of the JdK are also incompatible. By default, the ide

that is supported by the Android SdK is eclipse ( At least eclipse helios

3.6.2), which is actually even bundled with the AdT bundle for your spe-

cific platform.

Getting the SDKThe SdK can be procured from the official website and is available as

part of an AdT bundle or as a separate SdK Tools bundle if you have a

development environment already set up. As mentioned before, the AdT

tools bundle already includes everything you need in order to get started

with running a new development environment for Android including the

eclipse ide. The package is available for all the three platforms including

linux, windows and Java. it can be downloaded from the Android SdK

download page (dvwx.in/VKWyKI), which will show you a “download the

AdT Bundle for linux/windows/Mac” button on the right as per your

present platform. if you want to download it for a platform which is not

your current one, you can click the “download for other platforms” link at

the bottom and it will show you the list of platforms which are supported

for the AdT Bundle as well as SdK Tools.

if you feel that you need just the SdK and have to integrate it with some

Page 18: devworx March 2013

16 GettinG Started

existing IDE, you can click the “Use Existing IDE” link, which will show

you the “download SdK Tools for linux/windows/Mac” button.

Setting up with the ADT bundleTo install and set up the AdT bundle, all we need to do it is to have a Java

JdK already installed on our system and launch the eclipse ide after

extracting the zip file that comes in the download. So, basically what we

do is the following:

1. Make sure that JdK and not just Jre is installed on your system.

2. Unzip the ADT bundle archive that came after being downloaded

from the Android developer website and place it where you would

want your eclipse and Android SdK directories to reside. Make

sure you do not change the folder structure, because changing it will

result in you having to change the Android SdK preferences in the

AdT plugin for eclipse.

3. Add the full path to the sdk/platform-tools and sdk/tools subdirectory

to your PATh variable so that you can access the relevant commands

from anywhere, otherwise you will need to type in the full path to each

command everytime you run it.

Finally, all you need to do is to launch the eclipse environment and choose

a default workspace where all your development related files will be stored.

On existing Eclipse environment

Windows

The windows Android SdK installer

comes as an executable which

exposes a wizard like interface which

checks for dependencies and allows

you to choose a location to install

your SdK. Since these are only the

SdK tools, it will only install the

bare minimum packages required

to start development. You will still

need to configure this from eclipse

and download more packages like

the Android Platform Tools and the

Android System image which will

Page 19: devworx March 2013

17GettinG Started

be used for the emulator. You might also need to add the path of the sdk/

platform-tools and sdk/tools subdirectories to your PATh variable in order

to use common Android SdK utilities.

Mac / Linux

The Mac and linux SdK comes as a zip file which can be extracted and

put anywhere you intend to keep it. Finally, as in windows – add the sdk/

platform-tools and sdk/tools subdirectories to your PATh variable with

the following methods:

Linux

edit the ~/.bash_profile or ~/.bashrc files looking for a line that sets the

PATh variable.

1. Add the full path location to your $SdK_rooT/tools location for the

PATh variable.

2. if no PATh line exists you can add the line by typing the following:

3. export PATh=$PATh:<path to your $SdK_rooT/tools>

Mac

in the home directory locate the .bash_profile and locating the PATh vari-

able add the location to your $SdK_rooT/tools folder.

Setting up the Eclipse IDEnow that everything is set up and the development environment is good to

go, all that we have left is to procure the eclipse plugin and install additional

packages that include the target platform and other APis after integrating

it with the exisiting ide on your system. if you still want to install the ide

by yourself, you can do so by getting the eclipse for Mobile developers

package at the eclipse.org/mobile/ website which is again available for all

the three popular platforms. To install the eclipse AdT plugin, we need

to add the Google repositories in its Add-On installation dialog. This can

be done as follows:-

1. Start eclipse and navigate to help > install new Software.

2. Click the Add button and name the repository as something like

“Android AdT”. The online location for the repository is :

https://dl-ssl.google.com/android/eclipse/

3. it will list a couple of available sites, from which you can choose

Page 20: devworx March 2013

18 GettinG Started

“developer tools” and check the components you would like to be

installed in the next screen.

4. Finally, accept the license agreements and restart eclipse when the

installation and download is complete.

5. when eclipse restart is complete, you will need to set the location

where you installed your Android SdK. You can do this directly in

the “welcome to Android” window or go to window > Preferences and

choose Android on the left, which should show you a field to specify

the Android SdK location.

This concludes the configuration of the eclipse development environ-

ment, following which only additional packages like target platforms and

APis are left to be installed.

Target platforms and APIsAs a last step towards environment setup, you must install at least one

android platform for which you are aiming to target you app. it will be

used to set up the emulator on which the system image will be used to run

your application. The AdT bundles the lastest available android platform

by default. if you neeed a slightly older version for which you are targeting

your app, like Android 4.0.4 or Android 4.1 or you had downloaded the

SdK Tools instead of the bundle, then you will have to download it from

the Android SdK Manager. To launch Android SdK Manager on linux or

Mac, use the “android sdk” command to launch the Android SdK Manager

dialog. on windows, you can launch it after locating the SdK Manager.exe

file from the SdK directory. Alternatively, it can also be launched from the

eclipse ide itself, by clicking the corresponding Android SdK Manager

button below the menu bar.

when you launch the SdK Manager, it should start downloading the

list of available packages from the server and then show you a list of those

which are already installed along with the ones which are available. At the

minimum, make sure you have at least all the Tools packages, one Android

platform (like Android 4.2) and the Android Support library installed, to

be able to work sufficiently in your development environment.

x86 system ImageTargeting your apps for x86 Platform is very easy. You need to select and install

Android x86 System image in Android SdK Manager and within couple

of minutes, your Android x86 System image is up and ready to build apps!

Page 21: devworx March 2013

19GettinG Started

Third party addonsBesides the default addon packages for Android SDK available from Google,

a number of third party websites might host customized add ons for their

own specific devices. These can be carriers, manufacturers or software

development companies as well. To enable the third party addons you

need to find a link to the repository.xml file for that particular repository,

and add them to the SdK Manager. To do this, click on the Tools > Manage

Add-on Sites. This will show you the Add-on Sites dialog box, where you

can find the User defined sites tab. Click on New and add the link to your

specific repository file. To refresh the list of available packages, click on

Packages > reload.

Creating a new Android Virtual DeviceAn Android Virtual device stores the configuration for the emulator

according to which it will do the emulation for your application. By default

Google provides emulation profiles for all the old and new Google Nexus

devices. Manual configuration can also be done for other devices. Android

Virtual devices or AVds can be created from the AVd manager, which

provides a decent GUI for configuring and editing the devices.

The AVd manager can be launched from the Android SdK manager too,

by clicking on Tools > Manage AVds. in the AVd Manager screen, click on

Page 22: devworx March 2013

20 GettinG Started

New, which will show a Create new AVD dialog box with various configu-

rations. it will allow you to name your device and specify the target. For

example, if you intend to develop for intel based devices, you would choose

“intel Atom x86 System image” that you should have installed before. it will

further ask you for device configurations like size of the SD Card, screen

size, DPI, RAM size etc. After all this configuration, click on Create AVD

and you device should be listed on the main screen. You should now be able

to launch the emulator by clicking on the “Start” button after selecting the

device you want to launch.

Using the Android System EmulatorThe emulator can be controlled easily from the on screen buttons or with

the help of keyboard shortcuts. There are also various commandline

options to turn on or off specific set of functionality. The keyboard short-

cuts are quite important and are listed in the table, as referenced from the

official documentation.

Page 23: devworx March 2013

21GettinG Started

Emulated Device Key Keyboard Key

Home HOME

Menu (left softkey) F2 or Page-up button

Star (right softkey) Shift-F2 or Page Down

Back ESC

Call/dial button F3

Hangup/end call button F4

Search F5

Power button F7

Audio volume up button KEYPAD_PLUS, Ctrl-F5

Audio volume down button KEYPAD_MINUS, Ctrl-F6

Camera button Ctrl-KEYPAD_5, Ctrl-F3

Switch to previous layout orientation(for example, portrait, landscape)

KEYPAD_7, Ctrl-F11

Switch to next layout orientation(for example, portrait, landscape)

KEYPAD_9, Ctrl-F12

Toggle cell networking on/off F8

Toggle code profiling F9

Toggle fullscreen mode Alt-Enter

Toggle trackball mode F6

Enter trackball mode temporarily(while key is pressed)

Delete

DPad left/up/right/down KEYPAD_4/8/6/2

DPad center click KEYPAD_5

Onion alpha increase/decrease KEYPAD_MULTIPLY(*) / KEYPAD_DIVIDE(/)

Android SDK with IntelliJ IdeaSetting up Android SdK with Android SdK does not require much configu-

ration after the SdK has been configured as before. The only difference is

that you need to download the SdK Tools package and manually configure

it instead on downloading the SdK bundle. Since, intelliJ idea supports

Android app development by default, you don't need to download or install

anything to enable support, only a little bit of configuration is required. The

Page 24: devworx March 2013

22 GettinG Started

main prerequisite is that you need to have the Java JdK and Android SdK

configured. Android support in intelliJ idea is provided with the Android

Support plugin that is bundled by default. if however you find that it is

not available, you can enable it in the plugins configuration wizard or the

plugins tab in the settings dialog box.

The only remaining configuration that remains regarding the Android

SdK and Java JdK can be done by adding a new Android project or adding

an Android facet to an existing project. when you do so, the respective

wizards will automatically ask you to specify the location of the Android

SdK and Java JdK.

Android SDK with NetBeansThe process for configuring Android SdK is also similar with netBeans.

Again, the Android SdK and Java JdK obviously need to be set up and

configured first. The Android app development functionality is provided

in netBeans with the help of the nbandroid plugin for netbeans. To install

this plugin, following steps need to done :-

1. Go to Tools > Plugins. Add the update center link for nbandroid to the

list in the Settings tab : http://kenai.com/projects/nbandroid/downloads/

download/updatecenter/updates.xml

2. now in the Available Plugins tab, install the Android plugin that

should now be available.

3. To configure the path of Android SdK, go to Tools > options > Miscel-

laneous > Android.

4. Mention the location of the Android SdK in the available field.

You should now be able to easily create new Android projects and

applications.

optionally you can also install the plugin manually by installing it from

a downloadable zip file. This can be done as follows :-

1. download the plugin from the nbandroid website : http://kenai.com/

projects/nbandroid/downloads

2. Unzip the file and go to Tools > Plugins > Downloaded

3. Click on Add plugins and select those which match the vsersion of

your current ide.

4. Click on the Install button, which will ask you to accept the licenses

after which installation will begin.

it should be noted that Android development with other ides is rec-

Page 25: devworx March 2013

23GettinG Started

ommended only when you are accustomed to their interface or already

use them on your system. if you are a new Android developer, the official

eclipse plugins are more than enough and will get better community sup-

port if you face any problems with the plugin or during the process of

your development.

Android SDK in Other Linux DistributionsTo give you an example of setting up a development environment in other

linux distributions, it is sometimes much easier to get the SdK from the

official repositories which provide it in the form of a preconfigured package.

Arch LinuxArch Linux, for example provides two AUR (Arch User Repository) packages

by the name of android-sdk and android-sdk-platform-tools. The process

for installing these AUR packages is as usual and these provide access to

the Android SdK quite easily. if you want to use eclipse as you development

environment, a package called eclipse-android is also available.

Intel Hardware Accelerated Execution Managerintel hardware Accelerated execution Manager is a hypervisor (or a Virtual

Machine Manager) that greatly speeds up the execution of the commands

in the emulator. intel claims that it does this by almost 5 to 10 times the

performance of an emulator without the presence of hAXM. The sur-

prising fact is that it is actually true and apps are noticebly much faster

and even ndK based games and resnderscript based heavy computations

are supported which suddenly become much more smoother to run and

thankfully no longer require a real development device to be bought for

testing and development purposes. The result is that it not only saves time,

but also a lot of money.

hAXM works on intel-VT enabled processors and is supported on all

the three platforms namely windows, Mac oS X and linux as long as

they are on intel processors and use the intel Atom X86 System image. it

uses the hardware virtualization features built into the intel processors,

as opposed to much slower software based emulation, where most of the

virtualization is done at the hardware level on the host processor. it even

supports GPU emulation (since SDK release 17), so that OpenGL calls can

be executed on the host. imagine the amount of graphics performance that

Page 26: devworx March 2013

24 GettinG Started

is enhanced by this very functionality, where only the peripheral emulation

is left to be handled by the software.

The installation of hardware Accelerated execution Manager requires

the following minimum system requirements:

Intel processor with VT support

• 1 GB RAM

• (Mac OX 10.6 Snow Leopard / 10.7 Lion) / (Microsoft Windows 7, Vista,

XP SP2 32 bit) / (Ubuntu Linux 64 bit recommended)

• Android SdK

• Android X86 System image

• JDK

All the above system requirements are checked by the hAXM installer, which if not met, makes the installation to be failed. To check if your processor

is supported or includes the VT technology, you can check the system BioS

or refer to the intel website.

To install hAXM, it can be procured directly from the intel website or

from within the Android SdK Manager itself from under the Available

packages below extras folder.

Getting Intel HAXM from the Android SDK

1. Start the Android SdK manager with the android sdk command.

2. Go to Extras and check the Intel x86 Emulator Accelerator (HAXM)

entry.

3. Click the install button.

4. review license Agreement and Accept it, followed by which you need

to click install Again.

5. The installer will be downloaded to the tools directory under the Android

SdK folder from where it can be run to continue the installation.

Installation of Intel HAXM on Windows

1. run the installer that you downloaded from the intel website or from

the Android SdK Manager.

2. Accept the license Agreement and Click Next.

3. The installation wizard will ask you to allocate the amount of rAM you

want to use for hAXM. By default this setting is 1024, if you want to use

more than 2048, it is recommended that you only use a 64 bit system.

4. when the installation is finished, go to command prompt and check if

Page 27: devworx March 2013

25GettinG Started

it is running by typing the command sc query intelhaxm. This should

show you a status as running, if not then try starting it and if you face

any errors, try reinstalling the whole software.

5. Additionally you may see some errors regarding intel Xd or intel

VT-x not being enabled, in which case, you will need to go into the

BioS and enable this setting. if the installer says that either of these

settings is not supported by your processor, then you will not be able

to use this software.

Installation of Intel HAXM on Mac OS X

installation of intel hAXM on Mac oS X is similar to windows with the

help of the installation wizard where you will need to specify the amount

of memory to be allocated. To check if hAXM is running, you can run the

kextstat | grep intel command which should show you the kernel extension

named com.intel.kext.intelhaxm to be loaded in memory.

Installation of Intel HAXM on Ubuntu

Ubuntu Linux is one of the main platforms on which a lot of developers

use eclipse and AVd to work on building their Android applications. A

From the Android package manager, install Intel Hardware Accelerated Execution Manager

Page 28: devworx March 2013

26 GettinG Started

lot of build servers are also based on linux and hence it is very important

for intel to support hardware Assisted KVM Virtualization in a manner

similar to hAXM on windows and Mac. The following KVM installation

instructions can also be referred from the Ubuntu Community documenta-

tion page for KVM.

Installing KVM1. The first task is to check for prerequisites and see if the required

configurations are indeed suppoted on your system. So, to check if

your processor supports hardware virtualization, run the following

command :• egrep -c '(vmx|svm)' /proc/cpuinfo

if the output value turns out to be zero then your processor does not

support hardware virtualization and if it somehow turns out to be greater

than that, you are good to go!

2. To check if KVM is suppported on our system, we need to install the

CPU checker utility, with the help of the following command: -

sudo apt-get install cpu-checker

when this is done, you will be able run the kvm-ok command to see if

KVM is supported.

3. Finally, if you want to allocate more than 2GB RAM to a VM, then it

is required that you have a 64 bit system. To check for that, run the

following commands:• egrep -c ' lm ' /proc/cpuinfo

if the output is 1, then it means that you are running a 64 bit system. in

order to make sure that you are also running the 64 bit kernel currently,

you can run uname -m command to check for the architecture.

4. To get started with the installation, the required packages can be

installed from the repository as follows: • sudo apt-get install qemu-kvm libvirt-bin ubuntu-

vm-builder bridge-utils

5. it will ask for postfix configuration during the install process, which

you can ignore by choosing the no Configuration option.

6. To start using the KVM virtualization, you need to add the user that

will be running the software to the kvm and libvirtd groups.

7. log out and log in again to make the changes effective.

8. To see if everything has been set up, check the output of • sudo virsh -c qemu:///system list.

Page 29: devworx March 2013

27GettinG Started

9. To start the emulator from the commandline, you can execute:• emulator-x86 -avd Your _ AVD _ Name -qemu -m 2047 -enable-kvm

10. Alternatively, to use it from eclipse you will have to change the run

Configurations for the Android Application Project. There you will

have to choose the x86 based AVd that needs to be run as the target

and in Additional commandline options, you will have to specify the

following: -qemu -m 2047 -enable-kvm

Page 30: devworx March 2013

28Chapter 3

Developing for Android

Given the rise of Android in the indian market, and

the variety of mobile devices, an app's availability on the

Google Play store gives it immense visibility. With this advan-

tage comes another challenge--that of customisation.

Page 31: devworx March 2013

29Developing for AnDroiD

The Android building blocksAfter installing the sdk, the first major task is to understand the basic

building blocks that make Android up. Once we've gotten familiar with

the basic building blocks of Android we will write a simple Android

application to illustrate how to use them in a rather practical setup.

the basic Android building blocks are:

• Activities

• Intents

• Views

• Content providers

• Services

• Notifications

• Android manifest

Before we proceed to building an app, let's go through each

building block and understand what role they play in the life of an

Android application.

ActivityAn activity is a core building block and is at the heart of most Android

applications out there. to put it simply, an activity is what you see on

a screen in Android. Users can interact with this screen to accomplish

a particular task.

As an example, consider the dialer

screen as an Activity. Whenever a user

wants to dial a number and make a call, he/

she must use the dialer 'Activity' or screen

to enter the number.

While the user inputs the digits to be

dialled, each activity is given a window

in which to draw its user interface and a

developer can specify how he/she wants

the activity to look.

As you might guess, an Android appli-

cation might have multiple screens and

therefore might contain multiple activi-

ties. For example, a dialer application

Page 32: devworx March 2013

30 Developing for AnDroiD

might have an Activity where you enter

the number to be dialed, once you hit 'dial'

it might invoke another Activity that shows

the number which is being dialed and so on.

so, applications typically have multiple

Activities in Android and the activity which

is presented to the user when launching the

application for the first time is called the

'Main' Activity.

An Activity also has a lifecycle of its

own and at some point in the same lifecycle

the Activity is created, started, paused,

stopped and eventually destroyed.

in other words, when the screen is

shown on the screen - in developer termi-

nology, it comes to the foreground - the Activity is 'created' and then

'started'. so any basic setup for the activity is done in the onCreate

method, which is called when the Activity is getting created before

being shown on the screen, typically, you would be setting up the User

interface of the Activity here. similarly, there are similar methods/call-

backs like onstart, onresume, onPause, onstop and ondestroy which

are called when the Activity is being started, resumed, paused, stopped

or destroyed respectively.

A quick note here, an activity which is in the foreground (directly

Source: android.google.com http://dvwx.in/Y0mCh9

Page 33: devworx March 2013

31Developing for AnDroiD

visible on the phone) is said to be in an 'Active' state and is able to

perform operations, as soon as the user starts another Activity, the old

Activity is 'paused' and stopped and then 'pushed' on a stack called the

'Activity stack'. in this state, the activity is no longer able to perform

any operations. if the user goes back from the new Activity to the old

Activity, the old Activity is then 'popped' from the Activity stack and

then 'started' and 'resumed' to go back to normal operation.

IntentAs mentioned previously, there might be multiple Activities in an

Android application and we might need to move between them. An intent

is a mechanism by which we can move between Activities or between

other Android building blocks such as services. More on that later.

Let's take the example of the dialer application again, when you are in

the Android home screen, you are basically in the home screen Activity,

here when you click the dialer button, you use an 'intent' to launch the

dialer application 'Activity'. there you enter the phone number that

you want to dial and hit the dial button which uses another 'intent' to

launch the dialing 'Activity'.

You can also pass data in an intent, for example, when you enter the

phone number of a person in the dialer and hit dial, you might want to

pass the phone number as a parameter to the next Activity and you can

pass that using the intent itself.

Viewsviews are the basic User interface building blocks in Android and an

Activity is typically made of many such atomic 'views'.

simply put these 'views' know how to draw themselves on the screen

and they respond to user events like a click or a long press. example of

views in Android are textviews which show simple text on the screen,

imageviews which draw images on the screen, edittext which let users

enter text and so on.

views are also hierarchically arranged in the form of trees to draw

fairly complex user interface layouts. For example, here in the twitter

app, 4 imageviews (the twitter logo, the refresh button, the tweet button

and the search button) are arranged linearly to form one atomic element

called the 'viewGroup'. since we arranged all the elements linearly (in

a straight line) we call it a LinearLayout viewGroup.

Page 34: devworx March 2013

32 Developing for AnDroiD

similarly if you look a tweet row in the twitter app, the various text-

views and imageviews are arranged in a row to form another viewGroup.

the idea is that we can arrange all these individual views together in

a way that makes sense to the user and all Android applications group

views together to make very complex layouts like the one shown in the

twitter app.

Also note that the layout which specifies the arrangement of the

Page 35: devworx March 2013

33Developing for AnDroiD

individual views is typically mentioned in a separate xml file. defining

the layout in an xml file enables Android to conform to the Model view

Controller architecture pattern and helps separate the User interface

component from the actual code.

Content ProvidersAs you might know databases are used to store and retrieve information

and Android provides an inbuilt database called sQLite to achieve that

functionality. it also provides an additional functionality called 'Content

Provider' to enhance the functionality of a database in a mobile scenario.

so a Content Provider is a wrapper on a database that provides

additional functionality to share data with multiple applications and

to manage data security with respect to the same. Let me illustrate that

with an example.

Let us say there is a contact application that does contact management

on an Android phone. As discussed before, that application might have

multiple Activities within it, like an Activity to add a contact, another

Activity to view all the contacts, another one to edit a contact and so on.

When you add a contact, instead of directly asking the database to add

a row for the contact, you would ask the 'Content Provider' to add that

row for you. the Content Provider would then have the logic of knowing

where to store the row, in the database, in a file or even in the cloud.

now say there is another application, say an sms/text app which

Page 36: devworx March 2013

34 Developing for AnDroiD

needs access to a persons phone number. now under normal circum-

stances, getting access to that data would be a cumbersome job (sharing

a database is not always straightforward) but thanks to the ContentPro-

vider which makes data sharing easy, we can do that in a flash. so when

the sms app needs access to the phone number, it queries the contacts

'ContentProvider' which in turn knows where the data is stored and

where it should look. Once the query is complete, it then returns the

results back to the sms application. this provides a very simple and

straightforward approach to sharing data between multiple applications

while maintaining security.

ServicesYou might recall that while discussing Activities, we saw how Activi-

ties are 'paused' and 'stopped' when they go into the background. You

might think that this might be a problem for applications that need to

keep working in the background for a long time, like a music player or

a download application. Apps like that might have a tough time per-

forming while in the background and need a way to keep playing music

or continue downloading even when they are not in the foreground.

For such functionality, a 'service' should be used. in other words, a

service is a faceless task that runs in the background and has no user

How it all comes together...

Page 37: devworx March 2013

35Developing for AnDroiD

interface. You can enable it to run for a specific amount of time and do a

certain task. Any task which should be done even if the corresponding

Activity might be pushed to the background should be delegated to

a service.

now that you are slowly understanding the basic building blocks of

Android, you are starting to get a picture of how Android applications

are built, they are often a combination of all such blocks, like an app

typically has multiple Activities, many views, maybe a ContentProvider

and maybe even a service, all depending on the use case

Notifications notifications are one of the hallmark features of Android. they notify

users about events and provide visual, audio or vibration feedback. in

the newer versions of Android, you can even empower users to provide

action on the notification.

For example, in this recording application, while playing back a

recording, you can enable users to pause the playback or skip to the

next track from within the notification itself.

Android manifestthe final basic building block of Android is the Android manifest file.

this file provides critical metadata about your application, like the

package name for your app, the various components like Activities,

services etc. in your app, the permissions that your app might seek

(example: your app needs to seek the 'internet' permission to be able

to access the internet), the libraries that your app uses, the version of

Android you support and so on.

it is a very important building block as the appearance and function-

ality of the whole application depends on the declarations you make in

the manifest file.

A simple applicationnow that we've gone through the basic building blocks of Android, we'll

see how we can harness the same building blocks to create a simple

music player program in Android.

the application is intended to be really simple and will provide

functionality to play, pause and stop a predefined music file. Again,

to keep things simple, the Ui is expected to look something like this:

Page 38: devworx March 2013

36 Developing for AnDroiD

to create a new program, go to File>new>Android Application Project

in that screen, enter the name

of the application say simple

Music Player

As you might have noticed, as you

enter the name of the application, the

project and package name for the app

start populating automatically.

the package name is typically in

the format of com.<Your Organisa-

tion name>.<Your application name>

there are some other variables

that are filled by default, like the

minimum required sdk, target sdk

etc. which let us specify the version

of Android we are targeting but let

us not look at them right now, just

click next.hit next on the next screen.

On the next screen, you can select

the launcher icon for your applica-

tion, you can play around with the

launcher icon for your application, for

now, just leave everything at its default value and hit next.

the next screen in the wizard highlights what kind of 'Activity' is

created in your application by default. since we are making a simple

application, just hit next. the final screen is about the name of the

Activity created by default and the name of the file that specifies the

layout (remember the separate xml file we discussed before), for now,

lets again leave everything to their default values and hit Finish.

Once you hit finish, you are taken to the Ui editor which previews

how your application looks by default. As you can see, the default

application has a view which shows 'hello World' in the centre of the

app, this is the Ui visual editor in Android.

now let's get started with the User interface design of the applica-

tion, to begin, click on the 'hello world' textview at the centre of the

screen and hit delete.

next, as we need three buttons in the application, let us drag and

drop three buttons from the "Form Widgets" section of the Palette on the

Example of a content provider app

Page 39: devworx March 2013

37Developing for AnDroiD

left to the top left of the Android

screen like so:

drag all the three buttons in a

straight line so that it appears as

seen the image below:

next click on 'activity_main.

xml' at the bottom to view the

underlying xml code that gener-

ates this layout. it is very impor-

tant to know how these layouts

are generated as understand them

will help you design even more

complex layouts.

here you can see that there

is a 'relativeLayout' element at

the very start which contains 3

'Button' elements inside it. We

shall cover the relativeLayout

viewGroup in a later chapter, but

here it is important to understand

that the relativeLayout viewGroup specifies how the child elements

(each button) is arranged on the screen.

note that each child/button has a

number of different attributes, the first

of which is an id, followed by others.

Let us quickly describe some of them.

the android:id attribute identifies

the element in the xml file and can be

used to reference the same from within

the code, for example, the id for the

first button is '@+id/button1' which

translates to the following: the first

button should create a new id with an

id of 'button1'. so in code, whenever

we need to reference this button, say

we want to know when it was clicked,

we will need to use the id of 'button1'.

the android:layout_width and

A sample music player app

Buttons in the app

Page 40: devworx March 2013

38 Developing for AnDroiD

android:layout_height attributes highlight the width and height of

the element respectively. here we say that the width and height of the

button should be 'wrapped' to the width and height of the content in the

element, in other words, in case the text in the button should increase,

the length of the button would increase automatically.

the android:text attribute highlights the label of the button, let us

To begin your Android project, go to File > New

Enter the name of your application

Page 41: devworx March 2013

39Developing for AnDroiD

change that to Play, Pause and stop for each of the buttons and hit save

(Control + s/Command + s).

hit the Graphical Layout tab and see how the labels of the buttons

have changed to reflect the changes in the android:text attribute.

In the next step, you'll have to select the type of activity

After you enter your application name, you need to configure its icons

Page 42: devworx March 2013

40 Developing for AnDroiD

Folder structure now that we've started working on the look of the application, the next

step is to get familiar with the rest of the application. notice the package

explorer section to the left of the eclipse window, that window displays

The last wizard screen

Once you're done, you'll see the layout of your app as it stands

Page 43: devworx March 2013

41Developing for AnDroiD

a list of the folders that make up your application. Let us go through

those folders one by one.

the first folder on the left is called 'src', as the name suggest, it

contains the source code of your application. Program code such as

Three buttons in a line (outline 'activity_main.xml' in a circle)

Drag and drop buttons as you need

Page 44: devworx March 2013

42 Developing for AnDroiD

MainActivity.java resides in this folder. As you can see, individual java

source files are arranged under their respective package names. double

clicking on a file (like MainActivity.java) will open it in the central edit

area of eclipse, it is there that you will be spending most of your time.

Lets take a quick detour here and check out a folder called 'res'. this

Set the text within activity_main

Acitivity_main.xml

Page 45: devworx March 2013

43Developing for AnDroiD

folder represents the list of resources used

in your application. resources can be of

many types, images/vectors or 'drawables'

as they are called in Android are one type,

layouts like the one that was created by

default is of another type, Android menu's

(the things that pop up when you press

the menu/settings button your phone) are

of another type. even layout styles and

external strings are considered resources

in Android and can be stored under their

respective folder in this directory.

note how there are multiple folders

which begin with 'drawable' here called

drawable-hdpi, drawable-ldpi, drawable-

mdpi and drawable-xhdpi. dPi in hdPi

stands for dots per inch and the h, M, L

and Xh stand for high, Medium, Low and extra high respectively and

they represent the screen densities of devices that users might have. A

galaxy nexus which has more than 300 dots per inch falls under the

xhdpi category whereas the nexus s which has a lower pixel density falls

under the hdpi category. the idea here is to store different resolutions

of the same image in different folders so that Android can automati-

cally select which is the best image for the users phone at run time. so

for example, you might store a very high resolution image in the xhdpi

folder and store a lower resolution version of the same in hdpi and then

a lower one in mdpi and so on.

note that in addition to pixel density you can attach many other such

qualifiers to the folder name to specify how Android chooses resources

at run time. For example you can choose a specific resource if the phone

is in portrait mode and another when you are in landscape mode. More

of that is available in this link: http://developer.android.com/guide/practices/

screens_support.html

Moving on, the next folder is called 'gen', this folder contains files that

are automatically generated by Android and should never be tampered

with. the file r.java is of particular importance, basically whenever

you add a resource like a new layout, a view etc. in an app, Android

automatically catalogues it for you in this file. this is done so that

The folder structure

Page 46: devworx March 2013

44 Developing for AnDroiD

you can easily refer to it in your program

without any manual intervention. see how

different drawables, layouts, menus, strings

and all other resources are given an identi-

fier here under all the different classes. Our

activity_main.xml file here is represented by

the int activity_main in the class layout, which

translates to r.layout.acitivity_main

so say you create a new layout called new-

layout.xml in your application, thanks to this

automatic catalogue, you would then be able

to refer to it as r.layout.newlayout

next we see some of the libraries that

are used in the application. We can see the

Android 4.2 and some other Android depend-

encies that is used to compile the app. the

next folder is the assets folder where you can store raw assets.

next we have the bin folder which is the output directory of the

build of the Android app. here you would find the final apk file for your

application after a build is complete. Finally we have the libs folder,

where you can store private or 3rd party libraries.

Page 47: devworx March 2013

45Developing for AnDroiD

there are also other files like AndroidManifest.xml which we have

discussed before.

now that we have understood the basic directory structure of an

Android apps, let us get back to our app. remember that we have

designed the basic look of our application, the next step is to go into

code and find out when a button has been clicked. to do that, first click

on the MainActivity.java file under your respective package name.

this file represents the 'main' or first activity of your application

and this is the activity that is launched when your app is launched. Also

note that there are some pre-created methods for you. the first one is

onCreate and that is what we're really interested in.

As discussed before, an Android activity has its own lifecycle, it

is created, started, paused and so on. Along the same lines, when the

activity is first created, the onCreate method is called and this is where

you should do a lot of Activity initialisation jobs. notice how there

is a statement called setContentview(r.layout.activity_main) in this

method, this tells Android that when the activity is created, set the view

of the application to what is represented by r.layout.activity_main.

in other words, whenever the application is started, the layout inside

activity_main.xml will be shown on the screen.

Our first task here is to find out when a button is clicked, more

specifically, when the play, pause and stop buttons are clicked. so we

need to get a reference to the buttons and then set listeners (remember

Java listeners) on them.

to create a reference to the play button we need to write the fol-

lowing statement • Button playButton = (Button) findViewById(R.id.button1);

this statement basically tries to get a reference to the button by using

the id of the button which is 'r.id.button1' on the method findviewByid.

the method findviewByid returns a generic view object which we must

typecast to 'Button' to get a proper button reference.

note how you might get an error in the form of a red underline under

'Button'. if you hover over button you will find that it states "Button

cannot be resolved to a type" which basically means that it needs to

import a package in order to understand what 'Button' is. if you hover

over button, you will also find that Android has offered you with around

7 quick fixes, the first of which is to "import Button (android.widget).

Page 48: devworx March 2013

46 Developing for AnDroiD

When you click that Android automatically imports the necessary

package so that you don't have to manually do it. do that and you will

see that the errors have disappeared, there are still a couple of warnings

but lets ignore them for now.

Write a similar statement to get references to the other buttons like so:

• Button pauseButton = (Button) findViewById(R.id.button2);• Button stopButton = (Button) findViewById(R.id.button3);

Once you do that, the next step is to attach 'onClickListeners' to these

buttons so that they you are informed as soon as the buttons are clicked.

to do that take the first button and then type the following

• playButton.setOnClickListener()

inside the brackets we need to create an anonymous inner class for

the listener, to do that type new and then press control + enter to let

autocomplete take over. select the first option which is view.OnClick-

Listener and hit enter.

the code should look something like this:

• playButton.setOnClickListener(new OnClickListener()

• @Override• public void onClick(View v) • // TODO Auto-generated method stub• • • );

remember to put a semi colon at the end and hover over the OnClick-

Listener in new OnClickListener() to import the necessary package.

notice how all the errors have magically disappeared. the idea here is

that as soon as the play button is clicked, the method onClick inside

that new OnClickListener is called. Over there we can do our processing.

For now we'll show a short message called 'toast' here.

Page 49: devworx March 2013

47Developing for AnDroiD

• Toast.makeText(MainActivity.this, "Starting to play", Toast.LENGTH_SHORT).show();

this line will enable us to show a short message: "starting to play"

on the screen. now let us try this on the emulator, run this program by

right clicking on the program name in the package explorer, then select

run As and then select 1. Android application. see how a short message

is shown on the screen as you press play.

now let us attach similar listeners for the other buttons, like so

• pauseButton.setOnClickListener(new OnClick-Listener() • • @Override• public void onClick(View v) • // TODO Auto-generated method stub• Toast.makeText(MainActivity.this, "Pausing", Toast.LENGTH_SHORT).show();• • );• • stopButton.setOnClickListener(new OnClickLis-tener() • • @Override• public void onClick(View v) • // TODO Auto-generated method stub• Toast.makeText(MainActivity.this, "Stopping", Toast.LENGTH_SHORT).show();• • );

now every time you press the Pause or stop buttons, you get a similar

message. now that we know when the buttons are clicked, we need to

understand what we might do when we click them.

For this we need to copy the music that we want to play to a new

folder under resources. Lets begin by creating that folder, right click

Page 50: devworx March 2013

48 Developing for AnDroiD

on 'res' in the Package explorer and then select Create new > Folder.

Over there type 'raw' as the folder name. see how the folder pops up

under the res folder.

next, select the music file you want and rename it to something

simple, like a.mp3 (note, you might face some difficulty with complex

names and upper case characters, so rename it to something really simple

like a.mp3) and then drag and drop the file to the raw folder in eclipse.

You will be asked to Copy the file and then hit Okay. notice how the

file now appears in the raw folder.

next to play that object we need to create a MediaPlayer object, so

to do that declare the following as a Class variable (just under public

class MainActivity extends Activity )

• MediaPlayer mMediaPlayer;

then initialise it in in onCreate, preferable just before you get a

reference to those buttons but after setContentview.

• mMediaPlayer = MediaPlayer.create(this, R.raw.a);

here we are creating a new MediaPlayer object and using the a.mp3

Page 51: devworx March 2013

49Developing for AnDroiD

file we copied in the raw folder to initialise it.

now that we have initialised the object, we need to start playing it

when we click the playButton. to do that we need to inside the onClick

method of the OnClickListener for the play button and write the fol-

lowing:

• mMediaPlayer.start();

this tells Android to start playing the music.

similarly to pause and stop the music add the following inside the

onClick method of the listeners for the pause and stop buttons respec-

tively like so:

• inside the pause button listener: • mMediaPlayer.pause();•

• inside the stop button listener• mMediaPlayer.stop();

there you are all set, save the program and run it. hit play and there,

you can start listening to the music file. in case you are not able to hear

anything, make sure to un-mute your speakers :d

notice how pause and stop work great as well, also note how you

Page 52: devworx March 2013

50 Developing for AnDroiD

can play after pausing a file right out of the box.

so here we looked at how we used the basic Android concepts to

create a simple program where we were able to play pause and stop

playing music in almost no time.

Developing for android

Interested to learn more about Android x86 Development Platform then visit

below links:

register at Intel android Developer Zone to get latest information

about android on x86:

http://software.intel.com/en-us/android∙

Coding android Game app to support Intel x86? here are some examples:

http://software.intel.com/en-us/articles/coding-android-game-app-to-sup-

port-intel-x86-here-are-some-samples∙

Creating and porting NDK based android apps on Intel architecture:

http://software.intel.com/en-us/articles/intel-for-android-developers-learn-

ing-series-7-creating-and-porting-ndk-based-android∙

Do

wn

loa

d

Page 53: devworx March 2013

Chapter 4

GUI Development

The user inTerface (ui) on the android platform has changed

drastically over the past few years. it went from being a conservatively

designed platform to something that was really modern and beautiful.

Given the heightened focus on ui design and the look and feel of the

app, it's critical to get ui design right. Keeping that in mind, let's look at

the fundamental building block of user interfaces in android.

Page 54: devworx March 2013

52 GUI Development

Viewsas discussed Views are the foundational user interface elements in

android. in the previous chapter, we had created a particular ViewButton

and here we shall take a look at more such views.

TextView is one of the most basic View elements in android and

represents a display of text on the screen.

editText is a similar control but one where you can edit or enter text.

a Button, as the name suggests, is a control which allows users to

press or click it. similarly, a checkBox is a two-state button that can

either be in a checked or unchecked state. a radioButton is similar to

a checkbox; but unlike a checkBox, once a radioButton is checked, it

cannot be unchecked.

a spinner is the equivalent of a DropDown menu in forms, it can

have many options and the user can select one amongst them.

now there are two basic ways of creating/displaying a view, one in

XML and the other one in code. Let's look at how we can do both.

The first way of defining a View is in XML, this way this conforms to the Model View controller architecture and separates the View

definition and declaration in a separate area.

in this case, to define a TextView we need to create a TextView ele-

ment as follows:

• <TextView • android:id="@+id/mytextbox"

Page 55: devworx March 2013

53GUI Development

• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ content"• android:text="This is a text box" />

here the TextView

elements has many

attributes, the android:id

attribute assigns a unique

id to this TextView,

the layout_width and

layout_height attributes

specify the width and

height of the element, in

this case, "wrap_content"

means that the TextView

will adjust its width and

height based on its con-

tent. finally the text con-

tent of the TextView is specified by the android:text attribute.

in a similar way, one can create similar elements for editTexts, But-

tons and other android Views. The second way of declaring/defining

a View is through code, as follows:• TextView tv = new TextView(this)

although this is not the primary way of defining a ui in android,

at times it is very handy as you might want to add, update or remove

views dynamically.

EventsJust like Java, in android user interactions like a click, long click etc. are

classified as events and we can intercept events generated by android

by using eventListeners. for example, one of the most commonly used

eventListener is the onclick() event listener and this is called whenever

a View (like a Button, TextView etc.) is clicked.

similarly the onLongclick() event listener is called whenever a user

touches and holds a View.

as seen in the previous chapter, we can create an OnclickListener

and attach it to a button to receive callbacks on these events. for example• Button playButton = (Button) findViewById(R.id.button1);

Page 56: devworx March 2013

54 GUI Development

• playButton.setOnClickListener(new OnClickListener() • • @Override• public void onClick(View v) • // TODO Auto-generated method stub• Toast.makeText(MainActivity.this, "Starting to play", Toast.LENGTH _ SHORT).show();• mMediaPlayer.start();•

• );

Layoutsnext, lets have a look at Layouts. Layouts define the overall structure

of the user interface and define how individual Views like TextView,

editText and the like are placed in the overall user interface of the

application. so layouts are primarily a group of Views and are formed

from the 'ViewGroup' class.

now there are a lot of different subclasses of ViewGroup and each

of them provides a very different way of displaying the Views in them.

While on the subject of Views, let's consider them. What follows is a

list of the most popular Views.

Add text to your app

Page 57: devworx March 2013

55GUI Development

LinearLayouta LinearLayout is a layout where the children are arranged in a single

horizontal or vertical row. so for example, if you want TextView, an

editText, an imageView and a Button is a straight t vertical line, we

can use a LinearLayout as follows:

• <LinearLayout• xmlns:android="http://schemas.android.com/apk/res/android"• android:layout _ width="fill _ parent"• android:layout _ height="fill _ parent" • android:orientation="vertical" >•

• <TextView• android:layout _ width="fill _ parent"• android:layout _ height="wrap _ content"• android:text="This is sample text" />•

• <EditText• android:layout _ width="fill _ parent"• android:layout _ height="wrap _ content" />•

• <ImageView• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ content"• android:src = "@drawable/sampleimage" />•

• <Button• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ content" • android:text = "Submit"/>•

• </LinearLayout>

here we have defined a Vertical LinearLayout where each of the

children either has the width of its parent or has a width that depends

on its content.

RelativeLayoutnext, lets have a look at a relativeLayout. here the location of chil-

dren are defined relative to each other or relative to their parent. Take

Page 58: devworx March 2013

56 GUI Development

this for example:

• <RelativeLayout• xmlns:android="http://schemas.android.com/apk/res/android"• android:layout _ width="match _ parent"• android:layout _ height="match _ parent" >•

• <TextView• android:layout _ width="fill _ parent"• android:layout _ height="wrap _ content"• android:layout _ alignParentTop="true"• android:text="This is sample text" />•

• <Button• android:id="@+id/button"• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ content"• android:layout _ alignParentBottom="true"• android:text="Submit" />•

• <ImageView• android:id="@+id/image"• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ content"• android:layout _ above="@id/button"• android:layout _ alignParentLeft="true"• android:src="@drawable/sampleimage" />•

• <EditText• android:layout _ width="fill _ parent"• android:layout _ height="wrap _ content"• android:layout _ above="@+id/button"• android:layout _ toRightOf="@id/image" />• </RelativeLayout>

here we have a TextView at the top of the page and a button at the

bottom of the page. The imageView is put at the top of the button (at

the bottom of the page) and aligned to the left of the page. The editText

is put above the Button but to the right of the imageView.

Page 59: devworx March 2013

57GUI Development

such complex ui structures can be created really easily in a rela-

tive layout and this is without doubt one of the most powerful layouts

out there. The code above will generate an app that resembles the fol-

lowing image:

FrameLayoutnext lets look at frameLayout, a frameLayout is a layout where all the

child views are anchored to the top left of the parent by default and

where you would want the Views to overlap each other. You can however

change the position of the children within the parent by controlling the

android:layout_gravity attribute. a good use case of a frameLayout is

to write a label on an image.

• <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ content" >•

• <ImageView• android:id="@+id/image"• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ con-tent"

Add text to your app

Page 60: devworx March 2013

58 GUI Development

• android:src="@drawable/sampleimage" />•

• <TextView• android:layout _ width="wrap _ content"• android:layout _ height="wrap _ con-tent"• android:layout _gravity="center|bottom"• android:text="Label text" />• </FrameLayout>

here we have an imageView anchored to the frameLayout and then

we have a TextView which is used to label the image at the bottom,

center position of the image.

ListView next, lets look at some complex ui elements, the first of which is the

ListView. a ListView is a ViewGroup that contains a list of scrollable

Views that are created dynamically by taking data from a data source

Suggested pattern for List View. Source: developer.android.com

Page 61: devworx March 2013

59GUI Development

like a database, array etc.

Let's take an example of a Twitter application, here you might have

stored all the tweets, user data, location of the tweet in a database and

to show the tweet, you would need to display this data dynamically

from the database. as you keep on adding data in the database, you

want them to be automatically visible to user without having to add

each row manually.

That is where you can use a ListView and another structure called

an adapter. an adapter is an object that takes a row of data from the

database/array etc and creates a 'View' for that row in the ListView.

so the adapter is responsible for gathering the necessary information

about a tweet from the database and then structuring in such a way

that it looks like a tweet.

as you go down a ListView, the adapter automatically gathers

more data in the database and displays them in the ListView while

reusing Views at the top that became invisible when the ListView was

scrolled down.

GridViewa GridView is very similar to a ListView but the main difference is that

the list is just not a single vertical list but a two dimensional list of items.

for example, to create an image gallery you can have a two dimensional

list of elements backed by a GridView. Just like a ListView, a GridView is

backed by an adapter which fetches the data from a database or array etc.

MenuMenus are common ui elements in android, they can refer to the

menu you see when you press the Options button on your android

phone or when you click on menu options in the action Bar at the top

of applications.

There are also can be contextual or popup menus which appear when

you long press on an item.

Dialogsa dialog is a window that allows the user to enter some information or

to make a decision, typically dialogs prompt the user to make a deci-

sions but at times a Dialog can also be shown to highlight that the app

is processing some information or waiting for more data etc. o create a

Page 62: devworx March 2013

60 GUI Development

simple dialog we must use the alertDialog aPi

• AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());•

• builder.setMessage("This is what you must be aware of")• .setTitle("My dialog");•

• AlertDialog dialog = builder.create();• then to show the dialog, you must use the show method on the dialog•

• dialog.show();

remember you can also create complex dialogs with buttons and

listeners for those buttons as well.• dialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() • @Override

Suggested pattern for grid view Source: developer.android.com

Page 63: devworx March 2013

61GUI Development

• public void onClick(DialogInterface dialog, int id) • // do something when the user clicks okay• • )• .setNegativeButton("Cancel", new DialogInterface.OnClickListener() • @Override• public void onClick(DialogInterface dialog, int id) • // do something when the user says cancel• • );

inside the button's respective OnclickListener, we can specify the

action that needs to be performed when the Okay or cancel is pressed.

Notificationsnotifications is one of the hallmark features of the android platform.

it is basically a message that is dis-

played outside the user's normal

user interface. Developers can first

show a notification icon in the noti-

fication tray at the top of an android

device, once the user opens the noti-

fication drawer, he/she has access to

the complete notification.

starting with android 4.1, noti-

fications are now expandable in

nature and one can expand the ini-

tial notification to show more data

and even provide actionable buttons

built right into the notification.

a notification is typi-

cally created by using the

notificationcompat aPi

• NotificationCompat.Builder

Additional menu

Page 64: devworx March 2013

62 GUI Development

builder =• new NotificationCompat.Builder(this)• .setSmallIcon(R.drawable.musicicon)• .setContentTitle("Song playing")• .setContentText("Its my life");•

• PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);•

• builder.setContentIntent(pendingIntent);•

• NotificationManager ntificationManager =• (NotificationManager) getSystemService(Context.NOTIFICATION _ SERVICE);•

• notificationManager.notify(1234, builder.build());

here we create the notification using the notificationcompat.Builder

object and then give it an icon, title and text so that the notification

itself is well defined.

next we give the notification a Pendingintent, by giving a Pendin-

gintent to the notification, you are basically giving the notification the

right to perform an action on the behalf of the application. in our case,

Example of a detailed notification. Source: developer.android.com

Page 65: devworx March 2013

63GUI Development

we are giving the notification the right to launch our activity Myactivity

when we click on it.

ultimately we must use build on the notificationcompat.Builder to

create the notification and then use the notificationManager object to

actually display the notification.

so now we’ve gotten a good understanding of all the basic View and

ViewGroups in android, with this knowledge we can build anything

from the very basic to the very complex of applications.

Additional notification

Notification on file information during playback

Page 66: devworx March 2013

Chapter 5

Android NDKMost basic android apps run well on the dalvik VM and are

portable enough to run on any type of hardware even if the hardware

architecture changes for example from arM to x86. but in cases when

heavy computations are to be done like computationally intensive games,

which require physics simulation to be done, it would take ndK to solve

the problem and uncap the kind of performance that an app cannot be

achieved under the dalvik VM. it is however not recommended simply

for the purposes of developer's preferences or because the developer

only knows a specific language. actually, before even attempting to use

the ndK it is suggested that a developer be sufficiently comfortable at

developing in android and proficient at both Java and c languages.

Page 67: devworx March 2013

65ANDroiD NDK

to have more clarity on what ndK really is, it should be emphasized

that it allows mixing some parts of native code in c and c++ with an

android app developed in Java. it is based on Jni i.e. Java native interface

and provides headers and native libraries to interact with the user and lets

you perform computations at a much faster speed natively on the hardware.

this however does not change the way in which the final application is

packaged and the way in which the overall application is managed in its

lifecycle. it is also wrong to assume that all type of applications will benefit

if the code runs natively with the help of the ndK, in fact a major drawback

for many will be that it will increase code complexity exponentially and

the overhead incurred by the exchanges between native code and other

parts of the application will also be significant enough that it cannot be

overlooked during its execution.

android first started supporting ndK since android 1.5 cupcake

and features like native activities and x86 support started coming into

picture only since android 2.3 Gingerbread. due to this, there have been

two ways to write an android app since android 2.3, one is to use Jni,

which offers you flexibility and availability of the android framework,

while writing code using native activities offers the opportunity to handle

application lifecycle callbacks from the native code itself. the result is that

these lifecycle events like onPause(), oncreate() are handled within the

native code itself but with less featues like services and content providers

not being available, which need to be accessed through Jni only. the

most recent release of android ndK supports the following instruction

set architectures:

• ARMv5TE, including Thumb-1 instructions

• ARMv7-A, including Thumb-2 and VFPv3-D16 instructions, with

optional support for NEON/VFPv3-D32 instructions

• x86 instructions

• MiPs instructions

With the help of ndK, you can take advantage of the siMd extensions

to speed up the execution of your application. these are mainly used for

Multimedia applications and can provide performance gains of several

orders of magnitude greater than before. the arM instruction set exten-

sion that supports this is known as NEON, while Intel CPUs support SSE3

(Supplemental Streaming SIMD Extensions) and older versions like SSE2,

SSE and MMX. There are a couple of API incompatibilities that may occur

when targeting applications that utilize both of these.

Page 68: devworx March 2013

66 ANDroiD NDK

the android ndK also includes a set of cross compilers and native aPi

libraries which provide headers to be used in development of the native

application. the android official documentation lists out the libraries

which are stable and safe to use, while most others are experimental and

are likely to change in future versions of android ndK. these stable

libraries are: -

• libc (C library) headers

• libm (math library) headers

• JNI interface headers

• libz (Zlib compression) headers

• liblog (Android logging) header

• OpenGL ES 1.1 and OpenGL ES 2.0 (3D graphics libraries) headers• libjnigraphics (Pixel buffer access) header (for Android 2.2 and above).

• A Minimal set of headers for C++ support

• OpenSL ES native audio libraries

• Android native application aPis

Minimum System Requirementsthe android ndK has a set of prerequisites which need to be fulfilled

before we start using and installing it on your development machine.

these include:

• The Android SDK version 1.5 or later and version 2.3 or later if Native:

activities are to be used. this of course includes dependencies like

Java JdK etc.

• Operating Systems like Windows XP (32-bit) or Windows Vista (32-

bit or 64-bit) / Mac OS X 10.4.8 or later / Linux 32-bit or 64-bit with

Glibc 2.7 or later

• GNU Make 3.81 or later

• Awk

• Cygwin 1.7 or later, if Windows is being usedthe minimum aPi version required for development of arM is

android 1.5 or higher, while for other platforms like x86 and MiPs, at

least Android 2.3 is required. Other restrictions related to OpenGL ES

also apply, wherein OpenGL ES 2.0 is only supported since Android

2.0 or later while OpenGL ES 1.1 is supported since Android 1.6 or later.

Installationthe android ndK is available from the android developer website,

Page 69: devworx March 2013

67ANDroiD NDK

which can be downloaded specific to your development environment and

operating system. intel based x86 processors are supported only on ndK

release 6b or later, while 8d is the most recent one at this time,which was

last released in December 2012. The installation involves only the extrac-

tion of the compressed package which was just downloaded. this package,

when extracted gives you all the relevant files related to the ndK. it also

includes documentation, which is recommended to be read before you go

about trying to develop an ndK application.

Bundled Sample Applicationsthe ndK packages with itself a couple of sample applications which can

be examined for a reference on how the ndK works. the provide a very

good demonstration of the manner in which native code can be integrated

in your application. some of these applications are explained in the official

docs as follows:

• hello-jni — a simple application that loads a string from a native

method implemented in a shared library and then displays it in the

application UI.

• Two-libs — a simple application that loads a shared library dynami-

cally and calls a native method provided by the library. in this case, the

method is implemented in a static library imported by the shared library.

• San-angeles — a simple application that renders 3D graphics through

the native OpenGL ES APIs, while managing activity lifecycle with a

GLSurfaceView object.

• Hello-gl2 — a simple application that renders a triangle using OpenGL

ES 2.0 vertex and fragment shaders.

• Hello-neon — a simple application that shows how to use the cpufea-

tures library to check CPU capabilities at runtime, then use NEON

intrinsics if supported by the CPU. Specifically, the application imple-

ments two versions of a tiny benchmark for a FIR filter loop, a C version

and a NEON-optimized version for devices that support it.

• Bitmap-plasma — a simple application that demonstrates how to access

the pixel buffers of android bitmap objects from native code, and uses

this to generate an old-school "plasma" effect.

• Native-activity — a simple application that demonstrates how to use

the native-app-glue static library to create a native activity

• native-plasma — a version of bitmap-plasma implemented with a

native activity.

Page 70: devworx March 2013

68 ANDroiD NDK

Setting up the Application StructureThe application's native sources are placed in the <project>/jni/ folder. These

also include any shared libraries that are being used in the application.

to describe the native sources to the build system it is required that

you also place the android.mk file. additionally you can also add the

application.mk file if you want if you want to describe the project files in

greater detail.

The native code can be built with the help of the ndk-build command

which is available from the script in the ndk installation directory. With

the introduction to android x86 sdK, the ndK app development experi-

ence has become very smooth. Be sure to specify APP_BI:=x86 to ndk-build

command to experience it yourself.

other parts of the application like Java code can then be developed in a

similar way as it is done before. the only difference will be that when the

application is finally packaged, these native libraries will also be bundled

along with the application.

ndK applications can be debugged with the help of separate tool called

ndk-gdb which is available for this platform.

An overview of the NDKndK is a tool meant for advanced app developers who know how to deal

with the performance boost given by this powerful tool and use it in a

manner that excessive calls to and from Java does not create too much of

an overhead and that it also does not defeat the purpose of using the tool

itself. Using NDK also creates the problem of maintaining the code base

for each platform separately and ensure that it is compatible with each

respectively. this not only increases project complexity but also multiplies

the effort of the app developer in the sense that multiple code bases have

to be maintained in cases when the code simply does not work by simple

porting or recompiling of the source. Whats more, it can even narrow the

reach of your application or some functionality tends to be lost if processor

specific proprietary extensions are used for the native app development.

that being said, most applications should however face no problems

in supporting different platforms and might be able to work with a simple

recompilation of the source code after a little bit of porting is done. in a

simple ndK project, the android.mk is used to define modules which

include shared libraries or static libraries. static libraries can be used to

generate shared libraries, which are in turn packaged into the android

Page 71: devworx March 2013

69ANDroiD NDK

application if the developer chooses to do so. the application.mk on the

other hand is used to describe the modules which are actually required by

your application. both these files can be placed under the jni folder of the

Android project. The Android.mk for any project could look like this: -• LOCAL _ PATH := $(call my-dir)• include $(CLEAR _ VARS)• LOCAL _ MODULE := helloworld• LOCAL _ SRC _ FILES := helloworld.c com _ test _ android _HelloWorld.c• include $(BUILD _ SHARED _ LIBRARY)

if the native code for the app is to be built for specific platforms, you can

specify this in the application.mk file, where you can mention all the target

ABIs. Alternatively you can also invoke ndk-build to specify the same at

the time of building the source files. For example, to build the application

source for x86 devices, you can do ndk-build APP_ABI=x86. But it is usu-

ally recommended to compile the code for all application binary interfaces

unless you are specifically targeting a particular platform by using the “all”

parameter like ndk-build APP_ABI=”all”.

to use native code in a java file, it has to be declared first and then the

library can be loaded. this can be done as follows:• private static native String printhello();• static System.loadLibrary(“helloworld”);

the above code for example declares and loads a sample helloworld

library which just returns a string that can be used anywhrere in the code.

it can be used to display the message in a textview as follows:• TextView tv = new TextView(this);• tv.setText(printhello());• setContentView(tv);

to make sure that this native code can be called from Java, we need to

ensure that it complies with the Jni specifications. this can be done with

the help of the javah tool which helps generate headers based on the java

sources. it requires that the files be compiled before running this command,

which could look like: • javah -d jni -classpath bin/classes com.test.android.Hel-loWorld

now all we need to do is create the source file that imports the gener-

ated java header file and implements a method corresponding to the one in

the headerfile that forms the body of the method that we called from Java.

Page 72: devworx March 2013

70 ANDroiD NDK

A simple method that returns a “Hello World” string would look like the

following in the native c code:• #include “com _ test _ android _ HelloWorld.h”• JNIEXPORT jstring JNICALL Java _ com _ test _ android _ Hel-loWorld _ printhello• (JNIEnv * env, jobject jObj)• • return (*env)->NewStringUTF(env, “Hello World!”);•

now, as mentioned before the file can be compiled with the help of

the ndk-build command after which the application can be compiled and

packaged into an apk. the package can then be installed on an emulator

where it can be tested if it is running correctly. the example shown above

is fairly straightforward and definitely very easy as compared to what you

would do in a real application, but a Hello World program is definitely the

best way for getting an idea of how everything works and to check the basic

functionality of any platform. there are however a couple of things that you

need to ensure you take care of while porting your native application across

platforms like arM to intel. these include things like toolchain compat-

ibility (if the application is using any tools directly instead of android

build scripts), SIMD API incompatibilities between SSE and NEON and

also some memory alignment implications that may occur when data is

not explicitly aligned between the two architectures. the manner in which

floating point operations are handled differently by each platform might

also have an impact on the support for each of them.

in conclusion, while the native development Kit is a powerful tool, it is

also something that needs to be made use of correctly. While some applica-

tions surely might benefit with its presence, it is something which should

only be used really needed. this is because most applications are best left

running on the dalvik VM itself without encountering any unnecessary

complications with the hardware unless it hampers the development of a

specific type of functionality that you would like to provide.

Page 73: devworx March 2013

Chapter 6

Developing forAndroid x86

Due to its open source roots in the Linux kernel, Android has

always been a major attraction of hobbyist developers and 'modders'

who like experimenting with their devices. this has resulted in Android

being used and ported to other devices and platforms. Before intel

officially announced support for Android and its partnership with

Google, there were already quite a few community projects which were

in progress to make Android work on x86. Now that official support

is available, needless to say some of them have either lost relevance

or gave gained momentum as better support has become available.

Page 74: devworx March 2013

72 Developing for AnDroiD x86

one such effort is the Android-x86 project which best represents the

most popular and successful port of Android to x86 devices before it was

announced by intel officially. the aim of this project was to port Android

Code to x86 devices and it did enable a couple of devices such as the Asus

eee PCs, Viewsonic ViewPads, Dell inspiron Mini and Lenovo thinkPad

tablets to boot and run Android natively. Now that it is officially supported

by intel, there have been efforts to enable booting of Android even on

Desktops (even though it wouldn’t make much sense). initially this project

started with the aim of consolidating different patches for x86 that were

available in the open source community, but eventually development was

spearheaded to enable full fledged support for some of the above devices.

the latest build supports 3.0.36 linux kernel, wifi, bluetooth and ethernet,

ALsA audio, external monitor and netbook native resolution. the website

for this project is available at www.android-x86.org where more information

and details can be accessed. the project is still active and developers are

working on porting the JellyBean 4.2 code along with an effort to enable

openGL hardware acceleration for intel and radeon devices.

Moorestown & Medfield PlatformsMoorestown is intel's smartphone platform, which it introduced in 2010.

it's based on the Lincroft soC and an Atom processor core. it focussed on

better power management, with upto half the idle power consumption as

compared to earlier devices.

intel announced a partnership with Google in 2011, and with version

3.1 (Honeycomb), and sDK revision 12 official support for x86 devices was

added to Android. only in Jelly Bean 4.2 and Android sDK level 17 was

the emulator released with native x86 support, which allowed it to run at

almost native speeds on the desktop.

in early 2012, a major overhaul in intel's architecture was announced

with the Medfield platform, which was the fourth generation of intel's

mobile device technology after Moorestown. it featured a 32nm processor

chip which was later known as the intel Atom Z2460 which was accompa-

nied by a smartphone reference design to ease the amount of effort spent

by device manufacturers. it was the first ever platform that eventually got

popular with leading manufacturers taking it seriously with devices like

Lava XoLo X900, Lenovo K800, Zte Grand X iN, Motorola Razr i and

others being announced and released in the market.

the Lava XoLo X900 is one of the first devices which was released in

Page 75: devworx March 2013

73Developing for AnDroiD x86

india in April 2012 by the domestic indian brand of the same name. it fea-

tured an intel Atom Z2460 1.6GHz processor with 1GB RAM and a 400MHz

PowerVR sGX 540 GPu with 512KB L2 Cache along with a dual channel

LPDDR2 memory interface. the phone fared well in terms of Javascript

performance as it lead the pack in tests like BrowserMark and sunspider,

where it beat phones like the samsung Galaxy Nexus, iPhone etc., while

it fared decently while giving a tough competition to other phones in the

battery department.

Comparing x86 and ARM Android makes it very easy to develop apps and make them able to run

anywhere without any problems on almost any platform as long as they are

free from native code and are dalvik based. this makes app development

easy and worth the effort as apps become portable without any effort and

being compromised much on performance. But when the Android NDK

and JNi come into picture, the developer needs to make sure that they factor

in support for all platforms. sometimes the effort is only restricted to rec-

ompiling the application package, though it may take more in other cases

where a proprietary ARM specific feature was previously implemented.

still, according to intel's official documentation more than 75-80% of apps

rely on Android sDK to run apps on top of Dalvik VM, resulting in the

widely accepted fact that a majority of Android's applications are already

available of intel x86 based Android because they are platform agnostic.

though Android supports both x86 and ARM, it has generally been an

unwritten rule that x86 has traditionally been much more successful on

the desktop market because of its impressive performance, while ARM has

been leading in the land of smartphones and tablets with its much better

power management and optimisations for the mobile environment. this

stereotype is set to become a thing of the past now, since intel has been

ramping up its efforts towards innovation in the mobile segment. this is

likely to result in a number of developments related to better performing

intel Atom processors which is likely to benefit other ecosystems too like

Windows 8, embedded Linux and others, in addition to Android. the fact

that porting desktop apps to Windows and Android tablets will become

much easier is an attractive proposition for many developers and will surely

result in some innovative features related to desktop integration.

if a comparison between the two architectures has to be made, it makes

more sense start with a table that accurately describes the main differences

Page 76: devworx March 2013

74 Developing for AnDroiD x86

in the fewest words:

saltwell is the microarchitecture that evolved from Bonnel which could

execute two instructions per cycle. the basic difference lied in the way chips

were laid out. While Bonnel was 45nm, saltwell was fabricated to be 32nm in

size. it also contained a single chip called Penwell as compared to earlier ver-

sions which were a combination of Lincroft and Langwell. this single chip

design is the reason why it has made itself easily comparable to ARM than

it was possible ever before. As mentioned, saltwell uses in-order execution

as opposed to ARM which results in reduced instruction execution latency.

A major advantage that x86 has available to itself is the fact that a 64 bit

instruction set has always been available on this platform, while ARM is

still testing it in its labs which is due to reach retail in the coming two years.

the intel Hyper-threading technology further supplements better sharing

of resources with more than 50 percent performance improvement due

to clock cycle sharing between two threads. this technology enables the

software to view a single processor as being more than one logical processor.

intel Atom processors obviously support the usual set of hardware sen-

sors needed for a smartphone and tablet device to work satisfactorily. these

include position sensors like accelerometer, gyroscope, magnetometer, prox-

imity sensor, GPs and environment sensors like the Ambient light sensor.

these sensors can be accessed with the help of Android sensor framework.

Binary TranslationWhen intel first released Medfield CPus, a big issue for many users was

unavailability of NDK based apps. this resulted in a solution proposed

by the company in the form of Binary translation which allows native

binaries for Android to run on intel based devices without much loss in

performance. the technology allows execution of ARM apps without having

the developers to port them to the x86 architecture. the binary translator

integrates with the Dalvik VM and other ARM based JNi libraries to perform

on the fly binary translation operations in such a way that it is completely

transparent to the app and the underlying hardware. intel says that it has

resulted in more than 90 percent of Play store apps being able to run on

intel based phones.

OpenGL ES on Intel Atom processorsthe main graphics APi for 3D rendering on Android is openGL es, which

is an specialized version of openGL, which was originally designed of

Page 77: devworx March 2013

75Developing for AnDroiD x86

desktop graphics processing. it has been specifically designed keeping

mobile devices like smartphones and tablets in mind. ever since the NDK

was released in Android 1.5 Cupcake, openGL has been a vital part of the

Android ecosystem for game development. it was since Android 2.2 Froyo

that openGL es 2.0 was available for Android development. support for

openGL development on intel Atom devices has been available in Android

Gingerbread 2.3.3 and consistently ever since ice Cream sandwich 4.0.3.

one limitation for intel devices however is that the Android emulator does

not yet support development on openGL es 2.0 and is restricted only to

versions 1.0 and 1.1. But intel does provide an important tool to accompany

it in the form of the intel Graphics Performance Analyzer, which runs on

both Windows and ubuntu desktops and can aid developers in getting

important performance metrics related to graphics on Android smartphones

and tablets. Further down the line, just recently openGL es 3.0 got released,

though support for which looks a little further away in time.

in addition to the graphics performance analyzer discussed earlier,

intel provides the Hardware Accelerated execution manager and intel

Atom x86 system image for Android. the Hardware Accelerated execution

manager is a hypervisor that allows much faster Android emulation than

is normally possible, by running its driver on the host machine as well as

within the emulated Android system. the software is available for all the

three plaforms namely, Windows, Mac and Linux. it also efficient use of

GPu by routing 3D computations through the host machine's GPu device.

the only limitation is that it does not work for Android platforms where

the intel's x86 system image is not available.

Advantages / Disadvantages of using x86 images for development

Pros1. the main advantage of developing for x86 devices for development is

the fact that targeting a mobile device which has the same architecture

as the development machine has its own advantages. Whether they

might be in the form of better emulation or faster execution, it just

makes the development process much more easier.

2. the kind of tools, dedicated software and support that intel is likely

to provide for its platform to encourage developers to start developing

apps will be much better than its ARM counterparts, which is admit-

tedly more popular due to the wide range of devices available.

3) the x86 architecture is already more mature and has various stand-

Page 78: devworx March 2013

76 Developing for AnDroiD x86

ardized interfaces such as sAtA, PCie, usB, HDMi, Firewire working

with it for many years.

4. the fact that intel devices offer better performance than its ARM

counterpart cannot be denied. though power management is admit-

tedly a little lacking in mobile devices, intel's x86 platform has been

known for its expertise in the server and desktop space.

Cons1. Reaching the hands of a majority of consumers has to be the focus of the

company and it will automatically attract the development community.

2. While porting most native apps is a no-brainer, other app developers

need to make a significant effort to support both the platforms on the

present and future versions of the app.

3. even if developing the app itself might be easy, testing for bugs and

patching existing bugs might become difficult and take more time as

more than one hardware architectures are involved.

4. Binary translation is a good alternative but the performance for highly

computing intensive apps is not good and the criteria of what kind of

apps might/might not be supported is not very clear.

Creating a native app for Intel Architecturein this tutorial, we'll look at creating NDK based apps for intel Architecture

(iA), and also consider simple use cases for porting existing NDK-based apps

to iA based devices. We assume you have already installed the Android

development environment including the Android sDK, Android NDK and

configured emulator-x86 to test the apps. For further information, refer to

the Android Community section on intel website (http://software.intel.com/

en-us/android). to keep our development environment simple, we will use

Linux* command line tools for the most part.

Creating Default Android ProjectAndroid sDK has command line tools to generate default project structure

for a typical hello world app. We will first create a default project, and then

modify the java sources to add JNi calls and native code.

As we've seen above, we first created a directory called labs/lab2 and

used “android” command line tool to generate the default project. We speci-

fied android-15 as the APi level, and named our app as “CPuidApp” with

package com.example.cpuid.

Page 79: devworx March 2013

77Developing for AnDroiD x86

We then used the “ant” command line tool to build the project in debug

mode and install using “adb” (or reinstall if exists on emulator or target).

We assume you have already have an emulator or a device attached and it

is the only listed device in “adb devices” command output.

Below is a screenshot of the Android x86 emulator with iCs after com-

pleting the above process.

Clicking on the App, you can see the default hello world output of the

app. We will now modify the app to use native code.

Invoking Native code from java sourcesthe default android project generates the java sources for a typical hello

world project with give package namespace (eg: com.example.cpuid). to use

native c/c++ code in our java source file, we first need to declare JNi call and

load the native library, as highlighted yellow box in the screenshot below.

Page 80: devworx March 2013

78 Developing for AnDroiD x86

As seen in the declaration, the native call returns a java string which

we can use anywhere in our java source. As shown in above screenshot,

we modified the textView to display the string we get from the native call.

this is highlighted in red in the box.

this is a very simple case for declaring and using JNi native calls in

Android App Java sources. Next, we will use “javah” tool to generate the

JNi header stubs for native code, and add or modify native code to comply

with JNi native headers.

Using “javah” to generate JNI header for native codeWe now have to modify our native code to comply with JNi call specifica-

tion. “javah” too helps us automatically generate the appropriate JNi header

stubs based on the Java source files. the “javah” tool requires the compiled

Java class file for generating the headers. so we use the “ant” tool to quickly

generate java class files as shown in below screenshot (“ant debug”).

use “javah” to generate the jni header as shown in the screenshot (second

yellow highlight box). it will create a directory “jni” and the header stub

based on Java class. the screenshot below shows the generated JNi native

header stub.

Create corresponding C source file (“com_example_cpuid_CPuidApp.c”)

for the above generated header. Following below is the source listing :

We call the native code cupid_parse and return the parsed buffer as

JNi string. We are now ready to compile the native code using the x86

Page 81: devworx March 2013

79Developing for AnDroiD x86

NDK toolset.

Building native code with NDK for x86Please refer to the Android community section on intel website (/en-us/arti-

cles/ndk-for-ia) for more information on NDK for iA installation and usage.

Android NDK toolset uses a build system, which requires a custom

android specific make file “Android.mk” to be present in the “jni” folder

of the project to compile native code. Android.mk specifies all the native

C/C++ source files to be compiled, the headers and the type of build (eg:

shared_library).

Below is the native code Android make listing for our project (“jni/

Android.mk”)

this is a simple scenario with 2 C source files and specifies to build a

shared library. We can now issue “ndk-build APP_ABi=x86” to build our

native code, and generate the shared lib. Android build system also provides

Page 82: devworx March 2013

80 Developing for AnDroiD x86

another supplementary make file “Application.mk”, which we can use to

specify additional configuration options. For example, we can specify all the

supported ABis in the Application.mk file, and the ndk-build will generate

native shared libraries to target all architectures.

the screenshot above shows the successful compile of native code for

x86, and also shows shared library being generated and installed. We are

now ready to rebuild our Android App, and install/run on x86 emulator

or target device.

Rebuilding, Installing and Running the NDK App for IAWe can use “ant debug clean” to clear our old build files, and issue “ant

debug” again to start off full rebuild of android project. use “adb” to reinstall

the app on target device or x86 emulator as shown in the screenshot below.

the screenshot below shows the App icon inside the x86 emulator, and

Page 83: devworx March 2013

81Developing for AnDroiD x86

the result of running the app inside the x86 emulator.

We have successfully built NDK based android app start to finish. using

x86 NDK toolset to port existing NDK Apps to iA based devices.

Android Apps with native code typically have a standard project struc-

ture, with “jni” folder containing the native sources, and the corresponding

Android.mk/Application.mk build files. in the previous section, we saw a

simple example with native source code and corresponding Android.mk file.

Android NDK toolset lets us specify all the target ABis in Applica-

tion.mk at once, and automatically generates native shared libraries for

all targets. the android build system will automatically package all the

target native libraries inside the APK, and at install time the Android

package manager will install only the appropriate native library based on

the target architecture.

We can invoke “ndk-build” or specify in Application.mk

APP_ABi := all

oR

APP_ABi := armeabi armeabi-

v7a x86

Please refer http://developer.

android.com/sdk/ndk/index.html for

more information.

Page 84: devworx March 2013

Chapter 7

Debugging onAndroid

When you create an android app, it is important that

you deliver good quality apps that are rid of bugs and are safe to

run from the user’s perspective. eventually you might come across

that odd error or a situation when your app is not working in the

way that you expected it to be. often these situations are difficult to

sort out unless we have proper debugging tools available to us.

Page 85: devworx March 2013

83Debugging on AnDroiD

these tools allow you to step through each line of code and examine the

value of each variable at that point in time with their help. the debug mode

also allows you to set up breakpoints, pause execution, and offers other

powerful tools to point out the problems occurring in your code. Debugging

is not a big deal for people who already come from a related programming

background like Java, and it will be even better if you have done debugging

before in eclipse. In that case, there are only some android specific tools and

techniques that you need to be aware of.

the Dalvik VM in android supports JDWP or Java Debug Wire Protocol

to attach to the device over adb. adb or android debug bridge is a command

that is often used to communicate over the debug channel with a connected

phone or a virtual device such as an emulator or virtual machine. the adb

command works in the form of a client server model where an instance

runs on each – the client as well as the receiving server i.e. the device to

which communication is being established. the adb is part of the platform

tools package in the android sdk and should be available in the relevant

subdirectory, which needs to be added to your Path variable if you need

access to adb easily from anywhere.

Setting up ADB

To set up adb, we need to complete the following steps:1. Make the application debuggable by mentioning it in the android Man-

ifest. this is done by default for all apps in the eclipse IDe. If that is not

the case, you can do it manually by adding android:debuggable="true"

to the <application> element of the androidManifest.xml file.

2. enable debugging on the device to be connected. this can be done by

going to an option similar to Settings > applications > Development >

uSB Debugging on the device. actual menu options may vary a little

depending on the actual device and the manufacturer.

3. to make sure that the computer can detect the device, we need to

make sure that we either install a uSB driver for aDB if we are using

Windows or that we add it to the udev rules for uSB. Individual

oeM uSB drivers for Windows are available for each manufacturer

and are listed in the official documentation. to install the drivers on

Windows 7 for example :

• Connect the device to the computer

• Right click My Computer and click Manage.

Page 86: devworx March 2013

84 Debugging on AnDroiD

• Go to Devices > Other Device and right click on device and select Update

Driver Software.

• You should see an option to “Browse my computer for driver software”

which you need to click on and point it to the location of your driver

installation file.

For Ubuntu Linux

• Create the following file as root, by executing the following command:• sudo vi /etc/udev/rules.d/51-android.rules

• Add the following line for each vendor :• SUBSYSTEM=="usb", ATTRidVendor=="0bb4", MODE="0666", • GROUP="plugdev"

the information in the above line, like the vendor ID depends on your

device, a list for which can be found on the android developer website.

change permissions for the file you just created, as follows :• chmod a+r /etc/udev/rules.d/51-android.rules

to check if adb is able to connect properly, execute the adb devices

command which should give you an output of all the connected devices.

using the Dalvik Debug Monitor Server (DDMS)

the Dalvik Debug montior Service or DDMS as it is known is one of

the application debugging tools that must be a part of every android app

developer's toolkit. It is a very powerful debugging environment and offers

various unparalleled debugging features like allocation tracking, thread and

heap information, logcat monitoring, call / SMS / Location spoofing, File

explorer etc. It is available by default in the eclipse IDe environment as a

separate perspective and works on top of adb for device communication.

DDMS can be started from the eclipse IDe itself by changing the per-

spective to DDMS by going to Window > open Perspective > other > DDMS.

It can also be launched as a standalone application as part of the android

Device Monitor by typing the monitor command in the terminal. note

that earlier DDMS could be started independently with the help of ddms

command, which now appears to be deprecated. the new android Device

monitor application offers DDMS and a couple of other debugging tools like

the Hierarchy Viewer, Pixel Perfect and Tracer for OpenGL ES in a single

environment. the android Device Monitor is a standalone tool which can be

used in case you do not use the eclipse development environment, though

the integration with eclipse is something that other IDes could be envious of.

DDMS works by connecting a debugger to the debugging port of each

Page 87: devworx March 2013

85Debugging on AnDroiD

android VM, which is associated with each process in the system. When a

debuggable VM (or process) is started, the DDMS assigns the 8600 port to

the first VM and then keeps assigning similar port numbers for each addi-

tional VM. additionally 8700 is used as the base port by default, which is

used to accept traffic from any other debugging port and forward it to the

debugger on port 8700. this functionality has the advantage of debugging

all VMs on a single port. Which traffic gets forwarded is dependent on the

currently selected process in DDMS. In case IDes other than eclipse are

being used, it should be configured to attach to whatever port is assigned

to the VM or it can be configured to use 8700 as the base port.

DDMS View componentsDDMS offers a set of view components, each of which show relevant infor-

mation related to the connected physical device or emulator.

Devicesthe devices pane shows the

details of each process that

is running in the connected

device. there are various

operations that can be per-

formed from this view with

the help of the icons that we

can see along the right of

the tab. each of these icons

performs the following func-

tions:

• Debug Process Icon – It

shows the state of con-

nection of the debugger

– Red means disconnected and Green means connected.

• Update Heap – Clicking on this enables Heap information for the

process so that we do not have to do memory deallocation manually.

• Dump HPROF File – Generates a Heap dump, useful for tracing memory

leaks in your app.

• Cause GC – Invokes the Garbage Collector to collect heap data.

• Update Threads – Shows information about the running threads in

the thread tab for the selected process.

The Devices view showing list of processes

Page 88: devworx March 2013

86 Debugging on AnDroiD

• Start Method Profiling – Tracks the metrics related a method. It col-

lects information like the amount of time taken to execute a method,

number of calls made.

• Stop Process – Stops the currently selected process.

• Screen Capture – Takes a screenshot of whatever is displayed on

the screen.

• Reset adb – As the name suggests, resets adb to reconnect to the device.

can be used if you are facing problems.

Threadsthe threads tab on the right of devices view shows information related to

the threads for each process selected in the devices view. the information

shown can be described as follows: -

ID – a unique ID assigned to each thread by Dalvik VM, which are only

odd numbers that start from 3.

tid – the Linux thread ID associated with each thread, which matches

the process ID for each main thread in a process.

Status – Status of the thread in the VM, out of which daemons are marked

with an asterix. other values can be among running / sleeping / monitor

/ wait / native / vmwait / zombie / init / starting.

utime – time spent executing user code.

Stime – time spent executing system code.

name – Specifies the name of the thread.

Heapthe heap view shows information and statistics about the memory heap.

this is important from the memory usage tracking perspective for each

process and helps to track heap usage at a single point of time and gives

you an overview of the overall heap usage of the app. to view heap usage,

you can follow these steps:

1. From the devices view, select the process for which heap usage has

to be examined.

2. click on update heap.

3. From the heap Tab, select Cause GC to enable collecting heap data.

every time you need to update you heap statistics, you can then click

on Cause GC to update the statistics again.This will show object types with

the amount of memory allocated to each.

Page 89: devworx March 2013

87Debugging on AnDroiD

Allocation Tracker the allocation tracker shows real-time information about allocations hap-

pening in the application over a certain period of time. to enable allocation

tracking: -

1. Select the process to be tracked from the devices view.

2. click on Start tracking from the allocation tracker tab on the

right pane.

3. Perform any actions on the device or emulator that you want to

be tracked.

4. Click on Get Allocations to view the list of allocated objects since

tracking was enabled.

Network Statisticsthe network statistics tab allows you to track and optimize the amount

of network data that your app is using. It even allows you to tag network

sockets so that you can differentiate between different types of data. the

collected statistics are then displayed in the form of received and transmitted

byte figures and also as a graph for the same that indicates the speed and

amount of data received / transmitted.

The heap view showing info about memory heap

Page 90: devworx March 2013

88 Debugging on AnDroiD

Emulator Controlone problem with testing android devices using an emulator or developer

device is the fact that it is unable to examine the performance of the app in

real world conditions, like when a call or SMS is received / sent or when

a particular location is reached. to test these real world conditions, the

emulator enables you to spoof these data in order to test the functionality

of your application. to be specific, the emulator tab allows you to emulate

the following real world conditions in the emulator:-

Telephony Status : as per the official documentation, following options

are available to set the status of the phone’s network :

Voice - unregistered, home, roaming, searching, denied

Data - unregistered, home, roaming, searching, denied

Speed - Full, GSM, HSCSD, GPRS, EDGE, UMTS, HSDPA

Latency - GPRS, EDGE, UMTS

Telephony Actions: telephony actions allow you to spoof network

actions such as sending / receiving a call or an SMS.

Location settings: emulator control allows for location spoofing with

the help of location settings. these allow you to specify location manually

or with the help of a GPX / KML file.

The emulator control view lets you control telephony and location for the device

Page 91: devworx March 2013

89Debugging on AnDroiD

File Explorerthe file explorer view lets you view the directory and file structure if the

device and also easily copy files to and from the emulator so as to enable

easy data transfer in a manner similar to adb push or adb pull command.

the Push file and Pull file options are available as a wrapper in the File

explorer view on top of these very commands.

LogCat Viewer

android has a highly developed logging system in the form of Logcat

which outputs all the system messages, exceptions, application specific

information etc. Logcat for a device can be accessed with the help of the adb

logcat command or with the help of DDMS in the Logcat viewer. there is

so much information available through logcat that sometimes it becomes

important to filter out information according to a log tag, log message,

process ID, application name or log level. these options allow you to easily

customize the type of log output you get and hence troubleshoot the applica-

tion according to your needs.

LintLint is a static code analyzer that checks for possible optimizations, bugs

and structural integrity of the code. It is integrated with the eclipse IDe

by default and is also available for integration with other IDes like IntelliJ

Idea. It basically supplements the development by making suggestions to

improving unused resources, internationalizations etc. each error or sug-

gestion is accompanied with a severity level that signifies how serious the

problem or error is. Lint has been developed to be IDe independent and

can possibly be integrated with other IDes too, if not already available. In

most cases though, it can be run from the command-line. In case of eclipse, it runs automatically when an APK is exported, XML

file is updated or changes are made in the layout editor and displays the

output in the Lint Warnings view. Lint can be configured to check only a

specific set of files or ignore a particular file, if needed. Such lint preferences

are saved in the lint.xml file which is stored in the project’s root directory.

however, in case of eclipse this is not needed as the IDe automatically

handles these preferences which can be edited at the project level (from

the Lint Warnings view) or at global level (go to Window > Prefernces >

android > Lint error checking).

Page 92: devworx March 2013

90 Debugging on AnDroiD

Hierarchy Viewerthe hierarchy viewer is another tool available from within eclipse or as an

independent utility with a motive to optimize, inspect profile and debug the

layout of an android app. Before trying to run hierarchy viewer, you need

to make sure that the application is installed and running in the emulator

and that the debugger is not connected or trying to debug the app. after

running hierarchy viewer, select the activity and click on Load View hier-

archy. as expected, it will show you a hierarchy of the view objects with

different panes like properties, tree overview, tree view and layout view.

each of these depicts a specific set of information: -

Tree Overview: the tree overview shows the entire tree hierarchy and

can be used to navigate the tree view in detail on the right. It shows the

entire set of tree elements in a sort of zoomed out manner which can be

accessed individually from the tree view by moving the shaded rectangle

across the panel.

Tree View: the tree view depicts detailed information about individual

views and their relationships. Information about a view’s class or ID, screen-

shots or layered Photoshop file can be extracted from here.

Properties View: the Properties pane shows a list of properties related

to each view object that is selected.

Layout View: the layout view shows a wireframe block representation

of the layout which can also be used to navigate the tree view, alternatively

`The file explorer view lets you access files from the device

Page 93: devworx March 2013

91Debugging on AnDroiD

a specific block in this view gets highlighted when an element is selected

in the tree view.

Pixel PerfectPixel Perfect is another relatively new tool used for optimizing layout of an

application. It can be accessed by clicking on Inspect Screenshot after the

list of activities is displayed in the hierarchy Viewer. It is used to examine

individual pixels in the form of a magnified image of the screen layout. When

Pixel Perfect is loaded, you will be able to see three panes in the screen:

View Object pane: the view object pane lists all the view objects visible

on the screen, whether they are part of the android system or the app itself.

Pixel Perfect Loupe Pane: this pane shows the detailed information

about each pixel corresponding to the magnified image of the screen. the

information available is the detailed colour codes (htML colour codes,

RGB values), X/Y coordinates etc.

Pixel Perfect Pane: this pane shows the overall image of the screen as

it would be seen normally. this screen is also used to position the crosshair

in the Pixel Perfect Loupe pane.

Pixel Perfect also allows creation of uI elements based on bitmap overlays

done in the form of an image.

Traceviewtraceview is a nice little utility to speed up performance of your applica-

tion. It is a profiler that examines the execution logs and displays the data

in a graphical form. to enable traceview, you need to have enabled tracing

for your app and generated trace trace logs beforehand with the help of

the Debug class. this is generally a more preferred method as opposed

to another option where we generate trace logs with the help of the Start

method profiling button in DDMS.

once we have trace logs available to us, these can be loaded into trace-

view which displays a timeline view in the form of bars that signify the

execution time of each thread. It also shows a profile view which shows

the most important data related to the execution time of each method in

the application along with a few other statistics.

there are a couple of other utilities that are available for debugging

your application in a very detailed and in-depth manner. these include

utilities like systrace, dmtracedump, MonkeyRunner, Tracer for OpenGL

etc., which help in optimizing every aspect of the app. there are some

Page 94: devworx March 2013

92 Debugging on AnDroiD

utilities which you would want to

check out before distributing your

apps to the user like zipalign and

ProGuard. Most of these tools and

the ones that are included in this

chapter require extensive reading

of official documentation and other

resources, because each of these

are complex enough to have a com-

plete tutorial dedicated to them

that could span a few pages. the

trick is not how to debug using

these utilities, but infact extracting

useful information from the raw data that these utilities make available to

you and formulating an optimization strategy.the most important utility

however is DDMS, which is by far the most exhaustive and is offers complete

debugging and optimization environment for each aspect of the application.

With Intel's focus on android and special tools for android app develop-

ment such as Intel Hardware Accelerated Execution Manager, Intel Graphics

Performance analyzer and Intel atom x86 system image, the development

process is now greatly simplified.

The Pixel Perfect screen lets you take a peek at a pixel level

Intel haXM

http://software.intel.com/en-us/articles/intel-hardware-accelerated-execu-

tion-manager/∙

android Jelly Bean (4.2 ) x86 emulator System Image

http://software.intel.com/en-us/articles/android-4-2-jelly-bean-x86-emula-

tor-system-image∙

android Ice Cream Sandwich (4.0.4 ) x86 emulator System Image

http://software.intel.com/en-us/articles/android-ice-cream-sandwich-x86-

emulator-image/∙

Intel Graphics performance analyzer tool

http://software.intel.com/en-us/articles/intel-gpa-android-support

how to debug an app for android x86 and the tools to use:

http://software.intel.com/en-us/articles/how-to-debug-an-app-for-

android-x86-and-the-tools-to-use/

Do

wn

loa

d

Page 95: devworx March 2013

93Debugging on AnDroiD

Intel Graphics Performance AnalyzerThe Intel Graphics Performance Analyzer is a tool for collecting perfor-

mance metrics related to OpenGL based Games and graphical application

for the android as the target platform. It suppports all the three operating

systems namely Windows 7/8, Mac 10.7/10.8 and ubuntu Linux 11.10/12.04

for analysis. the tool can be downloaded from the Intel Website and can

be installed on ubuntu as follows:

1. Install the sharutils package before proceeding with the installation.

2. change the file permissions of the downloaded file with the following

command: chmod 755 intel-gpa _ 12.5 _ x86.deb.shar

this can be done similarly for the x64 package if you are installing

that version.

3. execute the extracted deb file and install the software with the

package manager.

The installation for Windows and Mac OS X platform is similar, where

you need to download the respective executables and follow the instructions

give by the installation wizard. Before proceeding futher, you will need to set

up the adb connection by installing it as instructed in the android Debug-

ging chapter and then provide the path to its directory in the settings dialog

which can be opened after launching the Intel GPA tool and accessing the

You can monitor all necessary parameters in real time with Intel GPA

Page 96: devworx March 2013

94 Debugging on AnDroiD

connection Dialog. alternatively, you can also connect to you phone over

wifi, for which you need to restart adb with the adb tcpip 5555 command

and then connecting to the phone by selecting it in the connection screen.

that is all you need to do for the initial set up, after which you can now

launch the Graphics Performance Analyzer and select the application to

be debugged / analyzed. however, the respective application needs to be

analyzable, for which you will need to go to the androidManifest.xml and

change its debuggable permission to be true.

When the GPA screen is opened, you will be able to see the performance

metrics on the left, which are organized according to their type, for eg. cPu,

Device IO, GPU, Memory, OpenGL, Power etc. The notification area shows

other information specific to the connection, like the application being

debugged, size of window, Vsync status and version of OpenGL ES API etc.

to select a metric to be displayed in the chart, select the metric and drag it to

the chart at an appropriate position. the chart area allows a maximum of 16

charts to visualize the metric values. Intel GPA can also be used to monitor

custom metrics defined by you specifically for your application. custom

metrics allow you to analyze all kinds of data which includes lights in the

scene, duration of post-processing pipeline stages etc. custom metrics need

to be defined in the applications source code itself with the help of OpenGL

ES extensions API provided by Intel GPA itself.

Intel GPA showing charts for each metric while analysing Angry Birds

Page 97: devworx March 2013

95

notes

Page 98: devworx March 2013

96

notes

Page 99: devworx March 2013

The

Inte

l® D

evel

oper

Zon

e co

mbi

nes

all t

he fa

mili

ar to

ols,

re

sour

ces,

and

sup

port

from

the

Inte

l® S

oftw

are

Partn

er

Prog

ram

, the

Inte

l® S

oftw

are

Net

wor

k an

d th

e In

tel A

ppUp

®

Deve

lope

r Pro

gram

into

one

pre

mie

r dev

elop

er p

rogr

am —

de

sign

ed to

pro

vide

the

deve

lopm

ent s

uppo

rt an

d bu

sine

ss

oppo

rtuni

ties

deve

lope

rs a

nd s

oftw

are

busi

ness

es n

eed

to

inno

vate

and

suc

ceed

.

Plea

se v

isit

Inte

l And

roid

Dev

elop

er Z

one

at th

e fo

llow

ing

link:

http

://so

ftwar

e.in

tel.c

om/e

n-us

/and

roid

/

Page 100: devworx March 2013

RNI REGN. No. MAH ENG/2001/3791