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:

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.

Summing Up

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.

And if you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like Advanced Android Development.

Comments on this article are closed. Have a question about Android Development? Why not ask it on our forums?

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.

GuitarMantra

Trying to learn Android APps. Hope this tutorial is best to start with. Thanks

paldss

I would LOVE to see mobile developing in one of Sitepoint’s awesome online classes.

Polaris431

After writing your first app, upload it to Aproov (www.aproov.com). This will allow you to not only publish your app but test it out by downloading it without requiring the Google Android Market app because Aproov uses the mobile device’s browser for downloading and installing the app. Remember, there are many devices out there without the Google Market app which makes it impossible for many users to use your app.

Usman

Thanks. It will help many Android developers.

http://www.cemerson.co.uk Stormrider

Shame Eclipse is so awful and they don’t exactly make it easy to use an alternative from what I’ve found :/

ULTiMATE

Agreed! I’ve spent the last three years writing Java applications and it sucks that Eclipse is the norm when I’ve found NetBeans to be so much better to use. It’s a shame that there has been no effort to provide the same level of functionality in a NetBeans Android kit.

There is a kit out there, but the Eclipse kit has backing from Google, whereas the NetBeans version is far from a finished product.

http://www.onsman.com Ricky Onsman

Interested to hear that. I hadn’t used Eclipse before working through this tutorial, but I found it pretty easy to work with.

http://www.trackmyspace.net splitzer

I would have to agree with Ricky. I do Java development as well, didnt had any major issues with developing in eclipse. It’s fairly a solid platform since it is free.

James

Very interesting. Would love to see a tutorial on building apps for Windows Phone 7 as well!

mezmo

Actually the whole registration and paying 25 dollars part is unnecessary until such time as you want to distribute your app in the Marketplace. If you’re just doing it for your own education, or plan on distributing from your own web site, which Android is one of the few platforms that will allow that, then you never have to pay Google anything, nor do you have to register anything.

http://www.onsman.com Ricky Onsman

That’s true, Mezmo. The paying and registering is only necessary if you want to share / sell to others. Then again, if you’ve worked out a whizz bang app, it might be worth investing a bit of cash to make some more.

Sphamandla

Learned A thing or two about android app development they should have more classes on this in future #sitepointsuggestion

Vivek Parmar @WebGuide4U

Thanks a lot for sharing this, i have just installed Android SDK on my PC and now i’m searching how to get started with it.

http://dish2u.com/ Dish2u

Thanks for the comprehensive article Daniel. I just recently got into Android mobile app development and I’ve been toying around with 2 development models, PhoneGap and Titanium Appcelerator. Still trying to figure out how to set a banner into my apps so I can publish them as free instead of charging for them. I like how PhoneGap works with HTML (which I’m very familiar with), but I’m having a heck of a time trying to get it to parse XML feeds that my apps are built on.

newbie

I found the article good.It is explained in a clear way and taking the reader into consideration . Hope you would keep writing these kind of articles with the same narration style.