普通文本  |  75行  |  2.33 KB

/*
 * Modified from https://gist.github.com/xian/05c4f27da6d4156b9827842217c2cd5c
 * Reference: http://robolectric.org/blog/2017/03/01/hermetic-builds/
 */
defaultTasks 'copyLibs'

def shadowArtifacts = [
        "org.robolectric:shadows-framework:${robolectricVersion}",
        "org.robolectric:shadows-httpclient:${robolectricVersion}",
        "org.robolectric:shadows-multidex:${robolectricVersion}",
        "org.robolectric:shadows-playservices:${robolectricVersion}",
        "org.robolectric:shadows-supportv4:${robolectricVersion}",
]

def buildDir = System.getProperty("user.dir")

apply plugin: 'java'

repositories {
    mavenCentral()
}

configurations {
    sandbox
    roboSources
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile("org.robolectric:robolectric:${robolectricVersion}") {
        // we don't need these MavenDependencyResolver in a hermetic build
        exclude group: 'org.apache.maven', module: 'maven-ant-tasks'
        exclude group: 'org.apache.ant', module: 'ant'
    }

    compile('com.ximpleware:vtd-xml:2.11')
    // Force ASM dependency to be 6.0 for JDK9 support
    compile('org.ow2.asm:asm:6.0')
    compile('org.ow2.asm:asm-commons:6.0')

    shadowArtifacts.forEach { shadowArtifact ->
        compile shadowArtifact
        sandbox shadowArtifact
    }

    def shadowArtifactsSet = shadowArtifacts.collect {it.toString()}  toSet()
    configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact ra ->
        ModuleVersionIdentifier id = ra.moduleVersion.id
        // download only core sources. relax restriction if required
        if ("org.robolectric".equals(id.group) && !shadowArtifactsSet.contains(id.toString())) {
            roboSources("${id.group}:${id.name}:${id.version}:sources") {
                transitive = false
            }
        }
    }
}


task copyLibs(type: Copy) {
    from configurations.compile
    from configurations.roboSources
    into "$buildDir/lib"

    doLast {
        def f = new File("$buildDir/classpath_jars.mk")
        f.delete()
        def jars = source.getFiles()
            .collect { it.name }
            .sort()
            .findAll { !it.endsWith("sources.jar") }
            .collect { "    lib/${it} " }
        f << "my_robolectric_runtime_deps := \\\n" << jars.join("\\\n") << "\n"
    }
}