def robolectricDependenciesFolder = rootProject.buildDir.path + "/robolectric-dependencies" configurations.create('robolectricRuntime') dependencies { robolectricRuntime "org.robolectric:android-all:6.0.1_r3-robolectric-0" robolectricRuntime "org.robolectric:shadows-core:3.2.2" } rootProject.task(type: Copy, overwrite: true, "downloadRobolectricDependencies") { println "downloadRobolectricDependencies " + robolectricDependenciesFolder from configurations.robolectricRuntime into robolectricDependenciesFolder } project.afterEvaluate { tasks.all { if (it.name.startsWith("test")) { it.dependsOn(rootProject.tasks.findByName("downloadRobolectricDependencies")) } } } android { testOptions { unitTests.all { systemProperty 'robolectric.offline', 'true' systemProperty 'robolectric.dependency.dir', robolectricDependenciesFolder } } }