Skip to content

Instantly share code, notes, and snippets.

@sumio
Last active July 20, 2016 16:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumio/8c08f751c27e827f3463 to your computer and use it in GitHub Desktop.
Save sumio/8c08f751c27e827f3463 to your computer and use it in GitHub Desktop.
uiautomatorのテストをgradleやAndroid Studioからビルド・実行するためのbuild.gradleファイルです。
/*
* Copyright 2014-2015 TOYAMA Sumio <jun.nama@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// uiautomatorのテストをgradleやAndroid Studioからビルド・実行するためのサンプルです。
// android create uitest-project コマンドで build.xml が生成されていることが前提です。
// testClassName変数に、テストスクリプトのクラス名を定義してから使用してください。
// Android Studio 1.0.2 (Mac OS X版)で確認。
//
// タスク一覧:
// uiautomatorClean: クリーン
// uiautomatorBuid: ビルド
// uiautomatorInstall: 端末へのインストール
// uiautomatorTest: テストの実行
//
import org.apache.tools.ant.*
import org.apache.tools.ant.taskdefs.condition.Os
// テストスクリプトのクラス名を定義する
def testClassName = '<<YOUR CLASS NAME>>'
// build.xmlを読み込む
ProjectHelper.configureProject(ant.project, file('build.xml'));
// local.propertiesから<AHDROID_HOME>へのパスを取得する
def sdkDir = ant.properties['sdk.dir']
// build.xmlに定義されたbuild, install, cleanターゲットを、
// それぞれ uiautomatorBuild, uiautomatorInstall, uiautomatorCleanという名前のgraldeタスクとして登録する。
['build', 'install', 'clean'].each { antTargetName ->
task "uiautomator${antTargetName.capitalize()}" << {
ant.project.executeTarget(antTargetName)
}
}
// uiautomatorのテストを実行する
task uiautomatorTest(dependsOn: ['uiautomatorBuild', 'uiautomatorInstall'], type: Exec) {
def adbDir = "${sdkDir}/platform-tools"
def adb = Os.isFamily(Os.FAMILY_WINDOWS) ? 'adb.exe' : 'adb'
def adbDeviceArg = ant.properties['adb.device.arg']
executable(file("${adbDir}/${adb}"))
args(*adbDeviceArg.tokenize(), 'shell', 'uiautomator', 'runtest', "${ant.project.name}.jar", '-c', testClassName)
}
// Android Studioを使わないのであれば、ここから下の記述は不要。
//
// Android Studioでコード補完を有効にするために
// Android SDKのandroid.jarとuiautomator.jarをクラスパスに追加する。
apply plugin: 'java'
def targetDir = ant.properties['target']
dependencies {
compile files("${sdkDir}/platforms/${targetDir}/android.jar")
compile files("${sdkDir}/platforms/${targetDir}/uiautomator.jar")
}
// Android Studioで、src/直下をテストコードと認識させる
sourceSets {
main {
java {
srcDir 'src'
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment