Skip to content

Commit 711fb04

Browse files
runningcodeclaude
andauthored
fix: Remove BaseExtension usage for AGP 9 compatibility (#1212)
* fix: Remove BaseExtension usage for AGP 9 compatibility Replace `BaseExtension` (removed in AGP 9) with stable APIs that work across all supported AGP versions (7.4+): - Use `ApplicationVariant.namespace` instead of `BaseExtension.namespace` - Use `CommonExtension` instead of `BaseExtension` for telemetry - Use `CommonExtension.defaultConfig.minSdk` instead of the deprecated `minSdkVersion.apiLevel` Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: Fail fast when Snapshots is used with AGP 7.x The `variant.namespace` provider may be absent on AGP 7.x, causing a `MissingValueException` at task execution time. AGP 8.0 made namespace mandatory, so add a clear check at configuration time. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: Add changelog entry for Snapshots AGP 7.x check Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7cf4cb5 commit 711fb04

5 files changed

Lines changed: 16 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Fail fast with a clear error when Snapshots feature is used with AGP 7.x ([#1212](https://github.com/getsentry/sentry-android-gradle-plugin/pull/1212))
8+
39
## 6.8.0
410

511
### Dependencies

plugin-build/src/main/kotlin/io/sentry/android/gradle/AndroidComponentsConfig.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension
1111
import com.android.build.api.variant.ApplicationVariant
1212
import com.android.build.api.variant.HostTestBuilder.Companion.UNIT_TEST_TYPE
1313
import com.android.build.api.variant.Variant
14-
import com.android.build.gradle.BaseExtension
1514
import com.android.build.gradle.internal.utils.setDisallowChanges
1615
import io.sentry.BuildConfig
1716
import io.sentry.android.gradle.SentryPlugin.Companion.sep
@@ -469,6 +468,10 @@ private fun ApplicationVariant.configureSnapshotsTasks(
469468
sentryOrg: String?,
470469
sentryProject: String?,
471470
) {
471+
check(AgpVersions.CURRENT >= AgpVersions.VERSION_8_0_0) {
472+
"Sentry Snapshots require Android Gradle Plugin 8.0 or higher. " +
473+
"Current version: ${AgpVersions.CURRENT}"
474+
}
472475
val variant = AndroidVariant74(this)
473476
val sentryProps = getPropertiesFilePath(project, variant)
474477
val taskSuffix = name.capitalized
@@ -490,8 +493,6 @@ private fun ApplicationVariant.configureSnapshotsTasks(
490493
// Wire Paparazzi test generation and upload task when the Paparazzi plugin is applied
491494
project.pluginManager.withPlugin("app.cash.paparazzi") {
492495
if (extension.snapshots.previews.generateTests.get()) {
493-
val android = project.extensions.getByType(BaseExtension::class.java)
494-
495496
project.dependencies.add(
496497
"testImplementation",
497498
"io.github.sergio-sastre.ComposablePreviewScanner:android:${BuildConfig.ComposablePreviewScannerVersion}",
@@ -510,7 +511,6 @@ private fun ApplicationVariant.configureSnapshotsTasks(
510511
GenerateSnapshotTestsTask.register(
511512
project,
512513
extension.snapshots,
513-
android,
514514
this@configureSnapshotsTasks,
515515
paparazziMajorVersion,
516516
)

plugin-build/src/main/kotlin/io/sentry/android/gradle/snapshot/GenerateSnapshotTestsTask.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.sentry.android.gradle.snapshot
22

33
import com.android.build.api.variant.ApplicationVariant
4-
import com.android.build.gradle.BaseExtension
54
import io.sentry.android.gradle.SentryTasksProvider.capitalized
65
import io.sentry.android.gradle.extensions.SnapshotsExtension
76
import java.io.File
@@ -67,7 +66,6 @@ abstract class GenerateSnapshotTestsTask : DefaultTask() {
6766
fun register(
6867
project: Project,
6968
extension: SnapshotsExtension,
70-
android: BaseExtension,
7169
variant: ApplicationVariant,
7270
paparazziMajorVersion: Provider<Int>,
7371
): TaskProvider<GenerateSnapshotTestsTask> {
@@ -79,10 +77,9 @@ abstract class GenerateSnapshotTestsTask : DefaultTask() {
7977
task.theme.set(extension.previews.theme)
8078
task.diffThreshold.set(extension.diffThreshold)
8179
task.paparazziMajorVersion.value(paparazziMajorVersion)
82-
// Fall back to the Android namespace when the user doesn't configure packageTrees
8380
task.packageTrees.set(
84-
extension.previews.packageTrees.map { packages ->
85-
packages.ifEmpty { listOf(android.namespace!!) }
81+
extension.previews.packageTrees.zip(variant.namespace) { packages, ns ->
82+
packages.ifEmpty { listOf(ns) }
8683
}
8784
)
8885
task.outputDir.set(

plugin-build/src/main/kotlin/io/sentry/android/gradle/telemetry/SentryTelemetryService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package io.sentry.android.gradle.telemetry
44

5-
import com.android.build.gradle.BaseExtension
5+
import com.android.build.api.dsl.CommonExtension
66
import io.sentry.BuildConfig
77
import io.sentry.IHub
88
import io.sentry.ISpan
@@ -395,13 +395,13 @@ abstract class SentryTelemetryService : BuildService<None>, BuildOperationListen
395395
// extension.projectName.orNull?.let { tags.put("projectName", it) }
396396

397397
try {
398-
val android = project.extensions.findByType(BaseExtension::class.java)
398+
val android = project.extensions.findByType(CommonExtension::class.java)
399399
if (android != null) {
400400
tags.put(
401401
"coreLibraryDesugaring_enabled",
402402
android.compileOptions.isCoreLibraryDesugaringEnabled.toString(),
403403
)
404-
android.defaultConfig.minSdkVersion?.apiLevel?.let { tags.put("minSdk", it.toString()) }
404+
android.defaultConfig.minSdk?.let { tags.put("minSdk", it.toString()) }
405405
}
406406
} catch (_: Throwable) {
407407
// Android extensions may not be available (e.g. JVM plugin)

plugin-build/src/main/kotlin/io/sentry/android/gradle/util/Versions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.gradle.util.GradleVersion
77
internal object AgpVersions {
88
val CURRENT: SemVer = SemVer.parse(Version.ANDROID_GRADLE_PLUGIN_VERSION)
99
val VERSION_7_4_0: SemVer = SemVer.parse("7.4.0-rc01")
10+
val VERSION_8_0_0: SemVer = SemVer.parse("8.0.0")
1011
val VERSION_8_3_0: SemVer = SemVer.parse("8.3.0")
1112
val VERSION_9_0_0: SemVer = SemVer.parse("9.0.0")
1213

0 commit comments

Comments
 (0)