Android: Getting Started


The Android Mobile platform has certainly taken the industry by storm. Its got cool features and even though it hasnt got the popularity of a Windows Mobile or Apple’s iOS, its destined to lead the market in a couple of years. For that to happen, Android needs more developers and Engineers to use its API to build applications for the platform enabled devices.

Windows Mobile and iOS also provide API for developers to develop apps and other software. But the thing that sets Android apart is the fact that the ‘Entire Android API’ is available in the public domain. What I mean is as a developer, you can use the exact same stuff that was used to develop the core Android OS to develop your applications for the Android powered devices. That means you have access to each and every component of the device. From the touch screen to the Accelerometer, from the Camera to the Proximity sensor and from Google maps to GPS devices. You can do anything with your apps. And another advantage is that Android uses Java Programming language. You just need to use the Android packages to build your applications and Android uses a similar Virtual Machine to run its applications like the highly popular JVM. So it shares most of the features of the Java platforms. The best feature is the Platform Neutral Development. You can start developing Android apps with whatever OS you use. Be it Windows, Macintosh or Linux etc. And it has got the backing of Google too. What more do you want?

I was one of those programmers who thought Android development would be very tough. But I saw one of my friends developing an app and and only then I realized the ease of creating Android Application. Fortunately the Eclipse IDE (by far the best IDE to build Java apps) supports Android development and makes it very easy. All you need to do is install a plugin from Google (License holders of Android).  And for testing your Android applications you dont need an Android device. You can use the innovative Android Emulator (Android Virtual Device) to run and test your applications. So I decided to write this article to provide all the stuff you will need to setup your Software Infrastructure needed to get started with your Android Adventure!

1. First of all, since you’re gonna work with Java, you need the latest version of Java SE SDK. At the time of writing this article, the latest version of the JDK (Java SDK) was JDK 1.6.0_26. So you can get this from Sun’s website. Download the the JDK for your platform (OS) from the following link:

http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u26-download-400750.html

After downloading the JDK, install it. Installation is pretty straight forward. You should be able to install it easily. But If you couldn’t install the JDK, brother, computer Science isn’t your field. You can try Management or something like that. Lol! Just kidding. If you face any problems just leave a comment here and Ill try to help. Or you could also open a thread at the Sun Java Support forums and there are lots of experts who would be more than happy to lend a helping hand. So don’t worry!

2. Now that you’ve got the JDK installed in your computer, we need to get the Android SDK (ADK) installed. Android SDK can be obtained from Android’s website for developers, which is developer.android.com. Its a great website to know more about Android Application development. Its very user-friendly. You can download the SDK from the following link. Choose the SDK for your platform.

http://developer.android.com/sdk/index.html

Windows users who download the exe version should be able to install the SDK easily. But if you downloaded the zip version for Windows and Mac or the tgz file for the Linux, then just extract the contents of the archive to a location in C: Drive. It would be better if you could place it in Program Files (in case of Windows) because thats where your Java is also installed.

3. Now that you have installed the SDK, you have to add Platform tools, Documentation and most important of all, the Android APIs for various Android platforms. At the time of writing this article the latest platform version was Android 3.1. Selecting a proper Platform is crucial because you need to develop the application for an Android device using the API of the Android platform that the device is using. (i.e) If you are developing an app for Android 1.6 device, you need to have the Android 1.6 API installed and you need to use this to develop your app.

So now you need to download the required platform APIs for the SDK. This is very easy to do. Now navigate to the directory where you extracted/installed the Android SDK. Inside the directory, you’ll find an executable called ‘SDK Manager.exe’  Double click it. Now you’ll see a window that searches the various repositories and finds the latest stuff available for you. After searching you should get a window something like this…

Now this window shows the latest stuff available for you. This may look confusing and very uncomfortable considering the fact that you’re just starting out. So press cancel and close the window. Now you’ll have the main SDK manager Window. In this window, on the left pane, you will see  5 options – Virtual Devices, Installed Packages, Available Packages, Settings and About. Dont get confused about all these options. We’ll come to them later. Now to see the available packages for download, select “Available Packages” option. If you do it, you will see two categories in the centre pane like this…

Now these two categories represent the two groups of API and tools available to you. One is Android Repository which contains the Platform APIs and other tools offered by Android. And the next option, 3rd Party Add-ons contains API and tools provided by 3rd party Companies like Samsung, Sony, Google (well, not exactly third-party. But still a separate entity from Android) etc. Expand them and see what they offer for you. You should get the following stuff…

Now you can select the platforms and tools you need. Now if you find it difficult to choose the required stuff, Ill suggest some for a start.

Make sure you select the following stuff:

  1. Android SDK tools r11
  2. Android Platform Tools
  3. Documentation for Android SDK, API 12, r1
  4. Any number of Android Platforms you need
  5. Select the sample codes for whatever API version you chose in the previous step
  6. Now, under the Third Party section, Google provides some APIs. These APIs allow you to add proprietary Google features like the Google Maps service in your app. So if you want to add that functionality to your apps, select the required Google API.
  7. You can also use APIs of other device makers like Sony, Samsung and LG.

Now All you have to do is press the Install Selected Button. After pressing that button, you will get a window exactly similar to the one you got when you started the SDK Manager Application. In this window, you will see all the stuff that you selected. Now I’ve selected some tools and platforms for me and it is showing the ones that I selected. Now you need to accept the Package License Agreement. If you want to read all the Package information select the package and the details for that package will be displayed in the center section. Now Select Accept All Radio button and select install. Now it should automatically start downloading the selected items and Install them to your computer. After Installing you can press the Close button in the “Download Window”. And then you can close the SDK Manager.

4.  Let us now install the eclipse IDE in your computer. If you already have the latest version, you can skip this step and proceed to step 5. Eclipse is a free IDE for Java development. But they also provide IDEs for many other languages. But what we need is an IDE for Java. Lets go get it! Use the following link:

http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/heliossr2

Dont get confused by the stuff given in this page. All you need to see are the links on the right side pane of the page. You will see links for Windows 32 bit, 64 bit, Mac and Linux. Select the version according to your platform. You will be taken to another page that will provide the download links. On this page you will see a big Down Arrow button. Click it and the download should start. If you prefer downloading via bit-torrent. Select the bit-torrent option and a .torrent file will be downloaded. Use it to download the IDE. The IDE comes ‘packaged’ in an archive. Extract it to the required directory. You need to do any Installation. Now you’ve got yourselves a proper Java IDE.

5. Eclipse IDE has been the benchmark for all Java IDEs and none of the other IDEs have come close to the Eclipse. Its sleek, user friendly, powerful and provides feature filled debugging options. One of the best things that happened for the Android developers is the introduction of the Android Development Tool (ADT) for Eclipse IDE. This tool allowed developers to create, test and deploy their Android apps using the Eclipse IDE. So let us download this plug in for Eclipse.

This plug in is downloaded through Eclipse itself. Go to the directory where you extracted Eclipse and run the eclipse.exe file. Now if this is the first time you are running the IDE, it will ask you to set a default working directory (i.e) the folder to store all your project files. Id recommend creating a folder for Android apps and set default Working Directory as that directory so that you can easily and conveniently save our projects. This directory can be changed any time and also you can save your projects anywhere you wish. Lets get down to business. After the IDE loads you will see the the IDE window with all sort of “complex” panes and options. Please dont get discouraged by all this. Its very simple to use actually. Once you develop one or two applications using Eclipse, you will start to love it. So now, select Help -> Install New Software

You will get a window like the one below.

Now near the text box that’s present on the top portion of the window, you will see a Button called “Add”. Press it. And you will see a window like the one below.

Now add the following data to the two fields:

Name:  ADT Plugin

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

Press OK.  In the Available Software section, you will see a check box near Developer Tools. Select it and click Next.  In the next window, you’ll see a list of the tools to be downloaded. Review it and click next. And in the next window, there will be license agreements. Read (lol) it and press Finish. When I pressed finish, I got a message asking to verify validity of the software being downloaded. Verify it proceed. Now download and installation will start. When the installation completes, restart Eclipse.

Great! Now we have setup everything needed to start developing. But wait, where do you test the applications you develop? You could do it on your phone. But if you dont have an Android phone dont worry. We have a facility to create a Virtual Device that emulates an Android phone!

Lets see how to create one:

Open the SDK Manager from the directory where you extracted the Android SDK. When you open it you will shown the list of available tools and platforms as you were shown earlier. Cancel that window and get to the main window, on the left pane select Virtual Devices. Now on the right pane you will see the list of Android virtual devices already created. Since we haven’t created one, it will be empty. Now press the “New” button on the right side. Now you will see a window where you can add whatever feature you want on your virtual device. And you can even select the SD card size, Display Size and most importantly the platform that the Virtual Device will run. So select all the options you need. You can select the Platform to use from the Target drop down box. Select appropriate display size. Now to add various hardware features, click new in the Hardware Section and you will see a whole host of hardware features. Select the one you need and set its property by double clicking it and selecting the value.

I have selected the options for creating an Android Virtual device that runs Android 1.5 with 512 MB SD Card and HVGA display and with D-Pad, Touch Screen and Camera. Its important to choose the options to mimic the device for which you are developing the application.

Finally Press Create AVD. You will get a confirmation window and you will see that the newly created Virtual Device in the list of Virtual Devices. Now to test drive the newly create device, select it and press the start button. You will see a window that asks you to scale display options or to wipe previous usage data. Just press Launch to Start the device. And Voila! Your very own Android Device! The device will behave exactly like a real mobile phone. That means you will have to go though the slow booting process of a smart phone. Enjoy!

Well, we have come to the end of the tutorial. I have given you all that you would need to start developing your very own apps for Android. There are a lot of books available for you to study Android programming.

One of the best books for amateurs and novices is Android Application Development For Dummies by Donn Felker. If you want a pdf of the book, drop a comment below or contact me via twitter @stevanity

You will find Android Development easy, adventurous and interesting. Well, thats what Android is all about. The Eclipse IDE supports android development comprehensively. From project creation to running the project, it will provide you with a very user-friendly workflow. You can publish your apps online and if your very serious about developing apps, you can get an account at Android App store for something like $25 and you can publish your apps, so that people can buy it. Show your creativity and skill to the world!

As always, if you have any doubts, suggestions etc just drop a comment. And If you like this article share it on Facebook, twitter, etc using the share buttons below.

Thank you,

Steve Robinson

Interfaces and Abstract Classes: Clearing the fog…..


There are two very important concepts or features in many Object Oriented Programming Languages that allow programmers to create a common interface to communicate with objects. They are Abstract Classes and Interfaces. They are fundamentally different but are used to provide the same functionality. Let us see about them separately and then we will see when to use an Interface and when to use an Abstract Class.

An Abstract Class is a class which does not provide concrete implementations (definitions) for its methods. Generally while creating a Class Hierarchy, we create a very general base class and derive these general members into a more specific Sub Class. I found the example in Paul and Harvey Deitel’s “C# for Programmers”  book very interesting and insightful.

A company wants to create an application to calculate salaries for the different types of employees it has, like Salaried Employees, Hourly Employee, Commissioned Employee. So basically you create a general Employee Class which will be abstract (to make a class Abstract just add the keyword ‘abstract’ before the keyword ‘class’) and put some general attributes and methods in it. But since the way the Salary is calculated differs from employee to employee, we can declare the Salary Calculation method as Abstract. When we declare a method as Abstract, we must not provide any concrete implementation for it (i.e) we must no provide definition for the method.

Java Syntax:

public abstract double salaryCalc();

C# Syntax:

public abstract double salaryCalc();

So now in the derived classes these methods can be “overridden” to provide employee-specific definitions/implementations.

Java Syntax:

public double salaryCalc() {

//definition

}

C# Syntax:

public override double salaryCalc() {

//definition

}

(Note: C# requires usage of  “override” keyword. In Java its optional to use “@Override” annotation above the method implementation)

Thus each derived class can provide its own implementation for the derived abstract method but all those methods will have the same signature/header/call syntax. So we provide uniform interface for accessing the methods of the derived class.

Speaking of providing common interface for accessing derived class, we can use a Class variable of the Abstract base class to polymorphically access the overridden methods of the derived classes. This is called “Subtype Polymorphism“. Abstract classes were created to make general base classes but they also serve as a basis for implementing Polymorphism.

But Polymorphism can also be implemented by another technique called Interfaces. Lets see about the Java and C# interface, a relatively new concept…

An Interface is similar to an Abstract Class, but differs in the fact that it provides absolutely no concrete implementation for any of its methods. Some may say an Interface is also a class. But its not. An Interface is not a class. Its like a contract that classes sign. When a class ‘implements’ an interface, it basically signs a contract saying that, “Ill provide definition for all the method declarations declared in the Interface”. Creating an interface is rather simple and similar to creating a class.

Java, C# Syntax:

public interface Employee {

double salaryCalc();

}

So now our employee specific classes can implement this interface and provide concrete definitions for the method salaryCalc(). In C#, syntax for implementing an interface is similar to that of inheriting a base class. But in Java instead of using the “extends” keyword which we use in order derive from a base class, we use the keyword “implements”.

And the classes that implement an interface can provide definitions for the interface methods just like creating a normal method. (i.e) There is no need to use the “override” key word (in case of C#). In Java however, its optional to include the ‘@Implements’ annotation above the method.

In the Abstract Class section, we implemented Subtype Polymorphism by creating  a Class Variable for base class and accessing the derived class instances/objects using that variable. Similar to that we can create an Interface Variable for the Interface and we can use that interface variable to refer the instances of the classes that implement that interface.

So now a question arises. Both abstract class and Interface provide same functionality and help implement polymorphism. So why do we need the new concept called Interfaces. There are two main reasons why we need both Interfaces and Abstract Classes.

The first reason is, In C# and Java, there is no such thing as ‘Multiple Inheritance’. This concept, which was included in languages like C++, caused lots of ambiguity and complexity and hence it was removed in the newer languages like C# and Java. And so a class can ‘extend’ or ‘inherit from’ only one base class. But there are some situations where you need something like multiple inheritance. So in those situations you may use an Interface. A class can implement ‘n number of Interfaces’

Id like to explain the second reason from the Polymorphism perspective. In polymorphism, you basically handle objects of multiple classes using a common Variable. But if you have only Abstract Class concept, only those classes that extend/inherit that abstract class can be handled using the common Variable (Class variable of the Abstract Base Class). But these classes that extend the Abstract Base class almost always come under the same category. (eg.) An employee Abstract Base class is extended by classes that represent some specific employees. An IO Device Base class is extended only by specific IO device classes. So if you have only Abstract Class concept, you can access multiple objects with a common variable only if these objects belong to the same class hierarchy of the Abstract Base class. In a sense you can deal only with related classes.

But if you want to handle instances of Classes which are unrelated with a common variable, then you need to use Interfaces. For eg: A company wants to develop an application that processes payments. But they want the same application to process payments to employees (Salaries) and payment to invoices. So we create two different class hierarchies, one for invoices and one for employees. And we will have to handle instances of these two hierarchies with a common variable. So to do this, we can create an interface that has the common method (say, getPaymentAmount() ) and make the two hierarchies implement this interface. Now we can create a variable for this interface and use it to access the instances of the two unrelated classes.

So use Abstract Class when you want to access instances of classes belonging to same Class hierarchy Polymorphically (using common variable) and use Interfaces when you want to access instances of different and unrelated class hierarchies Polymorphically (using common variable).

So these are the Abstract Class and Interface Concepts. So I hope this article gave you a clear idea about these two powerful concepts.

If you have any doubts, clarifications, suggestions please leave a comment below…..

Steve Robinson.

/* Class hierarchy example take from Paul and Harvey Deitel’s “C# for Programmers” 
UML Diagrams created using creately.com‘s online UML drawing application */