As a start here's a script that could be added to the build.gradle
file and do the job of copying files from a certain folder and placing them within the device.
I do want to point out that @m-reza-nasirloo has a point about storing these files within the repo and so it has to be taken into consideration for designing a proper test while having a maintainable and small sized repo that the CI server or teammate could easily download clone. With the script below, the test files can be placed outside the project root directory and achieve these points.
Also another point is this script assumes that the directory of the test files
(in here it is testFiles
) contains only files and not sub-directories. To handle sub-directories would need some more tweaks.
Furthermore, the script will push to all of the connected devices. So in case your running the tests from within Android Studio, devices which you didn't select to run will get the files as well.
Lastly, this script will be executed when building for android tests, specifically when gradle is executing assembleDebugAndroidTest
or similar tasks. To change that just modify the if condition as you deem necessary.
import com.android.ddmlib.AndroidDebugBridge
task pushFilesToDevices {
def location = "${project.rootDir}/../testFiles/"
def files = new File(location).listFiles()
AndroidDebugBridge.initIfNeeded(false)
def bridge = AndroidDebugBridge.createBridge(android.adbExecutable.path, false)
doLast {
bridge.devices.each { device ->
println "pushing files to ${device.name}"
files.each { file ->
device.pushFile(file.absolutePath, "/sdcard/${file.name}")
}
println "finished pushing"
}
}
}
tasks.whenTaskAdded { taskItem ->
if (taskItem.name.contains("assemble") && taskItem.name.endsWith("AndroidTest")) {
taskItem.dependsOn pushFilesToDevices
}
}