adbのCLIでAndroidのエミュレータを起動したりアプリの起動、テストの実行をしたりする
この記事はAndroid Advent Calender その2の15日目の記事です。
今回は
https://developer.android.com/studio/command-line/adb.html https://developer.android.com/studio/run/emulator-commandline.html https://developer.android.com/studio/test/command-line.html
を見て色々素振りしてみようと思います。
シミュレータの起動
/Users/woshidan/Library/Android/sdk/tools/emulator -avd Nexus_5X_API_22
で起動することが可能です。テストを実行させたり、アプリをインストールして起動したりしてみましょう。
アプリのインストール
$ adb install app/build/outputs/apk/app-debug.apk app/build/outputs/apk/app-debug.apk: 1 file pushed. 5.8 MB/s (2910562 bytes in 0.477s) pkg: /data/local/tmp/app-debug.apk Success # 二つ以上シミュレータを起動 or 実機を接続している場合 $ adb devices List of devices attached 162EJP011A71181614 device emulator-5554 device $ adb -s emulator-5554 install app/build/outputs/apk/app-debug.apk app/build/outputs/apk/app-debug.apk: 1 file pushed. 142.6 MB/s (2910562 bytes in 0.019s) pkg: /data/local/tmp/app-debug.apk Success
apkはプロジェクトのbuildディレクトリ以下を探せばあると思います。
アプリのアンインストール
$ adb uninstall package Success
アプリの起動
# アクションがandroid.intent.action.VIEWのIntentに反応するActivityへ暗黙的Intentを送る $ adb shell am start -a android.intent.action.VIEW # 特定のActivityへ明示的Intentを送る $ adb shell am start -n io.test.woshidan/io.test.woshidan.MainActivity
Logcatのログを標準出力に出す
$ adb logcat
操作の様子を録画する
# API19以上の実機かAPI24以上のシミュレータ $ screenrecord /sdcard/Movies/demo.mp4 # adb pull で当該ファイルをPCなどに持ってこれる
テストを実行する
ADBを使う
# テストで実行可能なinstrumantationの一覧 $ adb shell pm list instrumentation instrumentation:com.android.emulator.smoketests/android.support.test.runner.AndroidJUnitRunner (target=com.android.emulator.smoketests) instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest) instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis) instrumentation:com.example.woshidan.myapplication.test/android.support.test.runner.AndroidJUnitRunner (target=com.example.woshidan.myapplication) instrumentation:com.example.woshidan.newtestapplication.test/android.support.test.runner.AndroidJUnitRunner (target=com.example.woshidan.newtestapplication) # プロジェクト全体のAndroidTest $ adb shell am instrument -w com.example.woshidan.myapplication.test/android.support.test.runner.AndroidJUnitRunner # AndroidTestのうち特定のクラスのテストを行う $ adb shell am instrument -w -e class com.example.woshidan.myapplication.test.ExampleTest com.example.woshidan.myapplication.test/android.support.test.runner.AndroidJUnitRunner
gradleを使う
// プロジェクト用のbuild.gradle buildscript { repositories { google() // gradlewでgoogle()リポジトリに探しにいくためにはここに追加する必要あり jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' } }
// アプリ用のbuild.gradle android { compileSdkVersion 26 defaultConfig { applicationId "com.example.woshidan.newtestapplication" minSdkVersion 15 targetSdkVersion 26 buildToolsVersion '26.0.1' versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary= true } ...
# プロジェクトのAndroidTest全体を行う(JUnitを使うUnitテストは connectedAndroid を取る) $ ./gradlew app:connectedAndroidTest # プロジェクトのAndroidTestのうち特定のメソッドのものを行う $ ./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.woshidan.myapplication.MainActivityTest#mainActivityTest
// 出力される実行結果 // build/generated/outputs/connected/以下にある // build/generated/reports/以下にはHTML形式のレポートも <?xml version='1.0' encoding='UTF-8' ?> <testsuite name="com.example.woshidan.myapplication.MainActivityTest" tests="1" failures="0" errors="0" skipped="0" time="0.665" timestamp="2017-12-20T17:11:10" hostname="localhost"> <properties> <property name="device" value="Nexus_5X_API_22(AVD) - 5.1.1" /> <property name="flavor" value="" /> <property name="project" value="app" /> </properties> <testcase name="mainActivityTest" classname="com.example.woshidan.myapplication.MainActivityTest" time="0.563" /> </testsuite>
まとまってませんが色々試して満足したので現場からは以上です。