Wednesday, 21 September 2011

Android for the Java Developer - part 1

In this article, we will look at beginning Android development from a Java programmer's perspective. We will take a look at the similarities and differences between Java and Android, and consider how these may affect how best to develop an Android app. The information may assist if you are, for example:
  • Converting an existing Java program to Android.
  • Developing an application for both Java and Android.
  • Have some experience with Java and want to learn Android.

Android can be thought of as a direct descendant of Java. Not only do the languages share are large common core, but also many existing Java libraries can be used in Android apps directly and without modification or recompilation. This makes Android a relatively easy step for anyone who has a Java background.

Let's take a look at some of the well-known package names from the Java SDK. Those on the left are also included in Android. Those on the right are in Java only.
Packages in Java AND Android ¹ Packages in Java but NOT Android
  • java.beans
  • java.lang
  • java.math
  • java.nio
  • java.sql
  • java.text
  • java.util
  • javax.crypto
  • javax.sql
  • javax.xml
  • java.applet
  • java.awt
  • java.rmi
  • javax.sound
  • javax.swing
¹ Top level packages only, not an exhaustive list.

It's clear that a very large chunk of the JDK is available to the Android developer. In addition, because Java libraries can incorporated into an Android app, it means any existing Java library can be used as long as it only needs packages from the left-hand column.

Turning now to the right-hand column, arguably the most significant "missing" packages are the ones related to user interface building, java.awt and javax.swing. This is because Android has its own system for building UI elements - more on this in another article coming soon.

So what to conclude from this, and how should it affect the design of an Android app we might want to build? Good application design should separate the core program logic from the user interface. If this principle is used, it will also make life easier should you want work with both Java and Android. The program logic parts of the app are typically going to use packages common to both languages, and hence can be easily reused.

A pure Java user interface cannot be reused however, and so an Android-specific version will need to be built. We will look at aspect more in the next article (coming soon).

For those developing an application for both Java and Android, or wanting to migrate an existing Java application to Android, here is a simplified diagram of how an app might be structured.

In the next article, we will explore in more detail the differences between Java and Android, and how we might navigate these to build our Android app.

1 comment: