Both Eclipse and Android Studio provide a comprehensive emulator that can be fine-tuned to imitate an exhaustive range of device characteristics and real-world situations (incoming calls, text messages, changes of location, and so forth). However, the emulator has its limitations and cannot give you the same experience as running and debugging your app on a real device. With an Android device connected to your IDE, not only will you be able to experience your app exactly as the end-user will, but as an added bonus loading your app is often much quicker on a real device than on the Android emulator.
Connecting Eclipse or Android Studio to your Android device sounds straightforward, with tracking down and installing the correct drivers the only time-consuming step. However, this process is also notoriously temperamental and sometimes getting your IDE to recognize your device can be surprisingly difficult. In this tutorial, we'll cover the step-by-step, best-case-scenario of connecting Eclipse and Android Studio to your Android device, before providing a checklist of tips and tricks you can work your way through when your IDE refuses to cooperate.
How to Connect Your Device
1. Before we start, you must enable debugging mode on your Android device. Open your device's 'Settings,' select 'Developer options,' and ensure 'USB debugging' is enabled.
Figure 1: USB Debugging checkbox
If you don't see 'Developer options' in your 'Settings' menu, select 'About Phone' and tap 'Build number' seven times until you see a notification that you are now a developer. Return to the main 'Settings' screen and 'Developer options' should have appeared.
Figure 2: About your phone
2. If you're developing on Windows, you'll need to install the USB driver for your device.
|Note: If if you're on Mac or Linux, you shouldn't need to install additional USB drivers and can skip to the next step.|
Google maintains a list of OEM USB Drivers, or if you're working with a Google device (such as the Nexus line), you can download the necessary drivers through the Android SDK Manager.
Open either Eclipse or Android Studio; then, select the 'Android SDK Manager' icon from the toolbar. The Android SDK Manager will open, listing all of the installed packages, along with available updates and additional packages that can be installed. Locate the 'Google USB Driver' package under the 'Extras' folder. If it isn't installed, tick the checkbox and click the 'Install Package' button. This can take some time to download, so be prepared to wait a few minutes.
Figure 3: The Android_SDK_Manager
3. Connect the device to your computer using the appropriate USB cable.
4. In your IDE's menu, open the 'Run' menu and select 'Run...'
Figure 4: The Run menu
5. Select the Android project or module you want to run. After a few moments, the 'Choose Device' window should open.
6. Select 'Choose a running device,' and then select your device from the list. At this point, you may also want to select 'Use the same device for future launches.' Click 'OK.'
Figure 5: The Device Chooser
8. Check your device; your Android project should have made the leap from your IDE onto your smartphone or tablet's screen!
This all sounds very straightforward, but there are countless quirks and hard-to-diagnose problems that can make connecting a device much more difficult than it sounds. In this section, we'll run through a checklist of things to try when your device isn't registering.
1. Check that USB Debugging is enabled. This setting can inadvertently (and perhaps even automatically) change when you plug/unplug your device from your machine, or when you reboot your phone. 'Developer Options' can be also be toggled on and off, which overrides your USB debugging settings, so check this setting, too.
2. Check your device while it's connected to your machine. Is it asking you to choose between different modes? Some phones have a 'charge only' mode that can prevent your IDE from recognizing the device. Your device may also ask you to authorize the connection before it can connect to your computer for the first time.
Figure 6: Choose a connection type
3. adb kill/start. You may be able to force your IDE to recognize your device by stopping and then restarting the adb server process. Open a Terminal (Mac) or Command Prompt (Windows) and change directory ('cd') so it's pointing at your Android SDK folder, specifically the 'platform-tools' folder. Your command should look something like this:
After you've run this command, you can start issuing 'Android Debug Bridge' (adb) commands. Terminate the adb process:
And then restart it:
Check whether your IDE is now recognizing your Android device.
4. Make sure you don't have an Android Virtual Device running in the background.
5. Could there be a problem with the USB cable? If you have a spare cable to hand, plug it in to rule out a fault with the cable itself.
6. Double-check you're running the correct driver. Even if you are, some devices have all the required software pre-installed (for example, HTC Sync) and downloading drivers from another location can muddy the waters. If this might be the case with your device, try uninstalling the driver from your machine and reinstalling the software from your device's memory.
7. Does the driver require supporting software? Samsung Kies on Windows requires .Net Framework 3.5 SP1 or above, for example. This is usually installed along with the driver, but there's no guarantee.
8. A running adb.exe process can interfere with the connection. Close your IDE and then open either Activity Monitor (Mac) or the Task Manager (Windows) by pressing 'CTRL', 'Shift', and 'Esc.' Find the adb.exe process and kill it. Launch your IDE and try again.
9. Check that your 'Project Build Target' is compatible with the Android version on your device. You can check what version of Android your device is running by opening 'Settings' and selecting 'About Phone.' If you need a reminder of your project's build target, you'll find this information in either your project's Manifest (Eclipse) or its module-level build.gradle file (Android Studio).
About the Author
Jessica Thornsby is a technical writer based in Sheffield. She spends her spare time editing the CD reviews section at www.leedsmusicscene.net, contributing to A Short Fanzine About Rocking, and researching her family tree.