Android is a great platform. The development tools are free, all the documentation is online, and you don’t need to purchase a special developer phone or register your hardware as a development device. In fact, thanks to the very slick and functional emulator that comes with the Android SDK, you don’t even have to own an Android-powered device (not that I recommend releasing an app without running it on real hardware).
There are more guides on the Internet for setting up an Android development environment than your average java-script program has semicolons. And not only are there just as many tutorials for writing an Android version of the revered “Hello World!” program, but there is actually a wizard included with the Android Eclipse plug-in that will generate it for you.
Yet despite the wide availability of information and a low-to-no startup cost, I still hear complaints from new developers and experienced developers transitioning to Android from another platform that it’s overwhelming to know where to start. Setting up a development environment and writing a “Hello World” app is vastly different from understanding Android conceptually, and the unique approach required to writing an app that does something useful. I get asked all the time what an Android project looks like, how the layout manager works, and what are the overriding design best practices for implementing Android applications.
My goal with this guide is to answer some of the common questions, and also open a dialogue with TechRepublic readers. I’d like to hear lingering questions of developers new to Android, as well as insights from experienced Android developers. Perhaps collectively we can make the road to creating that first killer app a little smoother.
What is Android?
According to Google’s official documentation, Android is not just an operating system, but rather it is a software stack. Part of that stack is an operating system (a forked Linux kernel to be exact), but Android is also a set of middleware, core applications, and an SDK that ties it all together. Android provides all the required components needed to run the device, a base level of functionality, and a highly optimized virtual machine for running a variant of Java-byte code. It is in this Java-based layer that third-party application developers are given the tools and the necessary ecosystem to extend the functionality and usefulness of Android-powered consumer devices via the wonderful world of apps.
What tools do I need?
Unlike many other development environments, you don’t need to purchase expensive hardware and software to start writing your first Android app. Android development can be done on a Mac, a Windows PC, and just about any flavor of Linux. You need Eclipse, the Android SDK, and the ADT Plugin. All of these tools are free.
If you have an Android-powered tablet, the Allow Usb Debugging setting in the configuration menu will enable you to connect the device to your development environment, download code to it, and invoke the debugger. If you don’t yet have an Android-powered device, you can use the emulator that comes with the SDK.
How do I set up my development environment?
For my money, the installation guide put together by the folks at Google is probably the most comprehensive and actively updated on the net. There you can find documentation for installing everything you need, for whichever host operating system will house your development environment.
What does an Android project look like?
When you create a new Android project using the ADT plugin, a development scaffolding is created for you. This structure helps to keep your layout cleanly separated from your source code, and it makes it easy to support things like internationalization out of the box. Below is a typical Android project hierarchy with a brief explanation of each node.
- src- : This is the folder where our Java classes reside.
- gen- : This folder contains folders that get built for us automatically by the toolset. A good example of what you will find in here is the Android-specific file R.java. This file is updated by the system whenever we add a new id to one of our layout resources.
- Android x.x- : This folder is the reference to the SDK version our app is using. This is something we get to configure when we create the project.
- assets- : The assets folder provides a repository for raw resources. For example, if your app plays sound effects, placing the wave files in the asset folder makes those resources available to the application at runtime. You might think it would be a good idea to include your bitmap images in this folder as well, but you will learn in a moment that Android provides a specific set of folders for graphic resources, and using those folders hold certain advantages.
- bin- :This is where Eclipse will place your compiled binaries.
- res- :The res folder is very important, but it doesn’t contain any files directly. The res folder contains a series of subfolders.
- res/drawable: There is a series of drawable folders. The folder names represent the screen density (as an example res/drawable-hdpi contains all your image resources for high-resolution devices). If you size your graphic resources correctly and include them in the right folder, the operating system will pick out the best possible image at runtime, depending on the size and density of the user’s display.
- res/layout: The layout folder is where you use Android’s XML meta-language to describe how your screens should be displayed. Like the drawable folder, the layout folder can be used to represent multiple resources, in this case portrait and landscape layouts, allowing the operating to choose the most appropriate option at runtime.
- res/values: The values folder is used largely for string resources. The contents consist of key-value pairs, and the folders can be modified (e.g., values-ENG) to support various language prompts at runtime.
- AndroidManifest.xml: The manifest file is responsible for providing essential information to the operating system about a particular application. This information includes things like the package name, the minimum SDK needed, screen sizes supported, security permissions required by the app, and versioning to name a few.
How do I write an Android program?
Writing an Android app, and writing a good Android app, are two different beasts entirely. You can follow Google’s “Hello World!” tutorial and have something up and running in a matter of minutes; however, I highly recommend you first read the Application Fundamentals and start to get a feel for things like activities, services, content providers, and broadcast receivers.
What are some best practices for Android development?
There are “good” programming practices, which are general computer science fundamentals that span language and device barriers. Android is programmed using a modern object-oriented development language, and as such, you can and should apply accepted programming methodologies and patterns. Additionally, Android has some parameters unique unto itself that should be kept in mind when writing an Android app.
Reto Meier did a great presentation on the do’s and dont’s of Android development at Google IO 2010; you can still watch the 54-minute video of Meier’s presentation. I’ll touch on a couple of the big points:
Performance and responsiveness are paramount.
- If you don’t respond within five seconds to user input, the OS will ANR you. (ANR means application not responding — it’s a nasty dialog presented to the user giving them an option to force close your non-responsive app).
- Users can and will notice lags of more than 100ms.
- There is only one UI thread so DO NOT BLOCK IT! Do your long-running I/O on a background thread.
Be aware of the device’s limited resources.
- Don’t unnecessarily poll hardware like the accelerometer or GPS, as this will run down the battery fast.
- Use wakelocks sparingly. (A wakelock is a mechanism that forces the device to do something despite the battery manager’s recommendation to put the device to sleep. An example is using a wakelock to keep the screen brightness turned all the way up.)
- Unlike iOS, Android phones come in all different shapes, sizes, and even running very different versions of Android. It’s your responsibility to avoid hardcoding things like screen resolution and make the extra effort to utilize built-in mechanisms for making apps as compatible with as many devices as possible.
It’s impossible to include everything you’ll ever need to know about Android development in a single post, especially since Android and the mobile space in general is a rapidly changing ecosystem. I hope this post serves as a worthwhile starting point for anyone considering Android development. If you have questions, I hope you will take a moment and post them in the discussion. If I don’t know the answer, I bet another reader does.