diff --git a/.claude/skills/test/SKILL.md b/.claude/skills/test/SKILL.md index 5e19619b1b2..417c75b79a9 100644 --- a/.claude/skills/test/SKILL.md +++ b/.claude/skills/test/SKILL.md @@ -46,7 +46,7 @@ Determine the Gradle test task: | Module Pattern | Test Task | |---------------|-----------| | `sentry-android-*` | `testReleaseUnitTest` | -| `sentry-compose*` | `testReleaseUnitTest` | +| `sentry-compose*` | `testAndroidHostTest` | | `*-android` | `testReleaseUnitTest` | | Everything else | `test` | diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index a947330afea..7d583d726f3 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -9,6 +9,7 @@ dependencies { implementation(libs.vanniktechMavenPublishLib) implementation(libs.asm) compileOnly(libs.kotlin.gradle.plugin) + compileOnly(libs.kotlin.abi.tools.api) compileOnly(libs.android.gradle.plugin) } diff --git a/build-logic/src/main/kotlin/io/sentry/gradle/AndroidAbiValidation.kt b/build-logic/src/main/kotlin/io/sentry/gradle/AndroidAbiValidation.kt new file mode 100644 index 00000000000..9fd96e53407 --- /dev/null +++ b/build-logic/src/main/kotlin/io/sentry/gradle/AndroidAbiValidation.kt @@ -0,0 +1,211 @@ +package io.sentry.gradle + +import com.android.build.api.artifact.ScopedArtifact +import com.android.build.api.variant.AndroidComponentsExtension +import com.android.build.api.variant.ScopedArtifacts +import com.android.build.api.variant.VariantSelector +import java.io.File +import java.util.zip.ZipFile +import javax.inject.Inject +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.Project +import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.Directory +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFile +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.ListProperty +import org.gradle.api.provider.SetProperty +import org.gradle.api.tasks.CacheableTask +import org.gradle.api.tasks.Classpath +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity +import org.gradle.api.tasks.TaskAction +import org.gradle.work.DisableCachingByDefault +import org.gradle.workers.WorkAction +import org.gradle.workers.WorkParameters +import org.gradle.workers.WorkerExecutor +import org.jetbrains.kotlin.abi.tools.AbiFilters +import org.jetbrains.kotlin.abi.tools.AbiTools +import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion + +/** + * The Kotlin Gradle plugin's ABI validation only covers the `org.jetbrains.kotlin.android` plugin + * and the legacy KMP `androidTarget`. Neither AGP's built-in Kotlin nor the KMP Android library + * plugin gets it, so their modules would silently stop checking their ABI. This dumps the classes + * of one variant with the same ABI tools the Kotlin Gradle plugin uses, into the dump file it used + * to write, and hooks the check and update into `checkKotlinAbi` and `updateKotlinAbi`. + */ +internal fun Project.validateAndroidAbi( + extension: ApiValidationExtension, + referenceDump: String, + selectVariant: (VariantSelector) -> VariantSelector, +) { + val abiTools = + configurations.detachedConfiguration( + dependencies.create("org.jetbrains.kotlin:abi-tools:${getKotlinPluginVersion()}") + ) + val dump = + tasks.register("dumpAndroidAbi", DumpAndroidAbi::class.java) { + description = "Dumps the public ABI of the Android classes." + classpath.from(abiTools) + ignoredPackages.set(extension.ignoredPackages) + dumpFile.set(layout.buildDirectory.file("kotlin/abi-android/${project.name}.api")) + } + extensions.getByType(AndroidComponentsExtension::class.java).onVariants( + selectVariant(extensions.getByType(AndroidComponentsExtension::class.java).selector()) + ) { variant -> + variant.artifacts + .forScope(ScopedArtifacts.Scope.PROJECT) + .use(dump) + .toGet(ScopedArtifact.CLASSES, DumpAndroidAbi::classJars, DumpAndroidAbi::classDirs) + } + + val check = + tasks.register("checkAndroidAbi", CheckAndroidAbi::class.java) { + description = "Checks the public ABI of the Android classes against $referenceDump." + group = "verification" + classpath.from(abiTools) + actual.set(dump.flatMap { it.dumpFile }) + expected.set(layout.projectDirectory.file(referenceDump)) + } + val update = + tasks.register("updateAndroidAbi", UpdateAndroidAbi::class.java) { + description = "Writes the public ABI of the Android classes to $referenceDump." + actual.set(dump.flatMap { it.dumpFile }) + expected.set(layout.projectDirectory.file(referenceDump)) + } + tasks.maybeRegister("checkKotlinAbi").configure { dependsOn(check) } + tasks.maybeRegister("updateKotlinAbi").configure { dependsOn(update) } + tasks.named("check").configure { dependsOn(check) } +} + +private fun org.gradle.api.tasks.TaskContainer.maybeRegister(name: String) = + if (name in names) named(name) else register(name) + +@CacheableTask +abstract class DumpAndroidAbi @Inject constructor(private val workers: WorkerExecutor) : + DefaultTask() { + @get:Classpath abstract val classpath: ConfigurableFileCollection + + @get:Classpath abstract val classJars: ListProperty + + @get:Classpath abstract val classDirs: ListProperty + + @get:Input abstract val ignoredPackages: SetProperty + + @get:OutputFile abstract val dumpFile: RegularFileProperty + + @TaskAction + fun dump() { + workers + .classLoaderIsolation { classpath.from(this@DumpAndroidAbi.classpath) } + .submit(DumpAbiAction::class.java) { + classJars.from(this@DumpAndroidAbi.classJars) + classDirs.from(this@DumpAndroidAbi.classDirs) + excludedClasses.set(ignoredPackages.map { packages -> packages.map { "$it.**" }.toSet() }) + unpackDir.set(temporaryDir) + dumpFile.set(this@DumpAndroidAbi.dumpFile) + } + } +} + +@DisableCachingByDefault(because = "Comparing two small files is cheaper than caching it") +abstract class CheckAndroidAbi @Inject constructor(private val workers: WorkerExecutor) : + DefaultTask() { + @get:Classpath abstract val classpath: ConfigurableFileCollection + + @get:InputFiles @get:PathSensitive(PathSensitivity.NONE) abstract val actual: RegularFileProperty + + // InputFiles rather than InputFile, so that a missing dump fails with the message below. + @get:InputFiles + @get:PathSensitive(PathSensitivity.NONE) + abstract val expected: RegularFileProperty + + @TaskAction + fun check() { + val expectedFile = expected.get().asFile + if (!expectedFile.exists()) { + throw GradleException( + "The ABI dump ${expectedFile.path} is missing. Run updateKotlinAbi to create it." + ) + } + workers + .classLoaderIsolation { classpath.from(this@CheckAndroidAbi.classpath) } + .submit(CheckAbiAction::class.java) { + expected.set(expectedFile) + actual.set(this@CheckAndroidAbi.actual) + } + } +} + +@DisableCachingByDefault(because = "Copies one file into the source tree") +abstract class UpdateAndroidAbi : DefaultTask() { + @get:InputFiles @get:PathSensitive(PathSensitivity.NONE) abstract val actual: RegularFileProperty + + @get:OutputFile abstract val expected: RegularFileProperty + + @TaskAction + fun update() { + actual.get().asFile.copyTo(expected.get().asFile, overwrite = true) + } +} + +interface DumpAbiParameters : WorkParameters { + val classJars: ConfigurableFileCollection + val classDirs: ConfigurableFileCollection + val excludedClasses: SetProperty + val unpackDir: DirectoryProperty + val dumpFile: RegularFileProperty +} + +/** The ABI tools read class files only, so jars are unpacked first. */ +abstract class DumpAbiAction : WorkAction { + override fun execute() { + val unpackDir = parameters.unpackDir.get().asFile + unpackDir.deleteRecursively() + parameters.classJars.forEachIndexed { index, jar -> unpack(jar, File(unpackDir, "$index")) } + + val roots = parameters.classDirs.files + unpackDir + val classFiles = + roots.flatMap { root -> root.walk().filter { it.isFile && it.extension == "class" } } + val filters = AbiFilters(emptySet(), parameters.excludedClasses.get(), emptySet(), emptySet()) + parameters.dumpFile.get().asFile.bufferedWriter().use { + AbiTools.getInstance().printJvmDump(it, classFiles, filters) + } + } + + private fun unpack(jar: File, into: File) { + ZipFile(jar).use { zip -> + zip + .entries() + .asSequence() + .filter { !it.isDirectory && it.name.endsWith(".class") } + .forEach { entry -> + val target = File(into, entry.name) + target.parentFile.mkdirs() + zip.getInputStream(entry).use { input -> target.outputStream().use { input.copyTo(it) } } + } + } + } +} + +interface CheckAbiParameters : WorkParameters { + val expected: RegularFileProperty + val actual: RegularFileProperty +} + +abstract class CheckAbiAction : WorkAction { + override fun execute() { + val expected = parameters.expected.get().asFile + val actual = parameters.actual.get().asFile + val diff = AbiTools.getInstance().filesDiff(expected, actual) ?: return + throw GradleException( + "The public ABI of ${expected.path} changed. Run updateKotlinAbi if that is intended.\n$diff" + ) + } +} diff --git a/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt b/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt index dd0a8e275a6..4893ce09c20 100644 --- a/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt +++ b/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt @@ -1,7 +1,10 @@ package io.sentry.gradle import com.android.build.api.artifact.SingleArtifact +import com.android.build.api.dsl.KotlinMultiplatformAndroidHostTestCompilation +import com.android.build.api.dsl.KotlinMultiplatformAndroidLibraryTarget import com.android.build.api.dsl.LibraryExtension +import com.android.build.api.variant.KotlinMultiplatformAndroidComponentsExtension import com.android.build.api.variant.LibraryAndroidComponentsExtension import org.gradle.api.JavaVersion import org.gradle.api.Plugin @@ -12,9 +15,7 @@ import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.tasks.compile.JavaCompile import org.gradle.jvm.toolchain.JavaLanguageVersion import org.jetbrains.kotlin.gradle.dsl.JvmTarget -import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile /** The JDK every module compiles and tests with, whatever JDK runs Gradle. */ @@ -27,7 +28,9 @@ private val PUBLISHED_JAVA_VERSION = JavaVersion.VERSION_11 * Holds every toolchain, bytecode level decision and compilation option of a module. It does the * following * - sample apps target the build JDK (21) - * - unit test code will target the build JDK (21) (required for Mockito 5) + * - JVM and KMP unit test code targets the build JDK (21). Android unit tests share the main code's + * target, because AGP's built-in Kotlin compiles every component of a module for its single + * compileOptions target * - published JVM modules main code pins the Java 11 release, for Java and for Kotlin * - published modules Java code drops the MethodParameters attribute javac 21 adds, and their * archives are checked for it @@ -81,6 +84,15 @@ class JdkTargetConvention : Plugin { } } } + project.plugins.withId("com.android.kotlin.multiplatform.library") { + project.extensions + .getByType(KotlinMultiplatformAndroidComponentsExtension::class.java) + .onVariants { variant -> + verifyMethodParameters.configure { + archives.from(variant.artifacts.get(SingleArtifact.AAR)) + } + } + } // Only works for non-android modules // See https://issuetracker.google.com/issues/278800528 project.plugins.withId("java-library") { @@ -109,8 +121,21 @@ class JdkTargetConvention : Plugin { android.defaultConfig { aarMetadata { minCompileSdk = project.minSdk() } } } - listOf("com.android.library", "com.android.application").forEach { pluginId -> - project.plugins.withId(pluginId) { project.targetBuildJdkForUnitTests() } + project.plugins.withId("com.android.kotlin.multiplatform.library") { + project.extensions + .getByType(KotlinMultiplatformExtension::class.java) + .targets + .withType(KotlinMultiplatformAndroidLibraryTarget::class.java) + .configureEach { + aarMetadata { minCompileSdk = project.minSdk() } + compilations + .withType(KotlinMultiplatformAndroidHostTestCompilation::class.java) + .configureEach { + compileTaskProvider.configure { + compilerOptions.jvmTarget.set(JvmTarget.fromTarget(BUILD_JDK.toString())) + } + } + } } } @@ -122,37 +147,4 @@ class JdkTargetConvention : Plugin { .orElseThrow { IllegalStateException("libs.versions.minSdk is missing") } .requiredVersion .toInt() - - /** - * Test code - which is never published - targets the build JDK, as it does in the JVM modules, - * instead of the Java 11 target of the main code. AGP has no per variant compileOptions, so this - * goes through the Kotlin compilations: their compile task providers are configured after AGP - * wrote its own compileOptions, which an apply time configureEach would lose against. - */ - private fun Project.targetBuildJdkForUnitTests() { - plugins.withId("org.jetbrains.kotlin.android") { - extensions.getByType(KotlinAndroidProjectExtension::class.java).target.targetBuildJdk() - } - plugins.withId("org.jetbrains.kotlin.multiplatform") { - extensions - .getByType(KotlinMultiplatformExtension::class.java) - .targets - .withType(KotlinAndroidTarget::class.java) - .configureEach { targetBuildJdk() } - } - } - - private fun KotlinAndroidTarget.targetBuildJdk() { - compilations.configureEach { - if (name.endsWith("UnitTest")) { - compileTaskProvider.configure { - compilerOptions.jvmTarget.set(JvmTarget.fromTarget(BUILD_JDK.toString())) - } - compileJavaTaskProvider?.configure { - sourceCompatibility = BUILD_JDK.toString() - targetCompatibility = BUILD_JDK.toString() - } - } - } - } } diff --git a/build-logic/src/main/kotlin/io/sentry/gradle/SentryApiValidationPlugin.kt b/build-logic/src/main/kotlin/io/sentry/gradle/SentryApiValidationPlugin.kt index f4c1dca5948..08bea1b36d7 100644 --- a/build-logic/src/main/kotlin/io/sentry/gradle/SentryApiValidationPlugin.kt +++ b/build-logic/src/main/kotlin/io/sentry/gradle/SentryApiValidationPlugin.kt @@ -12,13 +12,13 @@ import org.jetbrains.kotlin.gradle.dsl.abi.ExperimentalAbiValidation private val KOTLIN_PLUGIN_IDS = listOf( "org.jetbrains.kotlin.jvm", - "org.jetbrains.kotlin.android", "org.jetbrains.kotlin.multiplatform", ) /** * Checks the public ABI of a module against the dump checked into its `api` directory, using the - * Kotlin Gradle plugin's own ABI validation. Adds `checkKotlinAbi`, which `check` depends on, and + * Kotlin Gradle plugin's own ABI validation, or [validateAndroidAbi] for Android modules, which it + * doesn't cover. Adds `checkKotlinAbi`, which `check` depends on, and * `updateKotlinAbi`, which rewrites the dump. */ @OptIn(ExperimentalAbiValidation::class) @@ -31,6 +31,14 @@ class SentryApiValidationPlugin : Plugin { KOTLIN_PLUGIN_IDS.forEach { project.pluginManager.withPlugin(it) { enable(project, extension) } } + project.pluginManager.withPlugin("com.android.kotlin.multiplatform.library") { + project.validateAndroidAbi(extension, "api/android/${project.name}.api") { it } + } + project.pluginManager.withPlugin("com.android.library") { + project.validateAndroidAbi(extension, "api/${project.name}.api") { + it.withBuildType("release") + } + } } private fun enable(project: Project, extension: ApiValidationExtension) { diff --git a/build-logic/src/main/kotlin/io/sentry/gradle/SentryRobolectricPlugin.kt b/build-logic/src/main/kotlin/io/sentry/gradle/SentryRobolectricPlugin.kt index a996574f7da..1504d286a7c 100644 --- a/build-logic/src/main/kotlin/io/sentry/gradle/SentryRobolectricPlugin.kt +++ b/build-logic/src/main/kotlin/io/sentry/gradle/SentryRobolectricPlugin.kt @@ -15,6 +15,7 @@ import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction +import org.gradle.api.tasks.testing.Test import org.gradle.process.CommandLineArgumentProvider import org.gradle.work.DisableCachingByDefault @@ -69,18 +70,30 @@ class SentryRobolectricPlugin : Plugin { propertiesFile.set(project.layout.buildDirectory.file("robolectric/deps.properties")) } + val useGradleCachedJars: (Test) -> Unit = { test -> + // The properties file holds absolute paths, so it can't be a test input without breaking + // build cache hits across machines. The jars are the input instead. + test.dependsOn(writeDeps) + test.jvmArgumentProviders.add( + project.objects.newInstance(RobolectricDepsArgument::class.java).apply { + androidAllJars.from(coordinatesToJar.values) + propertiesFile.set(writeDeps.flatMap { it.propertiesFile }) + } + ) + } + project.plugins.withId("com.android.library") { - project.extensions.getByType(LibraryExtension::class.java).testOptions.unitTests.all { test -> - // The properties file holds absolute paths, so it can't be a test input without breaking - // build cache hits across machines. The jars are the input instead. - test.dependsOn(writeDeps) - test.jvmArgumentProviders.add( - project.objects.newInstance(RobolectricDepsArgument::class.java).apply { - androidAllJars.from(coordinatesToJar.values) - propertiesFile.set(writeDeps.flatMap { it.propertiesFile }) - } - ) - } + project.extensions + .getByType(LibraryExtension::class.java) + .testOptions + .unitTests + .all(useGradleCachedJars) + } + project.plugins.withId("com.android.kotlin.multiplatform.library") { + project.tasks + .withType(Test::class.java) + .matching { it.name == "testAndroidHostTest" } + .configureEach(useGradleCachedJars) } } } diff --git a/build.gradle.kts b/build.gradle.kts index b4d99e20543..ba37a66a478 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,6 @@ plugins { alias(libs.plugins.spotless) apply false alias(libs.plugins.detekt) `maven-publish` - alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.kotlin.multiplatform) apply false alias(libs.plugins.kotlin.jvm) apply false alias(libs.plugins.kotlin.spring) apply false @@ -59,6 +58,10 @@ tasks.register("buildForCodeQL") { proj.tasks.findByName("compileReleaseUnitTestSources")?.let { testTask -> this.dependsOn(testTask) } + } else if (proj.plugins.hasPlugin("com.android.kotlin.multiplatform.library")) { + proj.tasks.findByName("compileAndroidHostTest")?.let { testTask -> + this.dependsOn(testTask) + } } else { proj.tasks.findByName("testClasses")?.let { testTask -> this.dependsOn(testTask) diff --git a/detekt.yml b/detekt.yml index e8a01a87135..0416800fc2a 100644 --- a/detekt.yml +++ b/detekt.yml @@ -1,16 +1,59 @@ +# detekt's defaults exclude test sources from these rules, but its list predates the KMP Android +# library plugin's androidHostTest source set. A rule's excludes replace the default list, so this +# repeats it with androidHostTest added. +comments: + KDocReferencesNonPublicProperty: + excludes: &testSources + - '**/test/**' + - '**/androidTest/**' + - '**/commonTest/**' + - '**/jvmTest/**' + - '**/androidUnitTest/**' + - '**/androidHostTest/**' + - '**/androidInstrumentedTest/**' + - '**/jsTest/**' + - '**/iosTest/**' + UndocumentedPublicClass: + excludes: *testSources + UndocumentedPublicFunction: + excludes: *testSources + UndocumentedPublicProperty: + excludes: *testSources +complexity: + StringLiteralDuplication: + excludes: *testSources + TooManyFunctions: + excludes: *testSources + ignoreAnnotatedFunctions: ['Preview'] +exceptions: + InstanceOfCheckForException: + excludes: *testSources + ThrowingExceptionsWithoutMessageOrCause: + excludes: *testSources + TooGenericExceptionCaught: + excludes: *testSources +naming: + FunctionNaming: + excludes: *testSources + ignoreAnnotated: ['Composable'] + TopLevelPropertyNaming: + # Accept both SCREAMING_CASE and Compose's PascalCase constants. + constantPattern: '[A-Z][_A-Za-z0-9]*' +performance: + ForEachOnRange: + excludes: *testSources + SpreadOperator: + excludes: *testSources +potential-bugs: + LateinitUsage: + excludes: *testSources + UnsafeCallOnNullableType: + excludes: *testSources style: ReturnCount: max: 3 MagicNumber: + excludes: *testSources ignorePropertyDeclaration: true UnusedPrivateMember: ignoreAnnotated: ['Preview'] -naming: - FunctionNaming: - ignoreAnnotated: ['Composable'] - TopLevelPropertyNaming: - # Accept both SCREAMING_CASE and Compose's PascalCase constants. - constantPattern: '[A-Z][_A-Za-z0-9]*' -complexity: - TooManyFunctions: - ignoreAnnotatedFunctions: ['Preview'] diff --git a/gradle.properties b/gradle.properties index ea37b9864f6..e82ce850bc2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,8 +11,7 @@ org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled # AndroidX required by AGP >= 3.6.x android.useAndroidX=true -# AGP 9+ migration opt-outs until we remove kotlin-android plugin and adopt built-in Kotlin. -android.builtInKotlin=false +# AGP 9+ migration opt-out until build-logic moves to the new DSL and Variant API. android.newDsl=false android.disableAgpUpgradePrompt=true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7b3a04b0ea0..97bb421ccf8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -65,7 +65,6 @@ minSdk = "24" [plugins] android-application = { id = "com.android.application", version.ref = "agp" } -kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } kotlin-spring = { id = "org.jetbrains.kotlin.plugin.spring", version.ref = "kotlin" } kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } @@ -170,6 +169,7 @@ leakcanary = { module = "com.squareup.leakcanary:leakcanary-android", version = lottie-compose = { module = "com.airbnb.android:lottie-compose", version = "6.7.1" } logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } +kotlin-abi-tools-api = { module = "org.jetbrains.kotlin:abi-tools-api", version.ref = "kotlin" } android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref = "agp" } asm = { module = "org.ow2.asm:asm", version = "9.9" } nopen-annotations = { module = "com.jakewharton.nopen:nopen-annotations", version.ref = "nopen" } diff --git a/sentry-android-core/build.gradle.kts b/sentry-android-core/build.gradle.kts index 22613df021f..9c146608189 100644 --- a/sentry-android-core/build.gradle.kts +++ b/sentry-android-core/build.gradle.kts @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11 plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) alias(libs.plugins.errorprone) alias(libs.plugins.gradle.versions) diff --git a/sentry-android-distribution/build.gradle.kts b/sentry-android-distribution/build.gradle.kts index cd4078eded8..da3c5e6ed0d 100644 --- a/sentry-android-distribution/build.gradle.kts +++ b/sentry-android-distribution/build.gradle.kts @@ -3,7 +3,6 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) id("io.sentry.detekt") id("io.sentry.spotless") id("io.sentry.jdk-target") diff --git a/sentry-android-fragment/build.gradle.kts b/sentry-android-fragment/build.gradle.kts index f83470a4fdc..7a7903e4b28 100644 --- a/sentry-android-fragment/build.gradle.kts +++ b/sentry-android-fragment/build.gradle.kts @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.detekt") id("io.sentry.spotless") diff --git a/sentry-android-integration-tests/build.gradle.kts b/sentry-android-integration-tests/build.gradle.kts index 19c71e99e5d..d722feb9abd 100644 --- a/sentry-android-integration-tests/build.gradle.kts +++ b/sentry-android-integration-tests/build.gradle.kts @@ -9,7 +9,6 @@ buildscript { plugins { alias(libs.plugins.android.application) apply false - alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.kotlin.compose) apply false alias(libs.plugins.errorprone) apply false alias(libs.plugins.detekt) apply false diff --git a/sentry-android-integration-tests/sentry-uitest-android-critical/build.gradle.kts b/sentry-android-integration-tests/sentry-uitest-android-critical/build.gradle.kts index fc43a6ad062..ad50d73f428 100644 --- a/sentry-android-integration-tests/sentry-uitest-android-critical/build.gradle.kts +++ b/sentry-android-integration-tests/sentry-uitest-android-critical/build.gradle.kts @@ -3,7 +3,6 @@ import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion plugins { id("com.android.application") - alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) id("io.sentry.spotless") id("io.sentry.jdk-target") diff --git a/sentry-android-integration-tests/sentry-uitest-android/build.gradle.kts b/sentry-android-integration-tests/sentry-uitest-android/build.gradle.kts index 557a5fab63a..4b50894aa50 100644 --- a/sentry-android-integration-tests/sentry-uitest-android/build.gradle.kts +++ b/sentry-android-integration-tests/sentry-uitest-android/build.gradle.kts @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion plugins { id("com.android.application") - alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) alias(libs.plugins.errorprone) alias(libs.plugins.gradle.versions) diff --git a/sentry-android-integration-tests/test-app-size/build.gradle.kts b/sentry-android-integration-tests/test-app-size/build.gradle.kts index 678ae4103e0..a62a3c5eb27 100644 --- a/sentry-android-integration-tests/test-app-size/build.gradle.kts +++ b/sentry-android-integration-tests/test-app-size/build.gradle.kts @@ -9,6 +9,8 @@ val sentryVersion = libs.versions.sentry.get() android { namespace = "io.sentry.tests.size" + // No Kotlin sources, and the size measurement shouldn't include the Kotlin stdlib. + enableKotlin = false compileSdk = libs.versions.compileSdk.get().toInt() compileSdkMinor = libs.versions.compileSdkMinor.get().toInt() diff --git a/sentry-android-navigation/build.gradle.kts b/sentry-android-navigation/build.gradle.kts index a9e75743b05..87041cf8025 100644 --- a/sentry-android-navigation/build.gradle.kts +++ b/sentry-android-navigation/build.gradle.kts @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.detekt") id("io.sentry.spotless") diff --git a/sentry-android-navigation3/build.gradle.kts b/sentry-android-navigation3/build.gradle.kts index 9478825b347..9fb9c0c6e06 100644 --- a/sentry-android-navigation3/build.gradle.kts +++ b/sentry-android-navigation3/build.gradle.kts @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) alias(libs.plugins.gradle.versions) id("io.sentry.detekt") diff --git a/sentry-android-ndk-outproc/build.gradle.kts b/sentry-android-ndk-outproc/build.gradle.kts index bb8fd5151a4..9302e92cf25 100644 --- a/sentry-android-ndk-outproc/build.gradle.kts +++ b/sentry-android-ndk-outproc/build.gradle.kts @@ -2,7 +2,6 @@ import org.jetbrains.kotlin.config.KotlinCompilerVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.spotless") id("io.sentry.jdk-target") @@ -36,7 +35,7 @@ android { // disabled, so unit tests must target release to run at all. testBuildType = "release" - kotlin { compilerOptions.jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8 } + kotlin { compilerOptions.jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11 } testOptions { animationsDisabled = true diff --git a/sentry-android-ndk/build.gradle.kts b/sentry-android-ndk/build.gradle.kts index 5627cf47d82..3d4cdc9e0bd 100644 --- a/sentry-android-ndk/build.gradle.kts +++ b/sentry-android-ndk/build.gradle.kts @@ -2,7 +2,6 @@ import org.jetbrains.kotlin.config.KotlinCompilerVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.spotless") id("io.sentry.jdk-target") diff --git a/sentry-android-replay/build.gradle.kts b/sentry-android-replay/build.gradle.kts index 912e85075a6..bc1c6c7d388 100644 --- a/sentry-android-replay/build.gradle.kts +++ b/sentry-android-replay/build.gradle.kts @@ -5,7 +5,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) alias(libs.plugins.gradle.versions) id("io.sentry.detekt") diff --git a/sentry-android-sqlite/build.gradle.kts b/sentry-android-sqlite/build.gradle.kts index 4c91c7bb59a..4002076349c 100644 --- a/sentry-android-sqlite/build.gradle.kts +++ b/sentry-android-sqlite/build.gradle.kts @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.detekt") id("io.sentry.spotless") diff --git a/sentry-android-timber/build.gradle.kts b/sentry-android-timber/build.gradle.kts index ef19cfac00a..1db56631458 100644 --- a/sentry-android-timber/build.gradle.kts +++ b/sentry-android-timber/build.gradle.kts @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.detekt") id("io.sentry.spotless") diff --git a/sentry-android/build.gradle.kts b/sentry-android/build.gradle.kts index 1f6b68f5d81..fc07acb7504 100644 --- a/sentry-android/build.gradle.kts +++ b/sentry-android/build.gradle.kts @@ -1,6 +1,5 @@ plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.spotless") id("io.sentry.jdk-target") diff --git a/sentry-compose/api/android/sentry-compose.api b/sentry-compose/api/android/sentry-compose.api index c74f96163b3..a56677f64ae 100644 --- a/sentry-compose/api/android/sentry-compose.api +++ b/sentry-compose/api/android/sentry-compose.api @@ -1,11 +1,3 @@ -public final class io/sentry/compose/BuildConfig { - public static final field BUILD_TYPE Ljava/lang/String; - public static final field DEBUG Z - public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String; - public static final field VERSION_NAME Ljava/lang/String; - public fun ()V -} - public final class io/sentry/compose/LocalSentrySpanKt { public static final fun getLocalSentrySpan ()Landroidx/compose/runtime/ProvidableCompositionLocal; } diff --git a/sentry-compose/build.gradle.kts b/sentry-compose/build.gradle.kts index 0c5d585989a..4f8aeb5cf4a 100644 --- a/sentry-compose/build.gradle.kts +++ b/sentry-compose/build.gradle.kts @@ -6,7 +6,8 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { alias(libs.plugins.kotlin.multiplatform) alias(libs.plugins.kotlin.compose) - id("com.android.library") + id("com.android.kotlin.multiplatform.library") + id("com.android.lint") alias(libs.plugins.gradle.versions) id("io.sentry.detekt") alias(libs.plugins.dokka) @@ -19,16 +20,67 @@ plugins { id("io.sentry.robolectric") } +// The KMP Android library plugin has no BuildConfig, so the version is generated as a Kotlin +// source. +val generateSentryComposeVersion = + tasks.register("generateSentryComposeVersion") { + val version = project.version.toString() + val outputDir = layout.buildDirectory.dir("generated/sentryComposeVersion") + inputs.property("version", version) + outputs.dir(outputDir) + doLast { + val file = outputDir.get().file("io/sentry/compose/SentryComposeVersion.kt").asFile + file.parentFile.mkdirs() + file.writeText( + """ + |package io.sentry.compose + | + |internal const val SENTRY_COMPOSE_VERSION_NAME: String = "$version" + |""" + .trimMargin(), + ) + } + } + kotlin { explicitApi() - androidTarget { + android { + namespace = "io.sentry.compose" + compileSdk { + version = + release(libs.versions.compileSdk.get().toInt()) { + minorApiLevel = libs.versions.compileSdkMinor.get().toInt() + } + } + minSdk = libs.versions.minSdk.get().toInt() + + androidResources { enable = true } + + optimization { + consumerKeepRules.publish = true + consumerKeepRules.file("proguard-rules.pro") + } + + withHostTest { + isReturnDefaultValues = true + isIncludeAndroidResources = true + } + + lint { + warningsAsErrors = true + checkDependencies = true + + // Lint 9.5 can't load the custom checks bundled with older androidx.lifecycle releases and + // reports that it skips them. They only cover lifecycle APIs this module doesn't call. + disable += "ObsoleteLintCustomCheck" + } + compilerOptions { jvmTarget.set(JvmTarget.JVM_11) apiVersion.set(KotlinVersion.KOTLIN_1_9) languageVersion.set(KotlinVersion.KOTLIN_1_9) } - publishLibraryVariants("release") } jvm("desktop") { compilerOptions { @@ -56,6 +108,7 @@ kotlin { } } getByName("androidMain") { + kotlin.srcDir(generateSentryComposeVersion) dependencies { api(projects.sentry) api(projects.sentryAndroidNavigation) @@ -65,7 +118,7 @@ kotlin { implementation(libs.androidx.lifecycle.common.java8) } } - getByName("androidUnitTest") { + getByName("androidHostTest") { dependencies { implementation(libs.androidx.compose.ui.test.junit4) implementation(libs.androidx.navigation.compose) @@ -82,56 +135,8 @@ kotlin { } } -android { - compileSdk = libs.versions.compileSdk.get().toInt() - compileSdkMinor = libs.versions.compileSdkMinor.get().toInt() - namespace = "io.sentry.compose" - - defaultConfig { - minSdk = libs.versions.minSdk.get().toInt() - - // for AGP 4.1 - buildConfigField("String", "VERSION_NAME", "\"${project.version}\"") - } - - buildTypes { - getByName("debug") { consumerProguardFiles("proguard-rules.pro") } - getByName("release") { consumerProguardFiles("proguard-rules.pro") } - } - - // AGP 9 only generates unit tests for the testBuildType. The debug variant is - // disabled, so unit tests must target release to run at all. - testBuildType = "release" - - testOptions { - animationsDisabled = true - unitTests.apply { - isReturnDefaultValues = true - isIncludeAndroidResources = true - } - } - - lint { - warningsAsErrors = true - checkDependencies = true - - // We run a full lint analysis as build part in CI, so skip vital checks for assemble tasks. - checkReleaseBuilds = false - - // Lint 9.5 can't load the custom checks bundled with older androidx.lifecycle releases and - // reports that it skips them. They only cover lifecycle APIs this module doesn't call. - disable += "ObsoleteLintCustomCheck" - } - - buildFeatures { buildConfig = true } - - androidComponents.beforeVariants { - it.enable = !Config.Android.shouldSkipDebugVariant(it.buildType) - } -} - // The default detekt source set is src/{main,test}, which doesn't exist in a multiplatform module. -detekt { source.setFrom("src/androidMain/kotlin", "src/androidUnitTest/kotlin") } +detekt { source.setFrom("src/androidMain/kotlin", "src/androidHostTest/kotlin") } tasks.withType().configureEach { // Target version of the generated JVM bytecode. It is used for type resolution. diff --git a/sentry-compose/gradle.properties b/sentry-compose/gradle.properties index 5ad025123aa..feb4ddee322 100644 --- a/sentry-compose/gradle.properties +++ b/sentry-compose/gradle.properties @@ -1,6 +1,4 @@ kotlin.mpp.stability.nowarn=true -kotlin.mpp.androidSourceSetLayoutVersion=2 # Don't show Dokka plugin's long message on every build -kotlin.mpp.androidGradlePluginCompatibility.nowarn=true org.jetbrains.dokka.experimental.gradle.pluginMode.noWarn=true diff --git a/sentry-compose/src/androidUnitTest/kotlin/androidx/compose/foundation/GestureModifierStubs.kt b/sentry-compose/src/androidHostTest/kotlin/androidx/compose/foundation/GestureModifierStubs.kt similarity index 100% rename from sentry-compose/src/androidUnitTest/kotlin/androidx/compose/foundation/GestureModifierStubs.kt rename to sentry-compose/src/androidHostTest/kotlin/androidx/compose/foundation/GestureModifierStubs.kt diff --git a/sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/ComposeIntegrationTests.kt b/sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/ComposeIntegrationTests.kt similarity index 100% rename from sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/ComposeIntegrationTests.kt rename to sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/ComposeIntegrationTests.kt diff --git a/sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/SentryLifecycleObserverTest.kt b/sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/SentryLifecycleObserverTest.kt similarity index 100% rename from sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/SentryLifecycleObserverTest.kt rename to sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/SentryLifecycleObserverTest.kt diff --git a/sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/SentryModifierComposeTest.kt b/sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/SentryModifierComposeTest.kt similarity index 100% rename from sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/SentryModifierComposeTest.kt rename to sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/SentryModifierComposeTest.kt diff --git a/sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/SentryTracedTest.kt b/sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/SentryTracedTest.kt similarity index 100% rename from sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/SentryTracedTest.kt rename to sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/SentryTracedTest.kt diff --git a/sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocatorTest.kt b/sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocatorTest.kt similarity index 100% rename from sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocatorTest.kt rename to sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocatorTest.kt diff --git a/sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/viewhierarchy/ComposeViewHierarchyExporterTest.kt b/sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/viewhierarchy/ComposeViewHierarchyExporterTest.kt similarity index 100% rename from sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/viewhierarchy/ComposeViewHierarchyExporterTest.kt rename to sentry-compose/src/androidHostTest/kotlin/io/sentry/compose/viewhierarchy/ComposeViewHierarchyExporterTest.kt diff --git a/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryNavigationIntegration.kt b/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryNavigationIntegration.kt index 07dd300075f..b83f91dbcd2 100644 --- a/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryNavigationIntegration.kt +++ b/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryNavigationIntegration.kt @@ -28,7 +28,7 @@ internal class SentryLifecycleObserver( private companion object { init { SentryIntegrationPackageStorage.getInstance() - .addPackage("maven:io.sentry:sentry-compose", BuildConfig.VERSION_NAME) + .addPackage("maven:io.sentry:sentry-compose", SENTRY_COMPOSE_VERSION_NAME) } } diff --git a/sentry-compose/src/androidMain/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocator.kt b/sentry-compose/src/androidMain/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocator.kt index ba011e455ab..b0148fb4f37 100644 --- a/sentry-compose/src/androidMain/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocator.kt +++ b/sentry-compose/src/androidMain/kotlin/io/sentry/compose/gestures/ComposeGestureTargetLocator.kt @@ -9,7 +9,7 @@ import androidx.compose.ui.node.Owner import androidx.compose.ui.semantics.SemanticsModifier import io.sentry.ILogger import io.sentry.SentryIntegrationPackageStorage -import io.sentry.compose.BuildConfig +import io.sentry.compose.SENTRY_COMPOSE_VERSION_NAME import io.sentry.compose.SentryComposeHelper import io.sentry.compose.boundsInWindow import io.sentry.internal.gestures.GestureTargetLocator @@ -25,7 +25,7 @@ public class ComposeGestureTargetLocator(private val logger: ILogger) : GestureT init { SentryIntegrationPackageStorage.getInstance() - .addPackage("maven:io.sentry:sentry-compose", BuildConfig.VERSION_NAME) + .addPackage("maven:io.sentry:sentry-compose", SENTRY_COMPOSE_VERSION_NAME) } override fun locate(root: Any?, x: Float, y: Float, targetType: UiElement.Type): UiElement? { diff --git a/sentry-launchdarkly-android/build.gradle.kts b/sentry-launchdarkly-android/build.gradle.kts index f2d7a4b813a..7fadf6fcc29 100644 --- a/sentry-launchdarkly-android/build.gradle.kts +++ b/sentry-launchdarkly-android/build.gradle.kts @@ -1,6 +1,5 @@ plugins { id("com.android.library") - alias(libs.plugins.kotlin.android) alias(libs.plugins.gradle.versions) id("io.sentry.spotless") id("io.sentry.jdk-target") diff --git a/sentry-samples/sentry-samples-android/build.gradle.kts b/sentry-samples/sentry-samples-android/build.gradle.kts index 71c34564edf..6fecebbfa33 100644 --- a/sentry-samples/sentry-samples-android/build.gradle.kts +++ b/sentry-samples/sentry-samples-android/build.gradle.kts @@ -20,7 +20,6 @@ plugins { alias(libs.plugins.android.application) id("io.sentry.spotless") id("io.sentry.jdk-target") - alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) alias(libs.plugins.ksp) alias(libs.plugins.sentry) apply false diff --git a/sentry-samples/sentry-samples-android/macrobenchmark/build.gradle.kts b/sentry-samples/sentry-samples-android/macrobenchmark/build.gradle.kts index a26fe088cd7..45b189b5567 100644 --- a/sentry-samples/sentry-samples-android/macrobenchmark/build.gradle.kts +++ b/sentry-samples/sentry-samples-android/macrobenchmark/build.gradle.kts @@ -2,7 +2,6 @@ plugins { id("com.android.test") id("io.sentry.spotless") id("io.sentry.jdk-target") - alias(libs.plugins.kotlin.android) } android { diff --git a/sentry-test-support/build.gradle.kts b/sentry-test-support/build.gradle.kts index b332f035958..0bb82497e11 100644 --- a/sentry-test-support/build.gradle.kts +++ b/sentry-test-support/build.gradle.kts @@ -8,6 +8,12 @@ plugins { id("io.sentry.jdk-target") } +// Android unit tests compile for the SDK's Java 11 target, and Kotlin can't inline bytecode of a +// higher target into theirs. +tasks.compileKotlin { compilerOptions.jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11 } + +tasks.compileJava { options.release = 11 } + tasks.withType().configureEach { compilerOptions.languageVersion = org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9 compilerOptions.apiVersion = org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9