// Variables: // ext.screenshotsPackageName => the package name to pull from // ext.screenshotsDirectory => the directory on the device to pull from inside the Pictures folder // ext.screenshotsSavePath => where to pull the images // ext.adbPath => Give adb path, if the script cant find it by itself from localProperties def propertyOrNull = { key -> if (extensions.extraProperties.has(key)) { return extensions.extraProperties.get(key) } else { return null } } def findAdbFromLocal = { def localProperties = new File(rootDir, "local.properties") if (localProperties.exists()) { Properties properties = new Properties() localProperties.withInputStream { instr -> properties.load(instr) } def sdkDir = properties.getProperty('sdk.dir') return "$sdkDir/platform-tools/adb" } else { println("WARNING: SDK dir not found by local properties, returning static: $System.env.HOME/Library/Android/sdk/platform-tools/adb") return "$System.env.HOME/Library/Android/sdk/platform-tools/adb" } } def packageName = propertyOrNull("screenshotsPackageName") ?: "$android.defaultConfig.applicationId" def screenshotDirectory = propertyOrNull("screenshotsDirectory") ?: "test-screenshots" def savePath = propertyOrNull("screenshotsSavePath") ?: "build/testscreenshots/" def adb = propertyOrNull("adbPath") ?: findAdbFromLocal() def contextInternalFullPath = "/data/data/$packageName/files/$screenshotDirectory/" def contextExternalFullPath = "/sdcard/Android/data/$packageName/files/Pictures/$screenshotDirectory/" def environmentExternalFullPath = "/sdcard/Pictures/$packageName/$screenshotDirectory/" task pullScreenshotsContextInternal(type: Exec) { group = 'Test-Screenshots' description = 'Pull screenshots From context.external Storage' ignoreExitValue(true) commandLine "$adb", 'pull', "$contextInternalFullPath", "$savePath/" } task pullScreenshotsContextExternal(type: Exec) { group = 'Test-Screenshots' description = 'Pull screenshots From context.external Storage' ignoreExitValue(true) commandLine "$adb", 'pull', "$contextExternalFullPath", "$savePath/" } task pullScreenshotsEnvironmentExternal(type: Exec) { group = 'Test-Screenshots' description = 'Pull screenshots From environment.external Storage' ignoreExitValue(true) commandLine "$adb", 'pull', "$environmentExternalFullPath", "$savePath/" } task pullScreenshots(dependsOn: [pullScreenshotsContextInternal, pullScreenshotsContextExternal, pullScreenshotsEnvironmentExternal]) { group = 'Test-Screenshots' description = 'Pull screenshots From Device' } task removeScreenshotsFromDeviceContextInternal(type: Exec) { group = 'Test-Screenshots' description = 'Remove screenshots From context.internal Storage' ignoreExitValue(true) commandLine "$adb", 'shell', 'rm', '-r', "$contextInternalFullPath" } task removeScreenshotsFromDeviceContextExternal(type: Exec) { group = 'Test-Screenshots' description = 'Remove screenshots From context.external Storage' ignoreExitValue(true) commandLine "$adb", 'shell', 'rm', '-r', "$contextExternalFullPath" } task removeScreenshotsFromDeviceEnvironmentExternal(type: Exec) { group = 'Test-Screenshots' description = 'Remove screenshots From environment.external Storage' ignoreExitValue(true) commandLine "$adb", 'shell', 'rm', '-r', "$environmentExternalFullPath" } task removeScreenshotsFromDevice(dependsOn: [removeScreenshotsFromDeviceContextInternal, removeScreenshotsFromDeviceContextExternal, removeScreenshotsFromDeviceEnvironmentExternal]) { group = 'Test-Screenshots' description = 'Remove screenshots From Device' } task removeLocalScreenshots(type: Delete) { group = 'Test-Screenshots' description = 'Remove screenshots From Local Machine' delete files("$savePath") } task saveLogcatLogs(type: Exec) { group = 'Test-Screenshots' description = 'Show Logcat' doFirst { standardOutput = new FileOutputStream("${buildDir}/logcat.txt") } commandLine "$adb", 'logcat', '-d' } task hasScreenshots(type: Exec) { commandLine "sh", "./verifyfiles.sh", "$savePath" } afterEvaluate { connectedDebugAndroidTest.finalizedBy saveLogcatLogs saveLogcatLogs.finalizedBy pullScreenshots clean.dependsOn(removeLocalScreenshots) }