8
8

More than 5 years have passed since last update.

ホストマシンからAPK指定でAndroidスマホアプリを起動する♥

Last updated at Posted at 2014-02-06

<前提>
Androidスマホ実機は有線か何かでホスト側と接続中。adb devices したらIDが表示される状態。
指定するAPKは既にスマホ実機側にインストール済み。

さて、ツールとしては aapt、awk、adb shell を使う。

  • aapt : 指定したAPKのパッケージ名とかアクティビティ名などのアプリ情報を表示する
  • awk : aaptが出したアプリ情報を整形する。ここではパッケージとアクティビティの名前を抽出。
  • adb shell: awkが整形したアプリ情報を受け取りスマホ実機でアプリ起動

次のような感じ。長いので改行した。

aapt d badging <APKファイルのパス> | \
awk -F'[ =\047]' '
 /^package/{
  pk=$4
 } 
 /^launchable/{
  system("adb shell am start -a android.intent.action.MAIN -n " pk "/" $4);exit
 }'

多分もっと良い方法があるのでは? とりあえず使えるからいいや。

以上

※ 追記:2014-02-22
Android OS 4.3 ではadb shell am startに失敗するかも?
package.listのパーミッションが変わっていてパッケージ起動ができないケースがあるとか。未確認。
ちなみに、package.listが読めないとrun-asで失敗する。ndk-gdbはrun-asを使うので、つまるところ4.3ではネイティブデバッグが出来なかったりするかも?
<参考URL:4.3のpackage.list問題について>
 http://stackoverflow.com/questions/18032234/run-as-says-package-is-unknown-after-upgrading-android-to-4-3
 http://code.google.com/p/android/issues/detail?id=58373
 http://stackoverflow.com/questions/17219906/run-as-package-a-b-c-is-unknown-galaxy-s4-jellybean-or-android-4-3

※ 追記:2015-07-04
awkを使わずActivity名をとってみる。かんたん。

aapt d badging <APKファイルのパス> | grep launchable | cut -d"'" -f 2

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8