Writing your first app and seeing it running on your phone is only half the fun when it comes to Android. It’s relatively simple to have your app up and live in the Android Market, being used by Android users almost instantly.
Head over to http://market.android.com/publish and using your Google Account, register as a developer in the Android Market. The registration process is immediate and requires a one-off registration fee of $25.
By the end of this article, you’ll be ready to submit an Android app to the Market (although it’s probably best to avoid submitting this HelloWorld-style app that we’re about to create).
Setting Up Your Development Environment
Java developers, especially those using Eclipse, will have an easy transition to the Android development world. Most apps are written in Java (you can drop down to using native C), and it’s typical for development to be done within the Eclipse IDE due to the tooling support provided by Google (although everything can be done outside the IDE from the command line). Android development is supported on the three major operating systems: Windows (XP, Vista, and 7), Mac OS X, and Linux.
In order to set up your environment, you’ll need to download and install the following:
- A Java Development Kit (JDK 5 or JDK 6)—the Java Runtime Environment (JRE) alone is not enough. You can download the specific JDK for your system from http://java.sun.com/javase/downloads/index.jsp.
- Eclipse 3.5 (Galileo), which can be downloaded from http://www.eclipse.org/downloads/. “Classic” is the recommended version.
- Android SDK Tools available from http://developer.android.com/sdk/index.html. Take note where this is installed or unzipped for the next step.
- Android Developer Tools (ADT)—Eclipse Plugin, which needs to be installed from inside Eclipse:
- From the Help menu, click on Install New Software, then select Add.
- In the dialog box, enter ADT Plugin for the Name and https://dl-ssl.google.com/android/eclipse/ in Location; then press OK.
- Select Developer Tools and click Next, and Next again when presented with the items to be installed.
- Read and accept the license terms, and click Finish.
When the installation is complete, restart Eclipse.
Now configure the installed plugin by clicking on the Window menu and selecting Preferences. If you’re on a Mac, select Eclipse -> Preferences.
Select Android, and then for the SDK Location, browse to where you installed the Android SDK Tools, and select that. Click Apply, then OK.
Using the tools now installed, you need to install the SDK components. This is done using the Android SDK and AVD Manager found under the Window menu in Eclipse. Launch the manager and select Available packages. Then select Documentation, SDK Platform Android 2.3, and Samples for SDK API 9; click Install Selected.
Running the Emulator
Now that you have all the tools required to started developing and running Android apps, you need to create a virtual device for your apps to run on in the Android Emulator. An Android Virtual Device (AVD) defines a device profile that the emulator can use when running your apps. You can also create multiple AVDs to test against, which comes in handy when you want to test your app on different-sized screens and various versions of the SDK.
To create an AVD, select Virtual Devices from the Android SDK and AVD Manager, and click New. Give the device a name and select the target SDK from the drop-down. Here, you’re also able to select different screen sizes and emulator storage sizes; for now, we’ll leave everything as the default and click Create AVD.
Now you’re able to run the emulator by selecting the created AVD and clicking Start, and then Launch. After some time, you’ll see the emulator running; you’re now able to run some of the built-in apps.
Creating a Project
Leaving the Emulator running, restart Eclipse so as to create our first Android project. Once Eclipse has finished loading, go to File -> New -> Other -> Android and select Android Project.
Fill in the Project and Application names, and then enter a package name for the app. This package will not only become the top-level Java package for your source files, but also provides a unique identifier for your app. No two apps can have the same package identifier installed on the phone at the same time, and the same goes for apps submitted to the Android Market.
With Create Activity selected, provide a name for the Java class that will become the entry point to your app, and click Finish.
Explore the contents of the generated project. You will find various Java, XML, and configuration files.
This is a good time to explain three key concepts used in Android app development.
Activity (HelloWorldActivity.java)—An Android Activity is a single-focused task that makes up part of your app. The Activity will display a user interface in the supplied Window, and interact with the user to perform the task. A single Activity could be displaying a list of emails or showing a map of the current location.
Typically, multiple Activities together form a complete Android application.
The generated Activity extends from the class android.app.Activity and overrides a single method, onCreate. Activities are driven by events coming from the Android operating system, moving the Activity through different stages of its life cycle. The onCreate method is called when the Activity is being created with the intention of being the current running Activity.
It’s worth becoming familiar with the life cycle of an Activity and the methods that can be overridden as detailed in the Android developer docs.
Manifest (AndroidManifest.xml)—This is the central configuration file required by Android to understand the various pieces of your app and how they fit together. Take a look at the generated Manifest, where you’ll see:
package="com.sitepoint"which forms the unique identifier of the app
<application ... >contains the attributes required for the app icon and name as it appears on the phone
<activity ... >is where the single Activity that was generated is described with attributes for the class name (relative to the app package), and a display label for the Activity.
<intent filter>these XML elements indicate that this Activity is the entry point for your app when it’s launched:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Intents—Android is designed in such a way that apps can call out to other installed apps without having to know the specific details of their interfaces. This is achieved using the concept of Intents and Intent Filters. An Intent is an abstract description of an action (such as VIEW, SEND, EDIT), and typically references some data on which the action is to be performed (for example, a Contact in the address book).
Activities can create Intents as a way of passing responsibility for a task onto other Activities. This can be either within the same app (such as going from an Activity that displays a list of contacts to an Activity that displays the details of the single selected contact) or out to an external app (when, for example, you want to display a PDF using an installed PDF viewer).
Activities advertise their abilities to handle combinations of actions and data types through Intent Filters. Our generated Activity advertised that it’s able to handle a MAIN action when the app is being launched. A PDF viewer may advertise that it can display data of type PDF. If two apps advertise the same ability, Android will prompt the user to select which one they want to use. The Intent is run, and can make the selection the default as an option.
Running and Debugging
Now let’s run the app in the emulator, which you should already have running. Select the project in Eclipse, and from the Run menu, select Run; then select Android Application and OK in the dialog. While the app is loading (or the emulator starts up again if you closed it), you should add some of the Android-specific views to your current Eclipse perspective. You can add them from Window -> Show View -> Other -> Android. I typically add Devices, Emulator Control, and LogCat.
From the Devices view, you can see any emulators or phones you have plugged into your system that are available to debug apps on. You’ll also be able to see the processes currently running along with your own, identified by the package name you gave your app. If you select the emulator in this view and then move to the LogCat view, you’ll see all the logging that the system has been writing.
This view is extremely useful for debugging your apps. It allows you to create filters, so you can switch between seeing different levels of logging (warning, debug, fatal) and different tags. (Typically, an app will output logging with a tag that’s the same as the class name for where the logging is coming from).
Switch back to the emulator, and you should now see the app running as pictured below.
Once you’ve figured out a desirable functionality, writing an app for Android is largely a matter of ensuring you have the right tools installed, and then configuring them to do the hard work for you. After that, you can submit your app to the Market for other Android users to pick up.
Daniel Bradby has worked across the globe on leading edge open source projects such as Hibernate and Subclipse. Now, back home in Australia, he is pursuing his expertise in Android and iOS mobile platforms, as one of three directors at jTribe. With innovative ideas and applications hitting the 'top grossing' section in iTunes, Daniel is working on some leading projects in the mobile industry.