Skip to content

Commit

Permalink
RUM-713 Allow customer to use non standard obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Oct 10, 2023
1 parent 288840c commit 5e2931f
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import javax.inject.Inject
/**
* Plugin adding tasks for Android projects using Datadog's SDK for Android.
*/
@Suppress("TooManyFunctions")
class DdAndroidGradlePlugin @Inject constructor(
private val execOps: ExecOperations
) : Plugin<Project> {
Expand All @@ -40,6 +41,8 @@ class DdAndroidGradlePlugin @Inject constructor(
val androidExtension = target.extensions.findByType(AppExtension::class.java)
if (androidExtension == null) {
LOGGER.error(ERROR_NOT_ANDROID)
} else if (!extension.enabled) {
LOGGER.info("Datadog extension disabled, no upload task created")
} else {
androidExtension.applicationVariants.all { variant ->
configureVariantForUploadTask(target, variant, apiKey, extension)
Expand Down Expand Up @@ -73,13 +76,13 @@ class DdAndroidGradlePlugin @Inject constructor(
apiKey: ApiKey,
extension: DdExtension
): Task? {
if (!variant.buildType.isMinifyEnabled) {
LOGGER.info("Minifying disabled for variant ${variant.name}, no upload task created")
return null
}
val extensionConfiguration = resolveExtensionConfiguration(extension, variant)
val isDefaultObfuscationEnabled = variant.buildType.isMinifyEnabled
val isNonDefaultObfuscationEnabled = extensionConfiguration.nonDefaultObfuscation
val isObfuscationEnabled = isDefaultObfuscationEnabled || isNonDefaultObfuscationEnabled

if (!extension.enabled) {
LOGGER.info("Extension disabled for variant ${variant.name}, no upload task created")
if (!isObfuscationEnabled) {
LOGGER.info("Minifying disabled for variant ${variant.name}, no upload task created")
return null
}

Expand All @@ -91,13 +94,10 @@ class DdAndroidGradlePlugin @Inject constructor(
DdMappingFileUploadTask::class.java,
GitRepositoryDetector(execOps)
)
val extensionConfiguration = resolveExtensionConfiguration(extension, variant)

configureVariantTask(uploadTask, apiKey, flavorName, extensionConfiguration, variant)

val outputsDir = File(target.buildDir, "outputs")
uploadTask.mappingFilePath =
resolveMappingFilePath(extensionConfiguration, outputsDir, variant)
uploadTask.mappingFilePath = resolveMappingFilePath(extensionConfiguration, target, variant)
uploadTask.mappingFilePackagesAliases =
filterMappingFileReplacements(
extensionConfiguration.mappingFilePackageAliases,
Expand All @@ -108,9 +108,7 @@ class DdAndroidGradlePlugin @Inject constructor(
uploadTask.datadogCiFile = findDatadogCiFile(target.projectDir)
}

val reportsDir = File(outputsDir, "reports")
val datadogDir = File(reportsDir, "datadog")
uploadTask.repositoryFile = File(datadogDir, "repository.json")
uploadTask.repositoryFile = resolveDatadogRepositoryFile(target)

val roots = mutableListOf<File>()
variant.sourceSets.forEach {
Expand Down Expand Up @@ -187,19 +185,27 @@ class DdAndroidGradlePlugin @Inject constructor(

private fun resolveMappingFilePath(
extensionConfiguration: DdExtensionConfiguration,
outputsDir: File,
target: Project,
variant: ApplicationVariant
): String {
val customPath = extensionConfiguration.mappingFilePath
return if (customPath != null) {
customPath
} else {
val outputsDir = File(target.buildDir, "outputs")
val mappingDir = File(outputsDir, "mapping")
val flavorDir = File(mappingDir, variant.name)
File(flavorDir, "mapping.txt").path
}
}

private fun resolveDatadogRepositoryFile(target: Project): File {
val outputsDir = File(target.buildDir, "outputs")
val reportsDir = File(outputsDir, "reports")
val datadogDir = File(reportsDir, "datadog")
return File(datadogDir, "repository.json")
}

private fun filterMappingFileReplacements(
replacements: Map<String, String>,
applicationId: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ open class DdExtensionConfiguration(
*/
var mappingFileTrimIndents: Boolean = false

/**
* This property declares that the obfuscation technology used is not the default
* R8/Proguard included in the Android toolchain (e.g.: Dexguard, …).
* Doing so will create an upload task for all variants and all build types
*/
var nonDefaultObfuscation: Boolean = false

/**
* Ignore the config declared in `datadog-ci.json` file if found.
*/
Expand All @@ -98,5 +105,6 @@ open class DdExtensionConfiguration(
mappingFilePackageAliases = config.mappingFilePackageAliases
mappingFileTrimIndents = config.mappingFileTrimIndents
ignoreDatadogCiFileConfig = config.ignoreDatadogCiFileConfig
nonDefaultObfuscation = config.nonDefaultObfuscation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
private lateinit var libModuleMainSrcDir: File
private lateinit var libModuleKotlinSourcesDir: File
private lateinit var settingsFile: File
private lateinit var localPropertiesFile: File
private lateinit var appBuildGradleFile: File
private lateinit var libModuleBuildGradleFile: File
private lateinit var appManifestFile: File
Expand Down Expand Up @@ -69,6 +70,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
appRootDir = File(testProjectDir, "samples/app").apply { mkdirs() }
libModuleRootDir = File(testProjectDir, "samples/lib-module").apply { mkdirs() }
settingsFile = File(testProjectDir, "settings.gradle")
localPropertiesFile = File(testProjectDir, "local.properties")
gradlePropertiesFile = File(testProjectDir, "gradle.properties")
appMainSrcDir = File(appRootDir, "src/main").apply { mkdirs() }
appKotlinSourcesDir = File(appMainSrcDir, "kotlin").apply { mkdirs() }
Expand All @@ -86,6 +88,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// compatible)
val agpVersion = forge.anElementFrom(OLD_AGP_VERSION, LATEST_AGP_VERSION)
stubFile(settingsFile, SETTINGS_FILE_CONTENT.format(Locale.US, agpVersion))
stubFile(localPropertiesFile, "sdk.dir=${System.getenv("ANDROID_HOME")}")
stubFile(sampleApplicationClassFile, APPLICATION_CLASS_CONTENT)
stubFile(appManifestFile, APP_MANIFEST_FILE_CONTENT)
stubFile(gradlePropertiesFile, GRADLE_PROPERTIES_FILE_CONTENT)
Expand Down Expand Up @@ -117,7 +120,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand All @@ -136,7 +139,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand Down Expand Up @@ -175,7 +178,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleDebug")
.withArguments("--info", ":samples:app:assembleDebug")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand All @@ -194,7 +197,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleDebug")
.withArguments("--info", ":samples:app:assembleDebug")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand All @@ -203,6 +206,45 @@ internal class DdAndroidGradlePluginFunctionalTest {
.isEqualTo(TaskOutcome.SUCCESS)
}

@Test
fun `M success W assembleDebug { non default obfuscation }`() {
// Given
stubGradleBuildFromResourceFile(
"build_with_non_default_obfuscation.gradle",
appBuildGradleFile
)
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments("--info", ":samples:app:assembleDebug")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

// Then
assertThat(result.task(":samples:app:assembleDebug")?.outcome)
.isEqualTo(TaskOutcome.SUCCESS)
}

@Test
fun `M success W assembleDebug { plugin disabled }`() {
// Given
stubGradleBuildFromResourceFile(
"build_with_plugin_disabled.gradle",
appBuildGradleFile
)
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments("--info", ":samples:app:assembleDebug")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

// Then
assertThat(result.task(":samples:app:assembleDebug")?.outcome)
.isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.output).contains("Datadog extension disabled, no upload task created")
}

@Test
fun `M success W assembleDebug { build cache }`() {
// Given
Expand Down Expand Up @@ -340,7 +382,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand All @@ -366,7 +408,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleDebug")
.withArguments("--info", ":samples:app:assembleDebug")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand All @@ -393,7 +435,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.buildAndFail()
}
Expand All @@ -410,7 +452,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleDebug")
.withArguments("--info", ":samples:app:assembleDebug")
.withPluginClasspath(getTestConfigurationClasspath())
.buildAndFail()
}
Expand All @@ -427,7 +469,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand All @@ -448,7 +490,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleDebug")
.withArguments("--info", ":samples:app:assembleDebug")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand Down Expand Up @@ -480,13 +522,13 @@ internal class DdAndroidGradlePluginFunctionalTest {
// line, so we do the explicit split
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(taskName, "--info", "-PDD_API_KEY=fakekey")
.withArguments(taskName, "--info", "--stacktrace", "-PDD_API_KEY=fakekey")
.withPluginClasspath(getTestConfigurationClasspath())
.buildAndFail()

Expand Down Expand Up @@ -519,13 +561,13 @@ internal class DdAndroidGradlePluginFunctionalTest {
// line, so we do the explicit split
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(taskName, "--info", "-PDD_API_KEY=fakekey", "-Pdd-disable-gzip")
.withArguments(taskName, "--info", "--stacktrace", "-PDD_API_KEY=fakekey", "-Pdd-disable-gzip")
.withPluginClasspath(getTestConfigurationClasspath())
.buildAndFail()

Expand Down Expand Up @@ -567,7 +609,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// line, so we do the explicit split
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand Down Expand Up @@ -605,13 +647,13 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(taskName, "--info", "-PDD_API_KEY=fakekey")
.withArguments(taskName, "--info", "--stacktrace", "-PDD_API_KEY=fakekey")
.withPluginClasspath(getTestConfigurationClasspath())
.buildAndFail()

Expand Down Expand Up @@ -643,13 +685,13 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(taskName, "--info", "-PDD_API_KEY=fakekey")
.withArguments(taskName, "--info", "--stacktrace", "-PDD_API_KEY=fakekey")
.withPluginClasspath(getTestConfigurationClasspath())
.buildAndFail()

Expand Down Expand Up @@ -687,13 +729,13 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:assembleRelease")
.withArguments("--info", ":samples:app:assembleRelease")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(taskName, "--info", "-PDD_API_KEY=fakekey")
.withArguments(taskName, "--info", "--stacktrace", "-PDD_API_KEY=fakekey")
.withPluginClasspath(getTestConfigurationClasspath())
.buildAndFail()

Expand All @@ -712,7 +754,7 @@ internal class DdAndroidGradlePluginFunctionalTest {
// When
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments(":samples:app:tasks")
.withArguments("--info", ":samples:app:tasks")
.withPluginClasspath(getTestConfigurationClasspath())
.build()

Expand Down
Loading

0 comments on commit 5e2931f

Please sign in to comment.