1package org.robolectric.gradle 2 3import org.gradle.api.Plugin 4import org.gradle.api.Project 5import org.gradle.api.tasks.testing.Test 6 7class AndroidProjectConfigPlugin implements Plugin<Project> { 8 @Override 9 void apply(Project project) { 10 project.android.testOptions.unitTests.all { 11 // TODO: DRY up code with RoboJavaModulePlugin... 12 testLogging { 13 exceptionFormat "full" 14 showCauses true 15 showExceptions true 16 showStackTraces true 17 showStandardStreams true 18 events = ["failed", "skipped"] 19 } 20 21 minHeapSize = "2048m" 22 maxHeapSize = "12288m" 23 24 if (System.env['GRADLE_MAX_PARALLEL_FORKS'] != null) { 25 maxParallelForks = Integer.parseInt(System.env['GRADLE_MAX_PARALLEL_FORKS']) 26 } 27 28 def forwardedSystemProperties = System.properties 29 .findAll { k, v -> k.startsWith("robolectric.") } 30 .collect { k, v -> "-D$k=$v" } 31 jvmArgs = forwardedSystemProperties 32 jvmArgs += [ 33 '--add-opens=java.base/java.lang=ALL-UNNAMED', 34 '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED', 35 '--add-opens=java.base/java.io=ALL-UNNAMED', 36 '--add-opens=java.base/java.net=ALL-UNNAMED', 37 '--add-opens=java.base/java.security=ALL-UNNAMED', 38 '--add-opens=java.base/java.text=ALL-UNNAMED', 39 '--add-opens=java.base/java.util=ALL-UNNAMED', 40 '--add-opens=java.desktop/java.awt.font=ALL-UNNAMED', 41 '--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED', 42 '--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED', 43 '--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED', 44 ] 45 46 doFirst { 47 if (!forwardedSystemProperties.isEmpty()) { 48 println "Running tests with ${forwardedSystemProperties}" 49 } 50 } 51 } 52 53 project.task('provideBuildClasspath', type: ProvideBuildClasspathTask) { 54 File outDir = project.layout.buildDirectory.dir("generated/robolectric").get().asFile 55 outFile = new File(outDir, "robolectric-deps.properties") 56 57 project.android.sourceSets['test'].resources.srcDir(outDir) 58 } 59 60 project.afterEvaluate { 61 project.tasks.forEach { task -> 62 if (task.name.matches("process.*UnitTestJavaRes")) { 63 task.dependsOn "provideBuildClasspath" 64 } 65 } 66 } 67 68 // Only run tests in the debug variant. This is to avoid running tests twice when `./gradlew test` is run at the top-level. 69 project.tasks.withType(Test).configureEach { 70 onlyIf { variantName.toLowerCase().contains('debug') } 71 } 72 } 73} 74 75