From bf1acca62f73d6da380796e0f7664445e5b6b95a Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Fri, 21 Jun 2024 11:15:49 +0200 Subject: [PATCH] Fix source context with configuration cache on AGP 8+ (#725) * Fix source context with configuration cache on AGP 8+ * Changelog * ktlint --- CHANGELOG.md | 6 +++++ .../io/sentry/gradle/common/JavaVariant.kt | 4 ++-- .../io/sentry/gradle/common/SentryVariant.kt | 2 +- .../io/sentry/android/gradle/AGP70Compat.kt | 4 ++-- .../io/sentry/android/gradle/AGP74Compat.kt | 22 +++++++++++-------- .../SentryPluginSourceContextTest.kt | 8 ++++++- 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eae97d62..9c09bad4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Fix source bundles with configuration cache on AGP 8+ ([#725](https://github.com/getsentry/sentry-android-gradle-plugin/pull/725)) + ## 4.8.0 ### Fixes diff --git a/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/JavaVariant.kt b/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/JavaVariant.kt index 531b1da9..28bab303 100644 --- a/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/JavaVariant.kt +++ b/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/JavaVariant.kt @@ -38,7 +38,7 @@ data class JavaVariant( projectDir.dir(javaDir.absolutePath) } } - javaDirs.filterBuildConfig().toSet() - }.zip(additionalSources) { javaKotlin, other -> javaKotlin + other } + (javaDirs + additionalSources.get()).filterBuildConfig().toSet() + } } } diff --git a/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/SentryVariant.kt b/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/SentryVariant.kt index e4a800b7..1ac4378d 100644 --- a/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/SentryVariant.kt +++ b/plugin-build/common/src/main/kotlin/io/sentry/gradle/common/SentryVariant.kt @@ -28,7 +28,7 @@ interface SentryVariant { ): Provider> } -fun List.filterBuildConfig(): List = +fun Collection.filterBuildConfig(): Collection = filterNot { // consider also AGP buildConfig folder as well as community plugins: // https://github.com/yshrsmz/BuildKonfig/blob/727f4f9e79e6726ab9489499ec6d92b6f6d56266/buildkonfig-gradle-plugin/src/main/kotlin/com/codingfeline/buildkonfig/gradle/BuildKonfigPlugin.kt#L47 diff --git a/plugin-build/src/agp70/kotlin/io/sentry/android/gradle/AGP70Compat.kt b/plugin-build/src/agp70/kotlin/io/sentry/android/gradle/AGP70Compat.kt index c99fd4a8..0548062c 100644 --- a/plugin-build/src/agp70/kotlin/io/sentry/android/gradle/AGP70Compat.kt +++ b/plugin-build/src/agp70/kotlin/io/sentry/android/gradle/AGP70Compat.kt @@ -44,8 +44,8 @@ data class AndroidVariant70( val kotlinDirs = variant.sourceSets.flatMap { it.kotlinDirectories.map { kotlinDir -> projectDir.dir(kotlinDir.absolutePath) } } - (kotlinDirs + javaDirs).filterBuildConfig().toSet() - }.zip(additionalSources) { javaKotlin, other -> javaKotlin + other } + (kotlinDirs + javaDirs + additionalSources.get()).filterBuildConfig().toSet() + } } } diff --git a/plugin-build/src/agp74/kotlin/io/sentry/android/gradle/AGP74Compat.kt b/plugin-build/src/agp74/kotlin/io/sentry/android/gradle/AGP74Compat.kt index 820ad5cf..996dd6d1 100644 --- a/plugin-build/src/agp74/kotlin/io/sentry/android/gradle/AGP74Compat.kt +++ b/plugin-build/src/agp74/kotlin/io/sentry/android/gradle/AGP74Compat.kt @@ -51,20 +51,24 @@ data class AndroidVariant74( ): Provider> { val javaProvider = variant.sources.java?.all val kotlinProvider = variant.sources.kotlin?.all + + // we cannot use .zip to combine the sources, because of possibly variation of this bug: + // https://github.com/gradle/gradle/issues/23014, but using .map works just fine, so we just + // call .get() inside the .map, and the providers will be lazily evaluated this way. return when { javaProvider == null && kotlinProvider == null -> additionalSources - javaProvider == null -> kotlinProvider!!.zip(additionalSources) { kotlin, other -> - (kotlin + other).toSet() + javaProvider == null -> kotlinProvider!!.map { kotlin -> + (kotlin + additionalSources.get()).filterBuildConfig().toSet() } - kotlinProvider == null -> javaProvider.zip(additionalSources) { java, other -> - (java + other).toSet() + kotlinProvider == null -> javaProvider.map { java -> + (java + additionalSources.get()).filterBuildConfig().toSet() } else -> - javaProvider - .zip(kotlinProvider) { java, kotlin -> - (java + kotlin).filterBuildConfig().toSet() - } - .zip(additionalSources) { javaKotlin, other -> (javaKotlin + other).toSet() } + javaProvider.map { java -> + (java + kotlinProvider.get() + additionalSources.get()) + .filterBuildConfig() + .toSet() + } } } diff --git a/plugin-build/src/test/kotlin/io/sentry/android/gradle/integration/SentryPluginSourceContextTest.kt b/plugin-build/src/test/kotlin/io/sentry/android/gradle/integration/SentryPluginSourceContextTest.kt index fb62f4c7..2cc0185d 100644 --- a/plugin-build/src/test/kotlin/io/sentry/android/gradle/integration/SentryPluginSourceContextTest.kt +++ b/plugin-build/src/test/kotlin/io/sentry/android/gradle/integration/SentryPluginSourceContextTest.kt @@ -266,7 +266,7 @@ class SentryPluginSourceContextTest : namespace 'com.example' buildFeatures { - buildConfig false + buildConfig true } } @@ -298,6 +298,12 @@ class SentryPluginSourceContextTest : "files/_/_/com/example/Example.jvm", ktContents ) + // do not bundle build config + verifySourceBundleContents( + testProjectDir.root, + "files/_/_/com/example/BuildConfig.jvm", + "" + ) } @Test