Sunday, 19 October 2014

Android Programming Basics

Background

Today Android has become very popular operating System. Most of the smart phones have Android as it's OS. Also if you don't know Android OS is not only used in smart phones but also in

Tablet

Computers

Laptops

Watches

Cars

Guns ......

and the list will continue...... . Lets start knowing more about Android.

To start with.....

At the backend we have Linux kernel running. If you are a software developer than you might already be familiar with many aspects of Android such as

Linux kernel

SQLite database [Yes Android has this in built for persistent storage]

Open GL library

Other aspects like Activities, it's life cycle, virtual machine that manages the processes are different. We will get to those.

Android Versions

Android versions that have been released till now -

Alpha (1.0)

Beta (1.1)

Cupcake (1.5)

Donut (1.6)

Eclair (2.0–2.1)

Froyo (2.2–2.2.3)

Gingerbread (2.3–2.3.7)

Honeycomb (3.0–3.2.6)

Ice Cream Sandwich (4.0–4.0.4)

Jelly Bean (4.1–4.3.1)

KitKat (4.4–4.4.4)

Lollipop (5.0)

Android Runtime

If you are familiar with Java then you must be aware of Java runtime (JRE) and also the JVM (Java virtual machine that runs the actual java processes). .java files are converted to .class files by java compiler . class files are essentially byte code that java interpreter in JVM understands.

Similarly Android has it's own set of APIs. We use this APIs to write Android applications in Java. Important changes in Android are -

Android uses Dalvik virtual machine to to manage Android processes. Dalvik is Google's implementation of JVM that is optimized for handheld devices.

From Jelly bean we also have another option for Android runtime and that is ART .So from Jelly Bean onwards we can select either of the runtimes from Developer options -

Dalvik

ART

(Above screenshot is from my Moto E running Kitkat)

.class files and jar files generated from .java files are further converted to .dex files as .dex files are more compact and efficient and form an important aspect memory limitations.

Android System Architecture

Android System architecture is as shown in above diagram. Top most layer is your Applications/Widgets. They talk to the underlying framework. For example Activity Manager manages the activity lifecycle of all the activities that form a part of your Application.Each module in the framework will inturn use libraries which may be 3rd pary like OpenGL or own APIs (Android runtime libraries). Finally at the bottom is the Linux kernel that takes care of processes, drivers etc.

Lets go over each layer in detail -

Linux Kernel : As any other Linux kernel this provides memory management, process management, security, drivers, File & Network I/O. In addition to this there are some android specific features that are added to this kernel layer. These include power management services, managing low memory, IPC (Inter process communication) between processes.

Libraries : These include native libraries written in C/C++ for performance. For eg. Surface manager to render display, media framework for audio/video, Webkit for rendering browser pages, OpenGL for graphics and SQLite for database. This layer also include Android Runtime - Core Java library and Dalvik VM. Dalvik VM is similar to normal Java virtual machine but optimized for hand held devices (designed for resource constraint environment). [Code is compiled into bytecode class file which is then converted to .dex files that is then installed on android device]

Application Framework : These layer mainly consists of frameworks that can be used by your android Application. These frameworks inturn will communicate with underlying native libraries to achieve what you desire. Think of it as kind of abstraction. You don't have to dive down to understand native code. Eg. package manager that takes care of installed apps, Activity manager that takes care of Activity lifecycle etc.

Applications : These are applications that you as a developer write (mostly in Java). As stated earlier you can use various managers at framework level to harness android features. There are also some in built apps that come with the android phone like browser, native email, phone dialer etc.

Building Blocks of Android

Every developer need to know following entities in Android

Activities

Intents

Services

Content Providers

You can read about them in a separate post that I had written some time back -

Just to give an overview I'll try to explain the use cases of above entities. If you have an Android smart phone the icons you see on your screen are nothing but intents. Intents are basically a mechanism for performing an action like stating an Activity or Make a call or share your picture. So when you click on the icon you basically trigger an intent that start your Application. Another exaple would be sharing your picture. When you click share you must have see a lot of options like Facebook, Gmail, Twitter etc. These are basically Applications that are listening to the intent specific to sharing some status. When you click on share it will fire an intent and will give you list of applications that are listening or registered for this intent. Activities are UIs that form you Application. Each application can have one or more activities. Each Activity can further have fragments (Don't worry about this if you are not aware - but it's the tabs you see on the screen like for dual SIM). Services are nothing but daemon processes that run in background. Music ? Does it get closed when you navigate or open some other application ? No... That's a service. Service will continue to run unless you explicitly terminate it. Content providers are basically way to share data between applications.

Resources in Android Project

Resources can be your icons/images that your Application uses, the localized Strings, layouts etc. Android resource compiler compresses and packs the resources and then generates a class named R that contains references or identifiers that you can use your code to reference the resources.

Android Manifest file

Manifest file form a very important part of any Android Application project. It provides Android OS information about the application.

It must always be named as AndroidManifest.xml and should be available in projects root directory.

It should contain permissions that application uses. These are shown to user for accepting when the application is installed.

It also has the Minimum API level or Android version that your application supports. Google play will decide compatibility of application for your device on the basis of this..

It also has other components listed such as Activities in the application, intent filters that the app listens too etc.

Recommended book

I have found following book to be very useful for beginners (like me :))