|
6 | 6 | import com.android.build.api.variant.BuiltArtifactsLoader;
|
7 | 7 | import com.android.build.api.variant.HasAndroidTest;
|
8 | 8 | import com.android.build.api.variant.LibraryAndroidComponentsExtension;
|
| 9 | +import org.apache.commons.lang3.StringUtils; |
9 | 10 | import org.gradle.api.*;
|
10 | 11 | import org.gradle.api.experimental.android.DEFAULT_SDKS;
|
11 | 12 | import org.gradle.api.experimental.android.library.AndroidLibrary;
|
12 | 13 | import org.gradle.api.file.Directory;
|
13 | 14 | import org.gradle.api.provider.Provider;
|
| 15 | +import org.gradle.api.tasks.testing.Test; |
| 16 | +import org.gradle.testing.jacoco.plugins.JacocoPluginExtension; |
| 17 | +import org.gradle.testing.jacoco.plugins.JacocoTaskExtension; |
| 18 | +import org.gradle.testing.jacoco.tasks.JacocoReport; |
14 | 19 | import org.jetbrains.annotations.NotNull;
|
15 | 20 | import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions;
|
16 | 21 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;
|
17 | 22 |
|
| 23 | +import java.io.File; |
18 | 24 | import java.lang.reflect.Method;
|
19 | 25 | import java.util.*;
|
20 | 26 | import java.util.function.BiConsumer;
|
@@ -43,6 +49,10 @@ public static void configureNia(Project project,
|
43 | 49 | configureGradleManagedDevices(androidLib);
|
44 | 50 | configureLint(androidLib);
|
45 | 51 | configurePrintApksTask(project, androidLibComponents);
|
| 52 | + |
| 53 | + if (dslModel.getConfigureJacoco().get()) { |
| 54 | + configureJacoco(project, androidLib); |
| 55 | + } |
46 | 56 | }
|
47 | 57 |
|
48 | 58 | private static void configureLint(LibraryExtension androidLib) {
|
@@ -211,4 +221,67 @@ private static void configurePrintApksTask(Project project, AndroidComponentsExt
|
211 | 221 | }
|
212 | 222 | });
|
213 | 223 | }
|
| 224 | + |
| 225 | + private static List<String> coverageExclusions() { |
| 226 | + return Arrays.asList( |
| 227 | + "**/R.class", |
| 228 | + "**/R$*.class", |
| 229 | + "**/BuildConfig.*", |
| 230 | + "**/Manifest*.*" |
| 231 | + ); |
| 232 | + } |
| 233 | + |
| 234 | + @SuppressWarnings("deprecation") |
| 235 | + private static void configureJacoco(Project project, LibraryExtension androidLib) { |
| 236 | + project.getPlugins().apply("jacoco"); |
| 237 | + |
| 238 | + androidLib.getBuildTypes().configureEach(buildType -> { |
| 239 | + buildType.setEnableAndroidTestCoverage(true); |
| 240 | + buildType.setEnableUnitTestCoverage(true); |
| 241 | + }); |
| 242 | + |
| 243 | + JacocoPluginExtension jacocoPluginExtension = project.getExtensions().getByType(JacocoPluginExtension.class); |
| 244 | + jacocoPluginExtension.setToolVersion("0.8.7"); |
| 245 | + |
| 246 | + LibraryAndroidComponentsExtension androidLibComponents = project.getExtensions().getByType(LibraryAndroidComponentsExtension.class); |
| 247 | + androidLibComponents.onVariants(androidLibComponents.selector().all(), variant -> { |
| 248 | + final String testTaskName = "test" + StringUtils.capitalize(variant.getName()) + "UnitTest"; |
| 249 | + final File buildDir = project.getLayout().getBuildDirectory().get().getAsFile(); |
| 250 | + project.getTasks().register("jacoco" + StringUtils.capitalize(testTaskName) + "Report", JacocoReport.class, task -> { |
| 251 | + task.dependsOn(testTaskName); |
| 252 | + |
| 253 | + task.reports(report -> { |
| 254 | + report.getXml().getRequired().set(true); |
| 255 | + report.getXml().getRequired().set(true); |
| 256 | + }); |
| 257 | + |
| 258 | + task.getClassDirectories().setFrom( |
| 259 | + project.fileTree(project.getBuildDir() + "/tmp/kotlin-classes/" + variant.getName(), tree -> { |
| 260 | + tree.exclude(coverageExclusions()); |
| 261 | + }) |
| 262 | + ); |
| 263 | + |
| 264 | + task.getSourceDirectories().setFrom( |
| 265 | + project.files(project.getProjectDir() + "/src/main/java", project.getProjectDir() + "/src/main/kotlin" |
| 266 | + )); |
| 267 | + |
| 268 | + task.getExecutionData().setFrom( |
| 269 | + project.files(buildDir + "/jacoco/" + testTaskName + ".exec") |
| 270 | + ); |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + project.getTasks().withType(Test.class, test -> { |
| 275 | + JacocoTaskExtension jacocoTaskExtension = test.getExtensions().getByType(JacocoTaskExtension.class); |
| 276 | + |
| 277 | + // Required for JaCoCo + Robolectric |
| 278 | + // https://github.com/robolectric/robolectric/issues/2230 |
| 279 | + // Consider removing if not we don't add Robolectric |
| 280 | + jacocoTaskExtension.setIncludeNoLocationClasses(true); |
| 281 | + |
| 282 | + // Required for JDK 11 with the above |
| 283 | + // https://github.com/gradle/gradle/issues/5184#issuecomment-391982009 |
| 284 | + jacocoTaskExtension.setExcludes(Collections.singletonList("jdk.internal.*")); |
| 285 | + }); |
| 286 | + } |
214 | 287 | }
|
0 commit comments