|
| 1 | +package datadog.trace.instrumentation.gradle |
| 2 | + |
| 3 | +import datadog.trace.api.civisibility.domain.BuildModuleLayout |
| 4 | +import datadog.trace.api.civisibility.domain.SourceSet |
| 5 | +import org.gradle.api.Project |
| 6 | +import org.gradle.api.file.FileTree |
| 7 | +import org.gradle.api.logging.Logger |
| 8 | +import org.gradle.api.logging.Logging |
| 9 | +import org.gradle.api.tasks.testing.Test |
| 10 | + |
| 11 | +class AndroidGradleUtils { |
| 12 | + |
| 13 | + private static final Logger LOGGER = Logging.getLogger(AndroidGradleUtils) |
| 14 | + |
| 15 | + static BuildModuleLayout getAndroidModuleLayout(Project project, Test task) { |
| 16 | + try { |
| 17 | + def variant = getVariant(project, task) |
| 18 | + if (variant == null) { |
| 19 | + return null |
| 20 | + } |
| 21 | + |
| 22 | + def sources = getSources(variant) |
| 23 | + def destinations = getDestinations(variant, project) |
| 24 | + return new BuildModuleLayout(Collections.singletonList(new SourceSet(SourceSet.Type.CODE, sources, destinations))) |
| 25 | + } catch (Exception e) { |
| 26 | + LOGGER.error("Could not get Android module layout for ${project.name} and ${task.path}", e) |
| 27 | + return null |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static getVariant(Project project, Test task) { |
| 32 | + def androidPlugin = project.plugins.findPlugin('android') ?: project.plugins.findPlugin('android-library') |
| 33 | + |
| 34 | + def variants |
| 35 | + if (androidPlugin.class.simpleName == 'LibraryPlugin') { |
| 36 | + variants = project.android.libraryVariants |
| 37 | + } else { |
| 38 | + variants = project.android.applicationVariants |
| 39 | + } |
| 40 | + |
| 41 | + for (def v : variants) { |
| 42 | + if (task.path.endsWith("test${v.name.capitalize()}UnitTest")) { |
| 43 | + return v |
| 44 | + } |
| 45 | + } |
| 46 | + return null |
| 47 | + } |
| 48 | + |
| 49 | + static Collection<File> getSources(variant) { |
| 50 | + List<File> sources = [] |
| 51 | + for (def srcs : variant.sourceSets.java.srcDirs) { |
| 52 | + sources.addAll(srcs) |
| 53 | + } |
| 54 | + return sources |
| 55 | + } |
| 56 | + |
| 57 | + private static final EXCLUDES = [ |
| 58 | + 'android/databinding/**/*.class', |
| 59 | + '**/android/databinding/*Binding.class', |
| 60 | + '**/BR.*', |
| 61 | + '**/R.class', |
| 62 | + '**/R$*.class', |
| 63 | + '**/BuildConfig.*', |
| 64 | + '**/Manifest*.*', |
| 65 | + '**/*$ViewInjector*.*', |
| 66 | + '**/*$ViewBinder*.*', |
| 67 | + '**/*_MembersInjector.class', |
| 68 | + '**/Dagger*Component.class', |
| 69 | + '**/Dagger*Component$Builder.class', |
| 70 | + '**/*Module_*Factory.class' |
| 71 | + ] |
| 72 | + |
| 73 | + private static Collection<File> getDestinations(variant, Project project) { |
| 74 | + def javaDestinations = getJavaDestinations(variant) |
| 75 | + FileTree javaTree = project.fileTree(dir: javaDestinations, excludes: EXCLUDES) |
| 76 | + |
| 77 | + FileTree destinationsTree |
| 78 | + if (project.plugins.findPlugin('kotlin-android') != null) { |
| 79 | + def kotlinDestinations = "${project.buildDir}/tmp/kotlin-classes/${variant.name}" |
| 80 | + def kotlinTree = project.fileTree(dir: kotlinDestinations, excludes: EXCLUDES) |
| 81 | + destinationsTree = javaTree + kotlinTree |
| 82 | + } else { |
| 83 | + destinationsTree = javaTree |
| 84 | + } |
| 85 | + return destinationsTree.files |
| 86 | + } |
| 87 | + |
| 88 | + private static getJavaDestinations(variant) { |
| 89 | + if (variant.hasProperty('javaCompileProvider')) { |
| 90 | + return variant.javaCompileProvider.get().destinationDir |
| 91 | + } |
| 92 | + return variant.javaCompile.destinationDir |
| 93 | + } |
| 94 | +} |
0 commit comments