def robolectricDependenciesFolder = new File(rootProject.buildDir, "robolectric-3.2.2-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 into " + 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 } } }