1/* 2 * Modified from https://gist.github.com/xian/05c4f27da6d4156b9827842217c2cd5c 3 * Reference: http://robolectric.org/blog/2017/03/01/hermetic-builds/ 4 */ 5defaultTasks 'copyLibs' 6 7def shadowArtifacts = [ 8 "org.robolectric:shadows-framework:${robolectricVersion}", 9 "org.robolectric:shadows-httpclient:${robolectricVersion}", 10 "org.robolectric:shadows-multidex:${robolectricVersion}", 11 "org.robolectric:shadows-playservices:${robolectricVersion}", 12 "org.robolectric:shadows-supportv4:${robolectricVersion}", 13] 14 15apply plugin: 'java' 16 17repositories { 18 mavenCentral() 19 google() 20} 21 22configurations { 23 sandbox 24 roboSources 25} 26 27// In this section you declare the dependencies for your production and test code 28dependencies { 29 implementation("org.robolectric:robolectric:${robolectricVersion}") { 30 // we don't need these MavenDependencyResolver in a hermetic build 31 exclude group: 'org.apache.maven', module: '' 32 exclude group: 'org.apache.ant', module: '' 33 } 34 35 shadowArtifacts.forEach { shadowArtifact -> 36 implementation ("${shadowArtifact}") { 37 // we don't need these MavenDependencyResolver in a hermetic build 38 exclude group: 'org.apache.maven', module: '' 39 exclude group: 'org.apache.ant', module: '' 40 } 41 sandbox ("${shadowArtifact}") { 42 // we don't need these MavenDependencyResolver in a hermetic build 43 exclude group: 'org.apache.maven', module: '' 44 exclude group: 'org.apache.ant', module: '' 45 } 46 } 47 48 def shadowArtifactsSet = shadowArtifacts.collect {it.toString()} toSet() 49 configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact ra -> 50 ModuleVersionIdentifier id = ra.moduleVersion.id 51 // download only core sources. relax restriction if required 52 if ("org.robolectric".equals(id.group) && !shadowArtifactsSet.contains(id.toString())) { 53 roboSources("${id.group}:${id.name}:${id.version}:sources") { 54 transitive = false 55 } 56 } 57 } 58} 59 60 61task copyLibs(type: Copy) { 62 from configurations.runtimeClasspath 63 from configurations.roboSources 64 into "$buildDir/lib" 65 66 doLast { 67 def f = new File("$buildDir/classpath_jars.mk") 68 f.delete() 69 def jars = source.getFiles() 70 .collect { it.name } 71 .sort() 72 .findAll { !it.endsWith("sources.jar") } 73 .collect { " lib/${it} " } 74 f << "my_robolectric_runtime_deps := \\\n" << jars.join("\\\n") << "\n" 75 } 76} 77