How to launch an Android app from adb - And toggle bluetooth

Have you ever wanted to launch an Android app from the shell, but you were confused as to how to go about it? Me too, but after a bit of searching about I found a couple of posts that explain how to do it. Short short form, you use aapt to find the activity you want to execute, and then you can use the am command (on the device) to launch the program.

As you probably know, Android apps are more or less Java programs. These programs are packed up in JAR files for normal Java or in APKs for Android, both of which are just ZIP archives with some additional fancy files which give them meaning. In the case of Android, you need to understand the AndroidManifest.xml file, but these are binary files which you are unlikely to comprehend when viewed by eye. But you can use the aapt command to list activities within the APK in order to figure out how to launch the program.

aapt is part of the Android SDK. I'm not going to cover how to install that, but you only need the tools and platform tools in order to accomplish this task. On my personal Ubuntu system, the sdk is installed in /opt/android-sdk-linux and aapt is in /opt/android-sdk-linux/platform-tools. If you can't just type "aapt" and get something other than command not found, you can add this (or whatever) directory to your path or you can just type the path. Either way, you want to fetch the APK from your device, perhaps with adb pull, and then do something like this:

aapt dump xmltree jp.ne.neko.freewing.BluetoothOnOff-1.apk \
  AndroidManifest.xml | less

This is a real example; I was trying to figure out how to efficiently toggle bluetooth on Ouya from ADB. The resulting output looks like this:

N: android=http://schemas.android.com/apk/res/android
  E: manifest (line=2)
    A: android:versionCode(0x0101021b)=(type 0x10)0x5
    A: android:versionName(0x0101021c)="1.4" (Raw: "1.4")
    A: android:installLocation(0x010102b7)=(type 0x10)0x2
    A: package="jp.ne.neko.freewing.BluetoothOnOff" (Raw: "jp.ne.neko.freewing.BluetoothOnOff")
    E: application (line=10)
      A: android:label(0x01010001)=@0x7f040000
      A: android:icon(0x01010002)=@0x7f020000
      E: activity (line=11)
        A: android:theme(0x01010000)=@0x1030055
        A: android:label(0x01010001)=@0x7f040000
        A: android:name(0x01010003)="BluetoothOnOff" (Raw: "BluetoothOnOff")
        E: intent-filter (line=15)
          E: action (line=16)
            A: android:name(0x01010003)="android.intent.action.MAIN" (Raw: "android.intent.action.MAIN")
          E: category (line=17)
            A: android:name(0x01010003)="android.intent.category.LAUNCHER" (Raw: "android.intent.category.LAUNCHER")

The output continues on from here, but this is the interesting part. We know that our app's full true name is jp.ne.neko.freewing.BluetoothOnOff and that the "name" of the MAIN intent is BluetoothOnOff. So now we can issue the am command with adb:

adb shell am start jp.ne.neko.freewing.BluetoothOnOff/.BluetoothOnOff

For me, this successfully ran the program and toggled bluetooth.

Add new comment

Default

  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • You may link to images on this site using a special syntax
  • Web page addresses and e-mail addresses turn into links automatically.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Filtered words will be replaced with the filtered version of the word.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <q>

Issue

  • Lines and paragraphs break automatically.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>

Drinking Game

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <p> <br> <pre> <h2> <h3> <h4>
  • Images may be embedded like: [image:node_id align=alignment hspace=n vspace=n border=n size=label width=n height=n nolink=(0|1) class=name style=style-data node=id] Leave off any attributes you don't want.
  • [img_assist|...] tags will be displayed, maybe. Please don't make more of them.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.