If you have not installed the necessary development tools, please check the previous tutorial in "Getting Started with Android Development". We are now ready to start going through the basic steps of creating new projects or importing exisiting ones into the development environment. The goal of this tutorial is to eventually watch your example in action on either the emulators or physical hardware devices. Throughout the tutorial, we will pick up essential knowledge of configuring emulators, i.e. Android Virtual Devices (AVD), establishing the connection with mobile devices, and introducing basic debugging functionalities. Once this basics are covered, you should be able to build more complex and advanced projects on your own.
Setting Up Android Virtual Devices (AVD)
An Android Virtual Device (AVD) is an emulator configuration allowing you to simulate a physical device by specifying hardware and software options to be used by the Android Emulator. One particular advantage of setting up an emulator is the capability of testing out the most recent Android features that might not even exist in any current mobile devices out in the market yet.
Before you can run Android apps on emulators, you will need to set up a virtual device through Android Virtual Device Manager as we mentioned in the previous tutorial's final screenshot. Then you will be presented with a configuration dialog as in Figure 1.
Figure 1: AVD Configuration
You can customize a name, a platform target, an SD card size, a device skin, and even specific hardware features (e.g. camera, battery, etc). Detailed hardware options are available at http://developer.android.com/tools/devices/managing-avds.html#hardwareopts One thing to note about "Emulation Options" is when the "Snapshot" option is enabled, the next time the device will start faster since AVD saves its state snapshot when it is closed. The other option "Use Host GPU" will improve the emulator rendering performance using your host computer's graphics card. At the moment, both options cannot be enabled at the same time. Figure 2 is the resulting emulator of our customized AVD setup. Instructions about how to make use of the advanced emulator features can be found at http://developer.android.com/tools/devices/emulator.html
Figure 2: Emulator
Connecting with Hardware Devices
Android runs on numerous devices with various platform versions by different hardware manufacturers. It is widely used for phones, tablets, smart watches, glasses, home appliances, and a long list of amazing mobile devices. It definitely comes in handy if you have any Android-powered devices and are thinking of developing or customizing apps on them.
Connecting with your Android devices, in general, should be very straightforward and effortless. Nevertheless, in some cases, you still might run into driver or incompatibility issues between an Android device and your computer system. As Android becomes more mature, these issues become less and less common. Most of the devices will simply show up during the harware detection automatically when they connect with your computer through USB, so you should not need to do anything other than plugging in the USB cable. If driver issues do come up, some recommendations are at: http://developer.android.com/tools/extras/oem-usb.html and http://developer.android.com/tools/device.html#VendorIds.
Since it's our intention to develop Android software, it is recommended you verify a couple of things as follows:
- Go to your device's "Setting" app and find "Developer Options". Enable the "USB Debugging" option so that you can monitor the debugging info from your device.
- Make sure your device is visible from the computer while connected. There are two ways of doing this.
(1) If you do it through the command line, go to the folder where you install the Android SDK. There is a platform-tools subfolder with an executable file named adb.exe. Issue a command adb devices and you should see a list of available Android devices. In Figure 3, I have a Nexus 5 phone and a Nexus 7 tablet connected with my laptop computer, so there are two device serial numbers listed.
(2) If you check the device availability through Eclipse, you can find it under "Run" and you will be prompted with the "Android Device Chooser" dialog to select the device you want the app to be installed onto. You should see the device in this list. In Figure 4, again the two devices mentioned before appear here.
Figure 3: ADB
Figure 4: Android Device Chooser
Creating a New Project
To start a new project, go to "File" menu and select "New" and "Android Application Project". You will see the dialog where you can set up the project info and specific SDK configurations as in Figure 5. "Application Name" is the name that will appear right below the application icon. "Package Name" conventionally contains developer's organization info as well as folder name for the entire app all in lower case. Click "Next" to step through wizard dialogs with default values and create a blank "Hello World" project as in Figure 6. The default is running on the emulator we created earlier in Figure 7.
Figure 5: New Android Application
Figure 6: Default App
Figure 7: App Running on Emulator
Basic Debugging Options
Locate the "Devices" tab on Eclipse and click to select the emulator created before. This is needed when you have multiple emulators or devices available. Switch to the "Logcat" tab and you should see our app package name "com.developer.firstapp" displayed on the right pane. This utility basically will display all the system logs, messages, and errors about all the running apps. You can set up filters on the left pane to only display info regarding your app. In many cases, potential errors can be found just from "logcat". Figure 8 illustrates our example.
To actually debug the app, you can doubleclick on the leftmost column for the specific line to set up the debugging break point. To start the process, you can either use the "Run" menu or click on the bug icon. A new tool bar will appear for debugging purpose.
Figure 8: Logcat
Importing an Existing Project
Often, it will save lots of effort to start a new project by modifying and reusing some bits and pieces from existing ones. To import an existing project, you can go to "File" menu and then "Import". Select "Existing Android Code Into Workspace" from under "Android". Browse to the package folder and usually you should be all set at this point. It occasionally fails on importing some older versions of Android projects, so if the above does not work, you can try to go to "File" menu and choose "New" and then "Project" instead. Under "Android" folder, select "Android Project from Existing Code" and browse to the package folder.
In this tutorial, we first learned how to configure the Android emulators and connect with hardware devices. Then we went through the process of creating and importing a project. Finally we put the app into action and covered the basic debugging utilities commonly used for Android development. Before finishing up this tutorial, I'd like to share some Eclipsekey shortcuts that should make your life easier while editing the code.
- Ctrl+aselects all the text of the current document
- Ctrl+iauto-indents the selected code in the right format
- Ctrl+ltakes you to a specific line of code
- Ctrl+Shift+o organizes the import packages automatically
- Android Portal at: http://www.android.com
- Google Play Store at: https://play.google.com
- Androidlet at http://www.androidlet.com
About the Author
Chunyen Liu has been a software professional for years. He was among winners at programming contests. He has co-authored software patents, written 25+ articles, reviewed books, and published apps at Androidlet and The J Maker. He holds advanced degrees in Computer Science with 20+ graduate-level courses. On the non-technical side, he is a tournament-ranked table tennis player, certified umpire, and certified coach of USA Table Tennis.