private void createUnitTestTask(@NonNull TaskFactory tasks, @NonNull final TestVariantData variantData) { final BaseVariantData testedVariantData = (BaseVariantData) variantData.getTestedVariantData(); final Test runTestsTask = project.getTasks().create( variantData.getScope().getTaskName(UNIT_TEST.getPrefix()), Test.class); runTestsTask.setGroup(JavaBasePlugin.VERIFICATION_GROUP); runTestsTask.setDescription( "Run unit tests for the " + testedVariantData.getVariantConfiguration().getFullName() + " build."); fixTestTaskSources(runTestsTask); runTestsTask.dependsOn(variantData.assembleVariantTask); final AbstractCompile testCompileTask = variantData.javacTask; runTestsTask.setTestClassesDir(testCompileTask.getDestinationDir()); ConventionMappingHelper.map(runTestsTask, "classpath", new Callable<ConfigurableFileCollection>() { @Override public ConfigurableFileCollection call() throws Exception { Iterable<File> filteredBootClasspath = Iterables.filter( androidBuilder.getBootClasspath(), new Predicate<File>() { @Override public boolean apply(@Nullable File file) { return file != null && !SdkConstants.FN_FRAMEWORK_LIBRARY .equals(file.getName()); } }); return project.files( testCompileTask.getClasspath(), testCompileTask.getOutputs().getFiles(), variantData.processJavaResourcesTask.getOutputs(), testedVariantData.processJavaResourcesTask.getOutputs(), filteredBootClasspath, // Mockable JAR is last, to make sure you can shadow the classes // withdependencies. createMockableJar.getOutputFile()); } }); // Put the variant name in the report path, so that different testing tasks don't // overwrite each other's reports. TestTaskReports testTaskReports = runTestsTask.getReports(); for (ConfigurableReport report : new ConfigurableReport[] { testTaskReports.getJunitXml(), testTaskReports.getHtml()}) { report.setDestination(new File(report.getDestination(), testedVariantData.getName())); } tasks.named(JavaPlugin.TEST_TASK_NAME, new Action<Task>() { @Override public void execute(Task test) { test.dependsOn(runTestsTask); } }); extension.getTestOptions().getUnitTests().applyConfiguration(runTestsTask); }