Android App Development For Dummies
Book image
Explore Book Buy On Amazon

You're an up-and-coming Android app developer. You've installed Android Studio and experimented with a few apps. You've used many of the features available in Android Studio.

You like Android Studio's interface. But you realize that, once in a while, Android Studio isn't enough. Sometimes you need to take the bull by the horns. Instead of poking menus and picking options, you need to communicate directly with your emulator or your physical device. Yes, you start off in Android Studio. But you also open a command window and type commands. Your commands tell the device exactly what to do. Here's how it works:

In Android Studio, look for the Terminal tool button in the lower left part of the main window. When you click that button, a new panel appears. This panel contain your development computer's command window — the MS-DOS command prompt on a PC, or the Terminal app on a Mac.

The adb tool (the featured celebrity in this article) lives in a directory called platform-tools. So the next thing to do is to find your platform-tools directory. (When you installed Android Studio, the installation routine created an Android SDK directory. You can find the SDK directory by choosing File→Project Structure→SDK Location in Android Studio. Inside that SDK directory lies the platform-tools directory.)

In the Terminal panel, type cd followed by the full path name of the platform-tools directory. For example, if you have a directory named UsersMyNameAppDataLocalAndroidsdk, type

cd UsersMyNameAppDataLocalAndroidsdkplatform-tools

and then press Enter.

Then, with your emulator running, or your physical device attached via USB, type

adb devices

With an emulator running, the adb program responds with something like

<span class="code">List of devices attached </span>
<span class="code">emulator-5554         device</span>

For a physical device, the second line looks something like this:

<span class="code">875A8B201AF74D01      device</span>

Either way, the word device tells you that something Android-ish is running.

Now imagine that you want to copy a file (call it myfile) from your development computer to the device's SD card. Type the following adb command:

adb push myfile sdcard/myfile

To copy the same file from the device to your development computer, type

adb pull sdcard/myfile

To install an app onto the Android device, find the name of the app's .apk file. Type

adb install myapp.apk

To see the logcat output of your device, type

adb logcat

The resulting logcat display isn't readily scrollable like the display in Android Studio's Logcat panel, but this display will do in a pinch.

Android is a version of Linux. So, like all devices running Linux, your emulator or physical device has its own command prompt. (Linux aficionados call this the Linux shell.) To run the shell from your development computer, type

adb shell

When you do, you can type many of the regular Linux shell commands. Here are a few of them:

  • pwd: Display the name of the current working directory.

  • cd: Change the current working directory.

  • ls: List the files in this directory.

  • cp: Copy a file.

  • mv: Move or rename a file.

  • rm: Delete a file.

  • mkdir: Make a new directory.

  • am: Perform activity manager tasks (e.g., start an activity, start a service, stop a process, or broadcast something).

  • pm: Perform package manager tasks (e.g., list installed packages or uninstall an app).

For example, using the following commands, you can uninstall an app:

C:>adb shell
# cd data
# cd app
# rm com.allmycode.menus.apk
# exit

(In this particular example, the app lives in a file named com.allmycode.menus.apk.)

If you have more than one device running, you can direct your adb command to a particular device. For example, to install myfile.apk on an emulator, you might type

adb -s emulator-5554 myfile.apk

To install the same file on a physical device, you might type

adb -s 875A8B201AF74D01 myfile.apk

Sometimes, the connection between your development computer and any running Android devices becomes garbled. Sometimes, you want to remove a USB-connected physical device, but your development computer says that removal isn't safe. At times like these, you can break your development computer's connection to the Android devices and start all over again. To do so, issue the following two commands:

adb kill-server
adb start-server

Between the killing and the restarting, you can disconnect a physical device or do whatever else you want to do in order to prepare for more Android fun.

About This Article

This article can be found in the category: