Sunday, April 21, 2013

Setting up an AVD emulator for application testing

At this stage, you have: 1. Downloaded ADT Bundle 2. Created a workplace for development (during Eclipse startup) and 3. Updated SDK tool package. This section describes how to setup your test bed for your applications.

Setting up your Android device for real time application testing:

1. Phone/Tablet: Go to Settings > Developer options > Enable USB debugging.
2. Connect your device to computer via standard USB cable. Skip any warning or notification popped up on your device.
3. Check connected device status: Open Terminal (or cmd.exe in Windows). Navigate to- 'platform-tools' inside sdk folder:
Example:
Ubuntu: evinish@68D2ZR1:~/Documents/Android/adt-bundle-linux-x86_64-20130219/sdk/platform-tools$
Windows: C:\Android\adt_bundle_windows\sdk\platform-tools
4. Type: adb devices. You should see your device name listed in your output:

Example:
List of devices attached
emulator-5554    device
YT910166SL    device

Your device is now ready to run your application. You may add the adb tool path in environment variable, so you don't have to navigate every time you launch command session.

Adding adb path to environment variable in Ubuntu

Open Terminal and follow these steps:

1. Check the path variable: echo $PATH
2. Add the adb path: export PATH=${PATH}:/home/evinish/Documents/Android/adt-bundle-linux-x86_64/sdk/platform-tools. This should be your path to adb.
3. To ensure the path is loaded each time system boots up, it should be added to .bashrc file:
echo -e '\nexport PATH=${PATH}:/home/evinish/Documents/Android/adt-bundle-linux-x86_64/sdk/platform-tools:/home/evinish/Documents/Android/adt-bundle-linux-x86_64/sdk/platform-tools' >> ~/.bashrc

Starting an AVD emulator

Fig 1.7 Creating AVD
Although, you can test your application on a real device, it is not recommended (unless it is absolutely necessary). Android SDK provides emulator tools to emulate your app on your computer itself. Follow these instructions to set up an AVD emulator:

1. Open Eclipse. Go to Window > Android Virtual Device Manager.
2. Click 'New' and follow new Emulator creation wizard: Provide a meaningful AVD name. Select 'Device' and 'Target' machine.
3. Select CPU/ABI: ARM (armeabi-v7a)
4. Select Emulation option: Snapshot
5. Click OK.
6. Select newly created AVD and click 'Start'. You'd see AVD initialization sequence in Eclipse console.

Fig 1.8 Android Virtual Device
Your new AVD would appear in a few minutes. The initial start-up may take upto 7 to 8 minutes!
If everything works fine, you'd see your AVD listed in the output of 'adb devices'

Known Issue #1 (Solved): Can't launch AVD emulator in Ubuntu

If you're using Ubuntu, there's a good chance your emulator won't start! Eclipse or AVD manager would not throw any error in such case. If so happens, check your console output. If the console output shows that preferred AVD is not available, then kill and re-start the ADB server.Open Terminal and type:

adb kill-server
adb start-server

Problem is still there?: goto tools directory in sdk bundle: evinish@68D2ZR1:~/Documents/Android/adt-bundle-linux-x86_64/sdk/tools$
It might be possible that Ubuntu is trying to run x86 or mips version of emulator. It's better to launch AVD from command line instead from Eclipse.
Can you run the emulator from command line?: ./emulator -avd <emulator_name>; emulator_name is the AVD name you created in eclipse. It might throw error like this:

Segmentation fault (Core dumped)

Now try to run the arm version of emulator directly from command line: ./emulator-arm -avd <emulator_name>. You would be able to launch the AVD! In order to launch AVD directly from Eclipse, here is a work around:
Seems like ADT can't load OpenGL driver on Ubuntu 12.04. The work around is to rename the file libOpenglRender.so
Solution: Goto the following path inside SDK bundle: tools/lib/ ----> Rename the file libOpenglRender.so to libOpenglRender.so.xxx so that Android can't find it!

Known Issue #2 (Solved): Can't delete AVD from Eclipse

If you can't delete AVD from AVD manager list, you can manually delete it. 
Go to /home/.android folder (/.android is a hidden file). Navigate to /avd and delete AVD from this folder. Now goto eclipse and delete AVD. That's it!!

Saturday, April 20, 2013

Setting up your development environment

Before you start, it is essential to setup your application development environment. You would require a computer running Windows/MAC/Linux based OS. I use Ubuntu 12.04 LTS for Android application development. You would also require an IDE (Integrated Development Environment) for writing, compiling and debugging your code.

Eclipse is a standard IDE and is very popular among Android developers. It is freely available on the Internet. Eclipse builds a complete project hierarchy for you (along with the skeleton app) and simplifies project  creation up to great extent. It is highly recommended for beginners as Eclipse also points out semantic errors and provides a list of 'Quick Fixes' which may come handy to all lazy developers (like ctrl+shift+o imports all missing classes).

The second thing is the Android SDK tools. It is a massive collection of Android APIs (Application Programming Interface) and developers' tools which you can use for application development.
The third one is Eclipse ADT (Android developers tool) plugin. This will make your life easier.. much easier! You can run your Android application either on a real device (I will tell you how) or on an Android Emulator (commonly known as Android Virtual Device or AVD). You can create an Android emulator on your computer for testing your applications using a special package- AVD manager. AVD manager comes bundled with ADT plugin for Eclipse.

Fig 1.4 Android Developer Tool
Google provides a complete bundled package of Eclipse, SDK tools and ADT plugin in the form of ADT Bundle. So, you don't need to download each one separately and configure them one by one (though you have an option to do that). With a single download, you can obtain all relevant

tools for Android development. ADT bundle includes:
a. Eclipse + ADT plugin
b. Android SDK Tools
c. Android Platform-tools
d. The latest Android platform
e. The latest Android system image for the emulator

Take your first step

a. Download ADT Bundle  for your OS from this link:
   http://developer.android.com/sdk/index.html


b. Once you've download the file, extract the content in an appropriate location (say C:\Android\ or ~/home/user/Android). You'll find two sub folders inside ADT Bundle folder (adt-bundle-linux-x86_64)- 'eclipse' and 'sdk'.


c. Updating SDK package: The ADT bundle you downloaded consists of minimalistic SDK features. You need to download additional emulator system images and tools for higher API levels (each Android release, for example- Gingerbread, Ice-cream Sandwich, Jellybean, etc is associated with an API level). Additional SDK tools are downloaded using a special utility called 'SDK Manager'. First of all, you need to set path for SDK tools in Eclipse: Go to Windows>Preferences>Android. Browse for the SDK folder inside ADT Bundle folder and click OK (Fig 1.5).
Fig 1.5 Setting up SDK path inside Eclipse
Now launch SDK manager: Go to Windows>Android SDK Manager. Select all and click 'Install packages' (Fig 1.6).
Fig 1.6 Updating SDK Tools




Tips n tricks:- If you're unable to download the packages, force https:// sources to be fetched using http:// and apply suitable proxy settings (if you're behind a firewall). In Android SDK Manager window, go to Tools>Options and do the necessary changes.

The next post will tell you how to set up an AVD emulator to test your application. You'll also find out how to run your application on a physical Android device.

Execution of Android programs

Android is a Linux based OS which forms an encapsulation shell around Linux kernel. All system calls and sub-routines from/to users (say an application component) are intercepted by Android Application Framework via core libraries assisted by Android run-time. Android runtime is composed of several core libraries and a virtual machine (VM). Every Java program is compiled into Java byte code which can be executed on a VM. Android is no exception.
Each Android program runs on separate instance of specialized Virtual Machines (VM) commonly known as Dalvik Virtual Machine (DVM) and Android programs are compiled into 'Dalvik Byte Codes'. DVMs are optimized for space and time complexities of code blocks. Virtual machines are just like your physical computer but equipped with virtual processor. So, its like a bunch of processors hooked up together to perform a task. Unlike traditional Java VMs (like Java ME), DVMs are designed to ensure that multiple instances run efficiently on a single device.

Dalvik Virtual Machine

The Dalvik VM uses the device’s underlying Linux kernel to handle low-level functionality, including security, threading, and process and memory management. It’s also possible to write C/C++ applications that run closer to the underlying Linux OS (this will be covered in advanced section of Android programing. Instead of using SDK, Android NDK is used where you need fast code execution time). In other words, Dalvik and the Android run time sit on top of a Linux kernel that handles low-level hardware interaction, including drivers and memory management, while a set of APIs provides access to all the underlying services, features, and hardware. Refer to the next section for better understanding.

Android Software Stack
Fig 1.3 Android Software Stack

This is gonna be your world! Any android application you see around you is a part of this stack. In simple terms, Android software stack is a bundle of underlying Linux kernel (for handling low level system calls intended to control actual hardware) and a collection of C/C++ core libraries. This section will introduce Android software stack in detail.

1. Linux kernel: Core services (hardware drivers, process and memory management, etc). This also provides a layer of abstraction to upper stack component. So, your application can run smoothly on any device as long as the system calls are successfully resolved by kernel layer.
2. Libraries: Running on top of the kernel, Android includes various C/C++ libraries. These libraries have content that can be shared among various application component running at the front/back end. Of course, appropriate permission is required to access shared library components. Various libraries are: 
a. A media library for playback of audio and video media
b. A surface manager to provide display management
c. Graphics libraries that include SGL and OpenGL for 2D and 3D graphics
d. SQLite for native database support
e. SSL and WebKit for integrated web browser and Internet security
3. Android Runtime: It is composed of two parts:
a. Core libraries - The core Android libraries provide most of the functionality available in the core Java libraries, as well as the Android specific libraries.
b. Dalvik VM - Dalvik is a register-based Virtual Machine that’s been optimized to ensure that a device can run multiple instances efficiently. It relies on the Linux kernel for threading and low-level memory management. 
4. Application Framework: The application framework provides the classes used to create Android applications. It also provides a generic abstraction for hardware access and manages the user interface and application resources.
5. Application Layer: Your Android application runs in this layer. It uses the class and hardware services provided by lower abstraction layers 


Friday, April 19, 2013

Just a mobile phone?

Not exactly!! With the latest advancement in technology, your small, stylish and versatile mobile phone has become a power packed computational module capable of doing almost all day to day computing task for average users (you heard me!). Do you know, an average cell phone is about 100 times more powerful than the on-board computers they used in Apollo-11?

Fig 1.2 Mobile phone or a work-station?
Modern mobile devices have become powerful tools that incorporate touchscreen, camera, media players, Global Positioning System (GPS) receivers, and Near Field Communications (NFC) hardware. As technology has evolved, mobile phones have become about much more than simply making calls. They've become a self contained hand held computers. You shouldn't be surprised if you find someone driving his Corolla using his smartphone (NASA is even planning to launch Android powered micro satellites!). Possibilities are endless and imagination is infinite! Android is flowing beyond hand-held device territory!

What does it take to become an Android programmer?

Android code is highly based on Java syntax. So if you've programmed in Java before, you'll definitely get an advantage over others.

Personally speaking, if you're familiar with C or OOP language semantics, you'll do just fine. Programming language is no different than your mother tongue. You didn't learn language grammar before you start talking as a kid... did you? For computer languages, all you have to do is to be aware of some very basic semantics and logic operations. You'll learn more and more language features as you dive deeper into programming! Learn by doing- That's our motto! Your learning curve would be flat in the beginning, but believe me, it would get steeper and steeper with time. You'll learn faster and grasp more stuff if you concentrate on language features. Bottom Line: DON'T GIVE UP!! EVER!

Your strong determination would drive you from Noob-land to Expert territory, and if you've read this far, I assume you've got a pretty strong one.

Tuesday, April 16, 2013

Hello World!

Well… I know this is not a new term for you. About 65% of world’s smart-phones run Android! So, there might be a good chance that you’re holding an Android device as you read this! Some say that Android is the next big thing after the World War-II. But…. what is Android? How’s the little green bug-eyed bot changing the world we see? What are the future prospects of Android? Will it wash my grandson’s socks???? The answer to all these questions is pretty neat- It is the future of mobile computing! That’s it! It’s only been around six years and Android has already taken a quantum leap from its primitive form to what we see today.

The Little Green Bot


Android is a Linux based operating system designed mainly for low power devices like smartphones, tablet computers, cameras, watches, etc.  It was developed by Android Inc. which Google bought later in 2005. In the year 2007, a number of hardware, software and telecommunication giants (Google, HTC, Sony, Dell, Intel, Motorola, Qualcomm, Texas Instruments, Samsung Electronics, LG Electronics, T-Mobile, Sprint Nextel, NVidia, and Wind River Systems) joined their hands and formed ‘Open Handset Alliance’. 

Fig 1.1 Open Handset Alliance Founders
The prime motive behind this alliance was to develop transparent standards and protocols for mobile phones. This was the time when Android came into limelight. It was decided to beef up Android stack with standard and open source telephony and multimedia protocols.
This is the open source code and permissive licensing that allows the software to be freely modified and distributed by device manufacturers, wireless carriers and enthusiast developers.  Today, Android has the largest community of developers busy writing applications that extend device capability beyond imagination.