Tuesday, September 12, 2017

Developing for Android - An Introduction

What is Android?

Android is the world's most popular operating system for mobile devices and tablets. It is an open source operating system, created by Google, and available to all kinds of developers with various expertise levels, ranging from rookie to professional.
(The term 'open source' sounds pretty familiar, doesn't it? Well, open-source means software with source available for modification and bound to an open source license agreement. More about open source terminology can be found here).
From a developer's perspective, Android is a Linux-based operating system for smartphones and tablets. It includes a touch screen user interface, widgets, camera, network data monitoring and all the other features that enable a cell phone to be called a smartphone. Android is a platform that supports various applications, available through the Android Play Store. The Android platform also allows end users to develop, install and use their own applications on top of the Android framework. The Android framework is licensed under the Apache License, with Android application developers holding the right to distribute their applications under their customized license. 




Like most software, Android is released in versions. Google has also assigned names to its versions since April 2009. Below are all the versions of Android released to date: 

Version No.NameFor:
1.0Android BetaPhone
1.1AndroidPhone
1.5CupcakePhone
1.6DonutPhone
2.0/2.1EclairPhone
2.2.xFroyoPhone
2.3.xGingerbreadPhone
3.xHoneycombTablet
4.0.xIce Cream SandwichPhone and Tablet
4.1/4.2Jelly BeanPhone and Tablet
As we can see in the table above, various versions of Android are supported on phones and tablets. There are umpteen Android devices available in the market from manufacturers like Samsung, HTC, Motorola and others. Google itself also has phones made in conjunction with OEMs, branded as the Nexus series.

Understanding Android

To begin development on Android even at the application level, I think it is paramount to understand the basic internal architecture. Knowing how things are arranged inside helps us understand the application framework better, so we can can design the application in a better way.
Android is an OS based on Linux. Hence, deep inside, Android is pretty similar to Linux. To begin our dive into the Android internals, let us look at an architectural diagram.
architecture diagram The above diagram illustrates the Android architecture. As you can see, it is a software stack above the hardware that is provided by the OEMs. Let's start with the topmost layer, i.e., the applications. 

Applications

The diagram shows four basic apps (App 1, App 2, App 3 and App 4), just to give the idea that there can be multiple apps sitting on top of Android. These apps are like any user interface you use on Android; for example, when you use a music player, the GUI on which there are buttons to play, pause, seek, etc is an application. Similarly, is an app for making calls, a camera app, and so on. All these apps are not necessarily from Google. Anyone can develop an app and make it available to everyone through Google Play Store. These apps are developed in Java, and are installed directly, without the need to integrate with Android OS.

Application Framework

Scratching further below the applications, we reach the application framework, which application developers can leverage in developing Android applications. The framework offers a huge set of APIs used by developers for various standard purposes, so that they don't have to code every basic task.The framework consists of certain entities; major ones are:
  • Activity Manager This manages the activities that govern the application life cycle and has several states. An application may have multiple activities, which have their own life cycles. However, there is one main activity that starts when the application is launched. Generally, each activity in an application is given a window that has its own layout and user interface. An activity is stopped when another starts, and gets back to the window that initiated it through an activity callback. 
  • Notification Manager This manager enables the applications to create customized alerts
  • Views Views are used to create layouts, including components such as grids, lists, buttons, etc.
  • Resource Managers Applications do require external resources, such as graphics, external strings, etc. All these resources are managed by the resource manager, which makes them available in a standardized way.
  • Content Provider Applications also share data. From time to time, one application may need some data from another application. For example, an international calling application will need to access the user's address book. This access to another application's data is enabled by the content providers.

Libraries

This layer holds the Android native libraries. These libraries are written in C/C++ and offer capabilities similar to the above layer, while sitting on top of the kernel. A few of the major native libraries include
  • Surface Manager: Manages the display and compositing window-ing manager. - Media framework: Supports various audio and video formats and codecs including their playback and recording.
  • System C Libraries: Standard C library like libc targeted for ARM or embedded devices.
  • OpenGL ES Libraries : These are the graphics libraries for rendering 2D and 3D graphics.
  • SQLite : A database engine for Android.

Android Runtime

The Android runtime consists of the Dalvik Virtual Machine. It is basically a virtual machine for embedded devices, which like any other virtual machine is a bytecode interpreter. When we say it is for embedded devices, it means it is low on memory, comparatively slower and runs on battery power. Besides the Dalvik Virtual Machine, it also consists of the core libraries, which are Java libraries and are available for all devices. 

Kernel

The Android OS is derived from Linux Kernel 2.6 and is actually created from Linux source, compiled for mobile devices. The memory management, process management etc. are mostly similar. The kernel acts as a Hardware Abstraction Layer between hardware and the Android software stack. 

Android SDK

As already mentioned, Android is open source and hence the source code is available for all developers. In totality it is called the Android SDK. You can download, build and work on Android in a number of different ways--it all depends on what you want to do. If your goal is to develop an Android application, you don't necessarily need to download all the source. Google recommends the Eclipse IDE, for which there is an available Android Developer Tools (ADT) plugin, through which you can install the specific SDK, create projects, launch emulators, debug, etc. You can see more details of Eclipse and ADT through Android's official website for developers - http://developer.android.com/sdk/index.html 

Android Development for Windows Users

Android as of now does not support building on Windows, so if you want to modify the Android OS itself, you'll have to use Linux (see building the Android OS). However, on Windows, we do have tools and plugins for application and native Android development. And here we will talk about setting up basic Android development tools on Windows. 

Downloading the Android SDK and developer tools

Google provides a convenient bundle to download and setup Android for Windows developers, which you can download here, under the name ADT bundle for Windows. The exact name of the file you download will depend on your OS architecture (32 vs 64 bit), but for my case (64 Bit Win 7), I see the following zip file downloaded: adt-bundle-windows-x86_64.zip. Extracting the zip file, I have contents as in the following snapshot:
 
First of all, we have Eclipse, which is the IDE for writing source. As an IDE it provides the environment for developing Android Applications. Android Applications are developed primarily in Java. Next we have the 'sdk', which does not include any of the source. However, it holds the already built platform tools, tools, images and some platform specific libraries. When we say, building Android is not supported on Windows,we mean that we can't compile system images and tools. However, other sources needed for application development are be available through the SDK Manager, which is the third entity present in the extracted zip file. 
So, let's download the sources! Double click the SDKManager.exe. You'll see something like this: 
 
This is the SDK Manager, via which we can install or delete whatever version of SDK we want. As you can see, it mentions we have the Android SDK Tools and Android SDK Platform Tools installed.
The latest Android available, as of the writing of this article, is 4.2, but with the SDK we can download and install any of the previous versions too. Now let us play around with the latest Android--i.e.,4.2, which is also known as Jelly Bean. 
Select the check box for "Android 4.2(API 17)", which will select everything required for and under Android 4.2. 



In all, as we can see, SDK Manager found 6 packages that need installation. Click the "Install 6 packages" button. We see another dialog box for package descriptions and license. 


Select "Accept All" and click Install, which will initiate the download and then installation. When done, you will see "Installed" in front of all the packages selected. 
It's now time to launch Eclipse, but first, we need to make sure that we have the Java Developer Kit (JDK) installed. If you don't have it, you can download it from Oracle here. I have JDK 7 installed in my case. Next, we need to launch Eclipse from the executable present in the Eclipse directory . If we obtained Eclipse via the instructions in this article, it should already have the ADT plugin installed. (Otherwise, you can download the ADT separately by following the instructions here.) Eclipse generally asks for a workspace path where it will create and maintain projects.
Here is what the newly launched Eclipse looks like: 

 
Eclipse also includes the SDK Manager from where you can manage the SDK packages. Check out the Window menu and select "Android SDK Manager". To see what the emulator looks like, go to the Window menu and select "Android Virtual Device Manager". From there, we can create our virtual device or use one of the standard devices available. To create a new virtual device, click on 'New' as we see in the following snapshot:

 
Another window will pop up to take inputs for device type, target processor, memory, etc.

 
You can provide customized device specifications for a virtual device. Once the device is created by clicking 'OK', it will be available in the list on "Android Virtual Device Manager" window. To launch the emulator for your own defined virtual device, select it and click "Start".
You can also select pre-loaded options that correspond to the specifications of existing Android devices. To see them in the Android Virtual Device Manager, go to the Device Definitions tab. 

 
We select the first in our list, "Nexus S by Google", and add a virtual device by clicking "Create AVD". The following dialog box requires the "Target" and "CPU" to be specified along with the size of the SD Card.

 We will assign Target as "Android 4.2 - API level 17" CPU as "ARM" and SD Card Size equals "1024 MiB" and click "OK". We can now see the newly defined virtual device in the AVD list

 
To launch the emulator, select it and click "Start". Here's what the emulator looks like: 



We can now use our newly-created emulator for running our Windows Android apps. All we have to do is compile our code, then load the app onto the emulator. 

Building the Android OS

The above instructions, for Windows users, will work great if all you want to do is create Android apps. However, you can do even more with Android, including modifying the Android OS to create Android ROMs or MODs--that's the beauty of open source! To do this, you'll need to download the complete available Android source from its repository and cross-compiling for the device. You'll also need to be using Linux, since building Android is not supposed on Windows machines. And before we move further, there are certain assumptions to get out of the way:
1. All the information is generic and should work for all Linux flavours, however these instructions have only been tried and tested on Ubuntu 11.04.
2. Because we had to pick one of the versions of Android, we chose Android 4.0.1. Hence, some commands might be specific to Android 4.0.1, but things might be slightly different for other versions.

Memory Requirements

It is always a wise idea to check the memory requirements before starting any project. The size of the Android SDK is around 8.5GB and you will need around 30GB free disk space to build it. 

Prerequisite Installations

Prior to downloading the SDK and starting cross-compiling, there are certain prerequisites of the Android SDK we have to have. It is better to set up these before jumping into the Android SDK, as we all know prevention is much better than debugging! First of all, for the Android version we have chosen (i.e Android 4.0.1) cross compilation is well tested on 64 -bit machines, but the documentation says it is experimental on 32-bit machines. 1. JDK Android SDK building requires the JDK, so you must install the JDK. For Android versions 2.3.x and later, one needs to install Java 6. It is recommend to install Sun JDK only, rather than Open JDK.. First, download the JDK 6. Then run the following commands. (Please Note: To avoid specifying a specific version, the installer binary name and directories are modified to be generic names.) 
$ chmod +x jdk-6xxxx-linux-xxx.bin
$ sudo ./jdk-6xxxx-linux-xxx.bin
$ sudo mv jdk1.6.xxxx /usr/lib/jvm/
 
$sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.xxxx/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.xxxx/bin/javac 1
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.xxxx/bin/javaws 1
 
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config javaws
For versions prior to 2.3.x, Java 5 is required. 2. You also need to install the following packages: 
$sudo apt-get install git-core gnupg flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl  
libncurses5-dev zlib1g-dev ant gcc-multilib g++-multilib
$sudo apt-get install libx11-dev:i386
These packages are needed for downloading and compiling the SDK. Once these get installed, you are ready to download the Android SDK. There may be some optional things to do, for example enabling caching, etc. But we will not go into much detail here as we don't really need to do those things.

Downloading the Android SDK

The entire Android SDK is stored in a git repository that also maintains various other versions as well. We need to know the repository path and then download to a separate directory where we wish to store the complete source.
Let us first create a root source directory.
$mkdir android
Further, we will initialise and configure a tool 'repo', which is used to work with git conveniently. To download it, we need a 'bin' directory in our home, which should be added to the path.
$mkdir bin
$export PATH=$PATH:~/bin
$curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > bin/repo
$chmod a+x bin/repo
Now we have the repo installed and must initialise it with the path of git where the Android SDK lies. There is a master branch of git, which holds the latest Android version. The master branch in git is the main source repository, which means any new release or new update would be part of this master branch. However, if we want a specific Android version, we need to initialise with a specific branch. 
Here in this article, we will go for a specific branch, since the master will keep on changing.
$repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
It will ask you for a name and email id, and you must provide them.
To get all the Android SDK files, you just now need to do: 
$repo sync
It may take some time to download the complete source. In the end, we get 
Syncing work tree: 100% (221/221), done. 
This indicates the sources are downloaded. Let's have a look at the folder structure from the topmost level; here is what I have:
$ls
abi       build   development  external    libcore   packages  system
bionic    cts     device       frameworks  Makefile  prebuilt
bootable  dalvik  docs         hardware    ndk       sdk

Building Android

Now that we have the source, the next step is to build it. The first thing we need to determine before building is what platform are we going to run this Android on? It is essential to determine this. The options could be the type of your device, or an emulator.
When flashing a phone, the worst case it that you may brick the phone in the event of a critical bug. As a result, it is a good idea to develop on Android through an emulator--so we'll do that.
First of all, we need to set the environment variables, which are specified in a shell script. 
$ source build/envsetup.sh
including device/samsung/maguro/vendorsetup.sh
including device/samsung/tuna/vendorsetup.sh
including device/ti/panda/vendorsetup.sh
including sdk/bash_completion/adb.bash
Next, we need to specify the target for which we need to cross-compile, using the command 'lunch'. But first of all, what is cross-compiling and why we need to do it? Well, cross-compiling is a compiling process that is done for a different platform, to create an executable or library to be used on a that different platform. Therefore, we would have two platforms, 
  • host : On which we have the sources and are being cross-compiled. In our case, this is our Linux machine.
  • target: The platform for which the sources are being compiled for, and the compiled image or library will be used on the target platform. In this case, this would be our Android emulator.
This is required for embedded systems, as not all platforms support compiling and debugging. Examples of such platforms are ARM, MIPS, etc. For the target emulator and development build, we do 
$lunch full-eng
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.0.1
TARGET_PRODUCT=full
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=ITL41D
============================================
Here 'full' is for emulator and 'eng' is because we want a development build. To see other available options, do: 
$lunch
You're building on Linux

Lunch menu... pick a combo:
     1. full-eng
     2. full_x86-eng
     3. vbox_x86-eng
     4. full_maguro-userdebug
     5. full_tuna-userdebug
     6. full_panda-en
Next, we just have to build the code using 'make'. GNU make has the ability to run parallel tasks. How many tasks to run in parallel is determined by the '-j' option. The usage is make -jN, where N is the number For example, to run 4 parallel tasks, our command would be
$make -j4
It is common to set this number between 1 to 2 times the number of hardware threads the computer supports, which is generally the number of processors. To know how many processors we have in our system, we need to peek into the cpuinfo. To get the CPU info, we have command 
$cat /proc/cpuinfo
However, to get to know the number of processors in a single command, we can use 
$grep -c ^processor /proc/cpuinfo
The building of Android also takes quite a lot of time when done for the first time. Note, all the images, libraries and applications built are placed in the directory out/target/product/generic/ 

Running on the Emulator

To launch the emulator with our built Android, just run the following command
$emulator
This will automatically pick the built images and binaries for the recently built Android. You can have the look and feel of the emulator environment, which is pretty similar to a device.

1 comment:



  1. If you ever want to change or up your university grades contact cybergolden hacker he'll get it done and show a proof of work done before payment. He's efficient, reliable and affordable. He can also perform all sorts of hacks including text, whatsapp, password decrypt,hack any mobile phone, Escape Bancruptcy, Delete Criminal Records and the rest

    Email: cybergoldenhacker at gmail dot com

    ReplyDelete