Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions packages/core/datadog-configuration.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,69 @@ def getConfigurationFile() {
}

/*
* Copies the configuration file into the build directory and renames it.
* Copies the configuration file into the output directory and renames it.
*/
task copyDatadogConfigurationFile() {
// Note: there can be multiple source sets depending on the variants declared, so we
// create a configuration for all of them. This prevents warnings in Android Studio when
// adding the same directory to sources for different variants.
android.sourceSets.forEach { source ->
copy {
from getConfigurationFile()
into getBuildAssetsDirectory(source.name)
rename {
'datadog-configuration.json'
}
}
abstract class CopyDatadogConfigurationFileTask extends DefaultTask {
@InputFile
@PathSensitive(PathSensitivity.NONE)
abstract RegularFileProperty getConfigurationFile()

@OutputDirectory
abstract DirectoryProperty getOutputDirectory()

@TaskAction
void copyConfigurationFile() {
def outputFile = new File(outputDirectory.get().asFile, 'datadog-configuration.json')
outputFile.bytes = configurationFile.get().asFile.bytes
}
}

// Adds the copied configuration file to the assets of the app for every sourceSet.
android.sourceSets.forEach { source ->
source.assets.srcDir(getBuildAssetsDirectory(source.name))
def isVariantSourcesApiAvailable() {
def androidComponents = project.extensions.findByName("androidComponents")
if (androidComponents == null) {
return false
}
// Variant.sources.assets and addGeneratedSourceDirectory are available from AGP 7.3.
def version = androidComponents.pluginVersion
return version.major > 7 || (version.major == 7 && version.minor >= 3)
}

gradle.projectsEvaluated {
preBuild.dependsOn(copyDatadogConfigurationFile)
if (isVariantSourcesApiAvailable()) {
// AGP 9 rejects Provider instances in the SourceSet API, so the configuration file is
// registered as a generated assets directory of every variant instead.
def configurationFile = file(getConfigurationFile())

androidComponents.onVariants(androidComponents.selector().all(), { variant ->
def copyTask = project.tasks.register(
"copyDatadogConfigurationFile${variant.name.capitalize()}",
CopyDatadogConfigurationFileTask
) {
it.configurationFile.set(configurationFile)
}
variant.sources.assets?.addGeneratedSourceDirectory(copyTask, { it.outputDirectory })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Assign an output directory before wiring the task

On every AGP 7.3+ build that applies this script, addGeneratedSourceDirectory wires the task's existing DirectoryProperty but does not choose a filesystem location for it. Because the registration block sets only configurationFile, AGP or the task action eventually queries an unset outputDirectory and the variant fails before the asset can be copied; set that property to a per-variant build directory when registering the task.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. addGeneratedSourceDirectory sets the location itself: AGP assigns the wired DirectoryProperty to build/generated/assets/<taskName>/, so no explicit value is needed. In the test builds the file lands at app/build/generated/assets/copyDatadogConfigurationFileDebug/datadog-configuration.json on AGP 7.4.2, 8.7.2 and 9.2.1, and ends up in the APK.

})
} else {
task copyDatadogConfigurationFile() {
// Note: there can be multiple source sets depending on the variants declared, so we
// create a configuration for all of them. This prevents warnings in Android Studio when
// adding the same directory to sources for different variants.
android.sourceSets.forEach { source ->
copy {
from getConfigurationFile()
into getBuildAssetsDirectory(source.name)
rename {
'datadog-configuration.json'
}
}
}

// Adds the copied configuration file to the assets of the app for every sourceSet.
android.sourceSets.forEach { source ->
source.assets.srcDir(getBuildAssetsDirectory(source.name))
}
}

gradle.projectsEvaluated {
preBuild.dependsOn(copyDatadogConfigurationFile)
}
}