// 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 { System.err.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 internalFullPath = "/sdcard/Android/data/$packageName/files/Pictures/$screenshotDirectory/" def deprecatedFullPath = "/sdcard/Pictures/$packageName/$screenshotDirectory/" task pullScreenshotsInternal(type: Exec) { group = 'Test-Screenshots' description = 'Pull screenshots From internal Storage' ignoreExitValue(true) commandLine "$adb", 'pull', "$internalFullPath", "$savePath/" } task pullScreenshotsDeprecated(type: Exec) { group = 'Test-Screenshots' description = 'Pull screenshots From deprecated External Storage' ignoreExitValue(true) commandLine "$adb", 'pull', "$deprecatedFullPath", "$savePath/" } task pullScreenshots(dependsOn: [pullScreenshotsInternal, pullScreenshotsDeprecated]) { group = 'Test-Screenshots' description = 'Pull screenshots From Device' } task removeScreenshotsFromDeviceInternal(type: Exec) { group = 'Test-Screenshots' description = 'Remove screenshots From internal Storage' ignoreExitValue(true) commandLine "$adb", 'shell', 'rm', '-r', "$internalFullPath" } task removeScreenshotsFromDeviceDeprecated(type: Exec) { group = 'Test-Screenshots' description = 'Remove screenshots From deprecated External Storage' ignoreExitValue(true) commandLine "$adb", 'shell', 'rm', '-r', "$deprecatedFullPath" } task removeScreenshotsFromDevice(dependsOn: [removeScreenshotsFromDeviceInternal, removeScreenshotsFromDeviceDeprecated]) { group = 'Test-Screenshots' description = 'Remove screenshots From Device' } task removeLocalScreenshots(type: Delete) { group = 'Test-Screenshots' description = 'Remove screenshots From Local Machine' delete files("$savePath") } afterEvaluate { connectedDebugAndroidTest.finalizedBy pullScreenshots pullScreenshots.finalizedBy removeScreenshotsFromDevice clean.dependsOn(removeLocalScreenshots) }