Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mapping file provider in case of legacy Variant API #265

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.RegularFile
import org.gradle.api.logging.Logging
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.TaskContainer
Expand Down Expand Up @@ -210,20 +211,22 @@ class DdAndroidGradlePlugin @Inject constructor(
)
}
val extensionConfiguration = resolveExtensionConfiguration(extension, variant)
configureVariantTask(uploadTask, apiKey, variant.flavorName, extensionConfiguration, variant)
configureVariantTask(
target.objects,
uploadTask,
apiKey,
extensionConfiguration,
variant
)

uploadTask.buildId.set(buildIdGenerationTask.lazyBuildIdProvider(providerFactory))

uploadTask.applicationId.set(variant.applicationId)
uploadTask.mappingFile.set(resolveMappingFile(extensionConfiguration, target, variant))
uploadTask.mappingFilePackagesAliases = extensionConfiguration.mappingFilePackageAliases
uploadTask.mappingFileTrimIndents = extensionConfiguration.mappingFileTrimIndents
if (!extensionConfiguration.ignoreDatadogCiFileConfig) {
uploadTask.datadogCiFile = TaskUtils.findDatadogCiFile(target.projectDir)
}

uploadTask.repositoryFile = TaskUtils.resolveDatadogRepositoryFile(target)
uploadTask.sourceSetRoots.set(variant.collectJavaAndKotlinSourceDirectories())
}
}

Expand Down Expand Up @@ -296,27 +299,32 @@ class DdAndroidGradlePlugin @Inject constructor(

private fun resolveMappingFile(
extensionConfiguration: DdExtensionConfiguration,
target: Project,
objectFactory: ObjectFactory,
variant: AppVariant
): Provider<RegularFile> {
val customPath = extensionConfiguration.mappingFilePath
return if (customPath != null) {
target.objects.fileProperty().fileValue(File(customPath))
objectFactory.fileProperty().fileValue(File(customPath))
} else {
variant.mappingFile
}
}

private fun configureVariantTask(
objectFactory: ObjectFactory,
uploadTask: MappingFileUploadTask,
apiKey: ApiKey,
flavorName: String,
extensionConfiguration: DdExtensionConfiguration,
variant: AppVariant
) {
uploadTask.apiKey = apiKey.value
uploadTask.apiKeySource = apiKey.source
uploadTask.variantName = flavorName
uploadTask.variantName = variant.flavorName

uploadTask.applicationId.set(variant.applicationId)

uploadTask.mappingFile.set(resolveMappingFile(extensionConfiguration, objectFactory, variant))
uploadTask.sourceSetRoots.set(variant.collectJavaAndKotlinSourceDirectories())

uploadTask.site = extensionConfiguration.site ?: ""
if (extensionConfiguration.versionName != null) {
Expand All @@ -331,6 +339,8 @@ class DdAndroidGradlePlugin @Inject constructor(
uploadTask.serviceName.set(variant.applicationId)
}
uploadTask.remoteRepositoryUrl = extensionConfiguration.remoteRepositoryUrl ?: ""

variant.bindWith(uploadTask)
}

internal fun resolveExtensionConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ internal object CurrentAgpVersion {

val EXTERNAL_NATIVE_BUILD_SOFOLDER_IS_PUBLIC: Boolean
get() = TaskUtils.isAgpAbove(major = 8, minor = 0, patch = 0)

val CAN_QUERY_MAPPING_FILE_PROVIDER: Boolean
get() = TaskUtils.isAgpAbove(major = 7, minor = 0, patch = 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package com.datadog.gradle.plugin.internal.variant

import com.android.build.gradle.AppExtension
import com.datadog.gradle.plugin.GenerateBuildIdTask
import com.datadog.gradle.plugin.MappingFileUploadTask
import com.datadog.gradle.plugin.NdkSymbolFileUploadTask
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
Expand Down Expand Up @@ -47,6 +48,8 @@ internal interface AppVariant {

fun bindWith(ndkUploadTask: NdkSymbolFileUploadTask)

fun bindWith(mappingFileUploadTask: MappingFileUploadTask)

// new variant API doesn't allow to run addGeneratedSourceDirectory from inside Task#configure, thus this
fun bindWith(generateBuildIdTask: TaskProvider<GenerateBuildIdTask>, buildIdDirectory: Provider<Directory>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package com.datadog.gradle.plugin.internal.variant

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.datadog.gradle.plugin.DdAndroidGradlePlugin
import com.datadog.gradle.plugin.GenerateBuildIdTask
import com.datadog.gradle.plugin.MappingFileUploadTask
import com.datadog.gradle.plugin.NdkSymbolFileUploadTask
import com.datadog.gradle.plugin.internal.CurrentAgpVersion
import com.datadog.gradle.plugin.internal.getSearchObjDirs
Expand All @@ -19,6 +21,7 @@ import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths

internal class LegacyApiAppVariant(
Expand All @@ -28,6 +31,7 @@ internal class LegacyApiAppVariant(
) : AppVariant {

private val providerFactory = target.providers
private val projectLayout = target.layout

override val name: String
get() = variant.name
Expand All @@ -50,9 +54,30 @@ internal class LegacyApiAppVariant(
override val flavors: List<String>
get() = variant.productFlavors.map { it.name }
override val mappingFile: Provider<RegularFile>
get() = target.layout.buildDirectory.file(
Paths.get("outputs", "mapping", variant.name, "mapping.txt").toString()
)
get() = if (CurrentAgpVersion.CAN_QUERY_MAPPING_FILE_PROVIDER) {
variant.mappingFileProvider
.flatMap {
providerFactory.provider {
try {
projectLayout.projectDirectory.file(it.singleFile.absolutePath)
} catch (e: IllegalStateException) {
DdAndroidGradlePlugin.LOGGER.info(
"Mapping FileCollection is empty or contains multiple files",
e
)
null
}
}.orElse(legacyMappingFileProvider)
}
} else {
legacyMappingFileProvider
}

private val legacyMappingFileProvider: Provider<RegularFile>
get() = projectLayout.buildDirectory.file(legacyMappingFilePath.toString())

private val legacyMappingFilePath: Path
get() = Paths.get("outputs", "mapping", variant.name, "mapping.txt")

override fun collectJavaAndKotlinSourceDirectories(): Provider<List<File>> {
val roots = mutableListOf<File>()
Expand All @@ -75,6 +100,11 @@ internal class LegacyApiAppVariant(
}
}

override fun bindWith(mappingFileUploadTask: MappingFileUploadTask) {
val minifyTask = target.tasks.findByName("minify${variant.name.capitalize()}WithR8") ?: return
mappingFileUploadTask.dependsOn(minifyTask)
}

override fun bindWith(
generateBuildIdTask: TaskProvider<GenerateBuildIdTask>,
buildIdDirectory: Provider<Directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.android.build.api.variant.ApplicationVariant
import com.android.build.api.variant.VariantOutput
import com.android.build.gradle.tasks.ExternalNativeBuildTask
import com.datadog.gradle.plugin.GenerateBuildIdTask
import com.datadog.gradle.plugin.MappingFileUploadTask
import com.datadog.gradle.plugin.NdkSymbolFileUploadTask
import com.datadog.gradle.plugin.internal.getSearchObjDirs
import org.gradle.api.Project
Expand Down Expand Up @@ -78,6 +79,10 @@ internal class NewApiAppVariant(
}
}

override fun bindWith(mappingFileUploadTask: MappingFileUploadTask) {
// nothing is needed, dependency on minification task is created by mapping file provider
}

override fun bindWith(
generateBuildIdTask: TaskProvider<GenerateBuildIdTask>,
buildIdDirectory: Provider<Directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,56 @@ internal class LegacyApiAppVariantTest {

@Test
fun `M return mapping file W mappingFile`(
@TempDir fakeMappingDirectory: File,
@StringForgery fakeVariantName: String,
@StringForgery fakeMappingFileName: String
) {
// Given
val fakeMappingFile = File(fakeMappingDirectory, fakeMappingFileName)
whenever(
mockAndroidVariant.mappingFileProvider
) doReturn fakeProject.provider { fakeProject.files(fakeMappingFile) }
whenever(mockAndroidVariant.name) doReturn fakeVariantName

// When
val mappingFile = testedAppVariant.mappingFile

// Then
assertThat(mappingFile.get().asFile).isEqualTo(fakeMappingFile)
}

@Test
fun `M return mapping file W mappingFile { fallback to legacy, no mapping files in provider }`(
@StringForgery fakeVariantName: String
) {
// Given
whenever(
mockAndroidVariant.mappingFileProvider
) doReturn fakeProject.provider { fakeProject.files() }
whenever(mockAndroidVariant.name) doReturn fakeVariantName

// When
val mappingFile = testedAppVariant.mappingFile

// Then
assertThat(mappingFile.get().asFile.path).endsWith(
Paths.get("outputs", "mapping", fakeVariantName, "mapping.txt").toString()
)
}

@Test
fun `M return mapping file W mappingFile { fallback to legacy, multiple files in provider }`(
@TempDir fakeMappingDirectory: File,
@StringForgery fakeVariantName: String,
@StringForgery fakeMappingFileNameA: String,
@StringForgery fakeMappingFileNameB: String
) {
// Given
val fileA = File(fakeMappingDirectory, fakeMappingFileNameA)
val fileB = File(fakeMappingDirectory, fakeMappingFileNameB)
whenever(
mockAndroidVariant.mappingFileProvider
) doReturn fakeProject.provider { fakeProject.files(fileA, fileB) }
whenever(mockAndroidVariant.name) doReturn fakeVariantName

// When
Expand Down
Loading