Time for action – setting up an Android device
Device configuration is dependent on your target OS. To do so:
- Configure your device driver on your OS if applicable:
- If you use Windows, installation of a development device is manufacturer-specific. More information can be found at http://developer.android.com/tools/extras/oem-usb.html with a full list of device manufacturers. If you have a driver CD with your Android device, you can use it. Note that the Android SDK also contains some Windows drivers under
$ANDROID_SDK\extras\google\usb_driver
. Specific instructions are available for Google development phones, Nexus One, and Nexus S at http://developer.android.com/sdk/win-usb.html. - If you use OS X, simply connecting your development device to your Mac should be enough to get it working! Your device should be recognized immediately without installing anything. Mac's ease of use is not a legend.
- If you are a Linux user, connecting your development device to your Distribution (at least on Ubuntu) should be enough to get it working too!
- If you use Windows, installation of a development device is manufacturer-specific. More information can be found at http://developer.android.com/tools/extras/oem-usb.html with a full list of device manufacturers. If you have a driver CD with your Android device, you can use it. Note that the Android SDK also contains some Windows drivers under
- If your mobile device runs Android 4.2 or later, from the application list screen, go to Settings | About phone and tap several times on Build Number at the end of the list. After some efforts, Developer options will magically appear in your application list screen.
On Android 4.1 devices and earlier, Developer options should be visible by default.
- Still on your device, from the application list screen, go to Settings | Developer options and enable Debugging and Stay awake.
- Plug your device into your computer using a data connection cable. Beware! Some cables are charge-only cables and will not work for development! Depending on your device manufacturer, it may appear as a USB disk.
On Android 4.2.2 devices and later, a dialog Allow USB debugging? appears on the phone screen. Select Always allow from this computer to permanently allow debugging and then click on OK.
- Open Command Prompt and execute the following:
adb devices
On Linux, if ????????? appears instead of your device name (which is likely), then
adb
does not have proper access rights. A solution might be to restartadb
as root (at your own risk!):sudo $ANDROID_SDK/platform-tools/adb kill-server sudo $ANDROID_SDK/platform-tools/adb devices
Another solution to find your Vendor ID and Product ID may be needed. Vendor ID is a fixed value for each manufacturer that can be found on the Android developer website at http://developer.android.com/tools/device.html (for example, HTC is
0bb4
). The device's Product ID can be found using the result of thelsusb
command in which we look for the Vendor ID (for example, here 0c87 is HTC Desire product ID):lsusb | grep 0bb4
Then, with root privilege, create a file
/etc/udev/rules.d/51-android.rules
with your Vendor ID and Product ID and change file rights to 644:sudo sh -c 'echo SUBSYSTEM==\"usb\", SYSFS{idVendor}==\"<Your Vendor ID>\", ATTRS{idProduct}=\"<Your Product ID>\", GROUP=\"plugdev\", MODE=\"0666\" > /etc/udev/rules.d/52-android.rules' sudo chmod 644 /etc/udev/rules.d/52-android.rules
Finally, restart the
udev
service andadb
:sudo service udev restart adb kill-server adb devices
- Launch Eclipse and open the DDMS perspective (Window | Open Perspective | Other...). If working properly, your phone should be listed in the Devices view.
Tip
Eclipse is a compound of many views, such as the Package Explorer View, the Debug View, and so on. Usually, most of them are already visible, but sometimes they are not. In that case, open them through the main menu by navigating to Window | Show View | Other…. Views in Eclipse are grouped in Perspectives, which store workspace layout. They can be opened by going to Window | Open Perspective | Other…. Beware that some contextual menus may be available only in some perspectives.
What just happened?
Our Android device has been switched into development mode and connected to our workstation through the Android Debug Bridge daemon. ADB gets started automatically the first time it is called, either from Eclipse or the command line.
We also enabled the Stay awake option to stop automatic screen shutdown when the phone charges, or when developing with it! And, more important than anything, we discovered that HTC means High Tech Computer! Jokes apart, connection process can be tricky on Linux, although little trouble should be encountered nowadays.
Still having trouble with a reluctant Android device? That could mean any of the following:
- ADB is malfunctioning. In that case, restart the ADB deamon or execute it with administrative privilege.
- Your development device is not working properly. In that case, try restarting your device or disabling and re-enabling development mode. If that still does not work, then buy another one or use the emulator.
- Your host system is not properly set up. In that case, check your device manufacturer instructions carefully to make sure the necessary driver is correctly installed. Check hardware properties to see whether it is recognized and turn on USB storage mode (if applicable) to see whether it is properly detected. Please refer to your device documentation.
Tip
When the charge-only mode is activated, SD card files and directories are visible to the Android applications installed on your phone but not to your computer. On the opposite side, when disk drive mode is activated, those are visible only from your computer. Check your connection mode when your application cannot access its resource files on an SD card.
More about ADB
ADB is a multi-facet tool which is used as a mediator between the development environment and devices. It is composed of:
- A background process running on emulators and devices to receive orders or requests from your workstation.
- A background server on your workstation communicating with connected devices and emulators. When listing devices, the ADB server is involved. When debugging, the ADB server is involved. When any communication with a device happens, the ADB server is involved!
- A client running on your workstation and communicating with devices through the ADB server. The ADB client is what we interacted with to list devices.
ADB offers many useful options among which some are in the following table:
Command |
Description |
---|---|
|
To get an exhaustive help with all options and flags available |
|
To print the whole device state |
|
To list all Android devices currently connected including emulators |
|
To install an application package. Append |
|
To terminate the ADB daemon |
|
To transfer a file to your computer |
|
To transfer a file to your device or emulator |
|
To restart an Android device programmatically |
|
To start a shell session on an Android device (more on this in Chapter 2, Starting a Native Android Project) |
|
To launch the ADB daemon |
|
To sleep until a device or emulator is connected to your computer (for example, in a script) |
ADB also provides optional flags to target a specific device when several are connected simultaneously:
|
To target a specific device by its name (device name can be found with adb devices) |
|
To target the current physical device if only one is connected (or an error message is raised) |
|
To target the currently running emulator if only one is connected (or an error message is raised) |
For example, to dump the emulator state when a device is connected at the same time, execute the following command:
adb -e bugreport
This is only an overview of what ADB can do. More information can be found on the Android developer website at http://developer.android.com/tools/help/adb.html.