@@ -2,6 +2,32 @@ def isWinOs = System.properties['os.name'].toLowerCase().contains('windows')
22def runOnDevice = project. hasProperty(" runOnDevice" )
33def runOnDeviceOrEmulator = runOnDevice ? " -d" : " -e"
44
5+ // One device for the whole run. Without ANDROID_SERIAL, :app:installDebug installs on every
6+ // connected device, overwriting whatever build of the test app the others are running.
7+ def targetSerial = null
8+ def resolveTargetSerial = { ->
9+ if (targetSerial != null ) {
10+ return targetSerial
11+ }
12+ def fromEnv = System . getenv(" ANDROID_SERIAL" )
13+ if (fromEnv) {
14+ targetSerial = fromEnv
15+ return targetSerial
16+ }
17+ def proc = [" adb" , runOnDeviceOrEmulator, " get-serialno" ]. execute()
18+ def stdout = new StringBuilder ()
19+ def stderr = new StringBuilder ()
20+ proc. waitForProcessOutput(stdout, stderr)
21+ def serial = stdout. toString(). trim()
22+ if (proc. exitValue() != 0 || ! serial) {
23+ throw new GradleException (" Could not pick a single ${ runOnDevice ? 'device' : 'emulator'} " +
24+ " (adb ${ runOnDeviceOrEmulator} : ${ stderr.toString().trim()} ). Set ANDROID_SERIAL to choose one." )
25+ }
26+ targetSerial = serial
27+ return targetSerial
28+ }
29+ def adbTarget = { -> [" -s" , resolveTargetSerial()] }
30+
531def onlyX86 = project. hasProperty(" onlyX86" )
632def useCCache = project. hasProperty(" useCCache" )
733def selectedAbis = project. hasProperty(" abis" ) ? project. property(" abis" ) : null
@@ -16,10 +42,12 @@ task waitForEmulatorToStart(type: Exec) {
1642 doFirst {
1743 println " Waiting for emulator to start..."
1844
45+ def envSerial = System . getenv(" ANDROID_SERIAL" )
46+ def selector = envSerial ? [" -s" , envSerial] : [runOnDeviceOrEmulator]
1947 if (isWinOs) {
20- commandLine " cmd" , " /c" , " node" , " $rootDir \\ tools\\ make_sure_emulator_is_ready.js" , runOnDeviceOrEmulator
48+ commandLine([ " cmd" , " /c" , " node" , " $rootDir \\ tools\\ make_sure_emulator_is_ready.js" ] + selector)
2149 } else {
22- commandLine " node" , " $rootDir /tools/make_sure_emulator_is_ready.js" , runOnDeviceOrEmulator
50+ commandLine([ " node" , " $rootDir /tools/make_sure_emulator_is_ready.js" ] + selector)
2351 }
2452 }
2553}
@@ -53,32 +81,20 @@ def getBuildArguments = { ->
5381task installApk (type : Exec ) {
5482 doFirst {
5583 println " Attempting to install buit apk"
84+ environment " ANDROID_SERIAL" , resolveTargetSerial()
5685 commandLine getBuildArguments()
5786 }
5887}
5988
60- task runAdbAsRoot (type : Exec ) {
61- doFirst {
62- println " Running adb as root"
63-
64- if (isWinOs) {
65- commandLine " cmd" , " /c" , " adb" , " -e" , " root"
66- } else {
67- commandLine " adb" , " -e" , " root"
68- }
69- sleep(10 * 1000 ) // wait 10s to make sure adb properly restarts, otherwise error: closed might happen on next command
70- }
71- }
72-
7389task deletePreviousResultXml (type : Exec ) {
7490 ignoreExitValue = true
7591 doFirst {
7692 println " Removing previous android_unit_test_results.xml"
7793
7894 if (isWinOs) {
79- commandLine " cmd" , " /c" , " adb" , runOnDeviceOrEmulator, " -e " , " shell" , " run-as" , " com.tns.testapplication" , " rm" , " -f" , " android_unit_test_results.xml"
95+ commandLine([ " cmd" , " /c" , " adb" ] + adbTarget() + [ " shell" , " run-as" , " com.tns.testapplication" , " rm" , " -f" , " android_unit_test_results.xml" ])
8096 } else {
81- commandLine " adb" , runOnDeviceOrEmulator, " -e " , " shell" , " run-as" , " com.tns.testapplication" , " rm" , " -f" , " android_unit_test_results.xml"
97+ commandLine([ " adb" ] + adbTarget() + [ " shell" , " run-as" , " com.tns.testapplication" , " rm" , " -f" , " android_unit_test_results.xml" ])
8298 }
8399 }
84100}
@@ -88,9 +104,9 @@ task startInstalledApk(type: Exec) {
88104 println " Starting test application"
89105
90106 if (isWinOs) {
91- commandLine " cmd" , " /c" , " adb" , runOnDeviceOrEmulator, " -e " , " shell" , " am" , " start" , " -n" , " com.tns.testapplication/com.tns.NativeScriptActivity" , " -a" , " android.intent.action.MAIN" , " -c" , " android.intent.category.LAUNCHER"
107+ commandLine([ " cmd" , " /c" , " adb" ] + adbTarget() + [ " shell" , " am" , " start" , " -n" , " com.tns.testapplication/com.tns.NativeScriptActivity" , " -a" , " android.intent.action.MAIN" , " -c" , " android.intent.category.LAUNCHER" ])
92108 } else {
93- commandLine " adb" , runOnDeviceOrEmulator, " -e " , " shell" , " am" , " start" , " -n" , " com.tns.testapplication/com.tns.NativeScriptActivity" , " -a" , " android.intent.action.MAIN" , " -c" , " android.intent.category.LAUNCHER"
109+ commandLine([ " adb" ] + adbTarget() + [ " shell" , " am" , " start" , " -n" , " com.tns.testapplication/com.tns.NativeScriptActivity" , " -a" , " android.intent.action.MAIN" , " -c" , " android.intent.category.LAUNCHER" ])
94110 }
95111 }
96112}
@@ -107,9 +123,9 @@ task waitForUnitTestResultFile(type: Exec) {
107123 println " Waiting for tests to finish..."
108124
109125 if (isWinOs) {
110- commandLine " cmd" , " /c" , " node" , " $rootDir \\ tools\\ try_to_find_test_result_file.js" , runOnDeviceOrEmulator
126+ commandLine([ " cmd" , " /c" , " node" , " $rootDir \\ tools\\ try_to_find_test_result_file.js" ] + adbTarget())
111127 } else {
112- commandLine " node" , " $rootDir /tools/try_to_find_test_result_file.js" , runOnDeviceOrEmulator
128+ commandLine([ " node" , " $rootDir /tools/try_to_find_test_result_file.js" ] + adbTarget())
113129 }
114130 }
115131}
@@ -138,10 +154,10 @@ task runtests {
138154}
139155
140156// waitForEmulatorToStart.dependsOn(deleteDist)
141- runAdbAsRoot . dependsOn(waitForEmulatorToStart)
142- deletePreviousResultXml . dependsOn(runAdbAsRoot)
143- installApk . dependsOn(deletePreviousResultXml )
144- startInstalledApk. dependsOn(installApk )
157+ installApk . dependsOn(waitForEmulatorToStart)
158+ // Runs after the install: run-as only works once the debuggable build is on the device.
159+ deletePreviousResultXml . dependsOn(installApk )
160+ startInstalledApk. dependsOn(deletePreviousResultXml )
145161createDistFolder. dependsOn(startInstalledApk)
146162waitForUnitTestResultFile. dependsOn(createDistFolder)
147163copyResultToDist. dependsOn(waitForUnitTestResultFile)
0 commit comments