Part I
Building a Better Foundation
Chapter 1 Fine-Tuning Your Development Environment
Chapter 2 Efficient Java Code for Android
Chapter 1
Fine-Tuning Your Development Environment
Depending on what youâre developing, you have different choices when it comes to the tools you can use. Your requirements on the development environment differs if youâre writing an HTML5 application or if youâre developing a server-side application in Java. Some platforms offer more choice than others, and as I describe next, developing Android applications gives you a lot of choice for your development environment.
I begin this chapter with some more advanced internals of the Android SDK and how you can use them in your daily development and how they can assist you in improving the quality of your application. I continue by describing how to structure your code projects in an optimal way for reuse by using library projects. You also learn how to take version control to a new level by integrating Git with a code-review tool called Gerrit. As a developer, you will spend most of your time using the IDE tool. While the Eclipse IDE is still supported by Google, they are now pushing developers to use their new Android Studio IDE for all Android projects. So, I give an introduction to Android Studio as well as to the new build system called Gradle. Finally, I go through the developer settings that are available on Android devices.
Operating Systems for Android Development
This is probably the one topic you donât have to worry about. Either you can pick the operating system on your computer used for development, or it is limited by the IT-policies of your employer. For most Android developers, any of the officially supported operating systems works fine. However, there are situations where the choice will matter.
Google supports Windows, Linux, and OS X for developing Android applications. Although Windows is officially supported by the Android SDK, youâll have problems if you decide to do advanced development, especially when it comes to writing native applications or building your own custom ROM. The best choice is either Linux or OS X. If possible, try to have one of these as your primary operating system, and youâll run into far fewer problems. Another reason for avoiding Windows on your Android development environment is that you wonât need to install new USB drivers for every Android device you work on.
Advanced Android SDK Tools
After you have your operating system and the required (and recommended) tools installed on your computer, you can focus on the Android SDK. You will find the download for your operating system and the latest installation instructions at http://developer.android.com/sdk. Android Studio comes with an SDK bundle that is completely managed from within the IDE, but if you prefer to have a standalone version you can download that as well.
Make sure that you always keep the SDK up to date and that you download the APIs for all the Android versions that youâre developing for. The easiest way to update your standalone Android SDK is to run the update tool from the command prompt:
$ android update sdk --no-ui
Inside the Android SDK folder, you will find a number of subfolders. From a tools perspective, only the platform-tools and tools folders are of interest for now. I will introduce some of these tools and explain how to use them, starting with the adb (Android Debug Bridge) tool. If you are frequently using the command line tools in the Android SDK I recommend that you add the path to these folders in your local PATH variable.
You can find the official documentation for most of the tools in the Android SDK at http://developer.android.com/tools/help/index.html.
The adb Tool
In the platform-tools folder, you will find the adb tool that is used to communicate with your device to install and start apps during development. In earlier versions of the Android SDK, this tool was found in the tools directory, but it has since been moved. Besides being used for installing, starting, and debugging applications from your IDE, the adb tool enables you to manually call many of the low-level operations on your Android device for debugging purposes. To list all the commands available, simply type adb help all in your terminal (Linux or Mac OS X) or command prompt (Windows).
Some common adb commands are
â adb devicesâList all connected Android devices and Emulators.
â adb push <local> <remote>âCopy a file from your computer to a device (usually on the SD card).
â adb pull <remote> <local>âCopy a file from the device to your computer.
adb and Multiple Devices
If you ever need to develop and debug an application for two or more devices simultaneously, such as for a multiplayer game or an instant-message application, adb needs an additional argument to know which device you want to address. You do so by adding âs <serial number> as the first parameter to adb. You can see the serial number for your connected devices by executing the adb devices command. To run the logcat command on a specific device, run the following:
$ adb devices
List of devices attached
0070015947d30e4b device
015d2856b8300a10 device
$ adb âs 015d2856b8300a10 logcat
When you have multiple devices connected or emulators started a dialog box will appear when you launch your application in your IDE.
Mastering Logcat Filtering
Logging is an important part of Android application development. Although using breakpoints and a debugger from your IDE is extremely powerful when you want to follow the execution flow and inspect the values and state of different variables, simply reading the output from logcat can sometimes be more efficient. Android logging is handled by the logcat function, which is usually integrated in your IDE but can also be called via the adb command.
Because Android outputs all system and application log messages to the same stream, it can become quite complicated to find the messages that relate to your ...