diff --git a/buildSrc/src/main/java/Config.kt b/buildSrc/src/main/java/Config.kt index 498beefff..7cf13c328 100644 --- a/buildSrc/src/main/java/Config.kt +++ b/buildSrc/src/main/java/Config.kt @@ -5,6 +5,8 @@ object Config { object BuildPlugins { val androidGradle = "com.android.tools.build:gradle:4.0.1" val kotlinGradlePlugin = "gradle-plugin" + val buildConfig = "com.github.gmazzo.buildconfig" + val buildConfigVersion = "2.0.2" } object Android { @@ -58,7 +60,8 @@ object Config { } object Sentry { - val SENTRY_CLIENT_NAME = "sentry.java.android" + val SENTRY_JAVA_SDK_NAME = "sentry.java" + val SENTRY_ANDROID_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.android" val group = "io.sentry" val description = "SDK for sentry.io" val website = "https://sentry.io" diff --git a/sentry-android-core/build.gradle.kts b/sentry-android-core/build.gradle.kts index b64b41cd9..ed347249c 100644 --- a/sentry-android-core/build.gradle.kts +++ b/sentry-android-core/build.gradle.kts @@ -23,7 +23,7 @@ android { versionName = project.version.toString() versionCode = project.properties[Config.Sentry.buildVersionCodeProp].toString().toInt() - buildConfigField("String", "SENTRY_CLIENT_NAME", "\"${Config.Sentry.SENTRY_CLIENT_NAME}\"") + buildConfigField("String", "SENTRY_ANDROID_SDK_NAME", "\"${Config.Sentry.SENTRY_ANDROID_SDK_NAME}\"") } buildTypes { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java index c79bb0bdf..0c10ba972 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java @@ -35,21 +35,21 @@ public final class SentryAndroidOptions extends SentryOptions { private boolean enableAppComponentBreadcrumbs = true; public SentryAndroidOptions() { - setSentryClientName(BuildConfig.SENTRY_CLIENT_NAME + "/" + BuildConfig.VERSION_NAME); + setSentryClientName(BuildConfig.SENTRY_ANDROID_SDK_NAME + "/" + BuildConfig.VERSION_NAME); setSdkVersion(createSdkVersion()); } private @NotNull SdkVersion createSdkVersion() { - final SdkVersion sdkVersion = new SdkVersion(); + SdkVersion sdkVersion = getSdkVersion(); - sdkVersion.setName(BuildConfig.SENTRY_CLIENT_NAME); + if (sdkVersion == null) { + sdkVersion = new SdkVersion(); + } + + sdkVersion.setName(BuildConfig.SENTRY_ANDROID_SDK_NAME); String version = BuildConfig.VERSION_NAME; sdkVersion.setVersion(version); - - // add 2 default packages sdkVersion.addPackage("maven:sentry-android-core", version); - // TODO: sentry-core should add itself as the version may mismatch - sdkVersion.addPackage("maven:sentry-core", version); return sdkVersion; } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidOptionsTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidOptionsTest.kt index fb2ff66e8..c43297577 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidOptionsTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidOptionsTest.kt @@ -11,7 +11,7 @@ class SentryAndroidOptionsTest { fun `init should set clientName`() { val sentryOptions = SentryAndroidOptions() - val clientName = "${BuildConfig.SENTRY_CLIENT_NAME}/${BuildConfig.VERSION_NAME}" + val clientName = "${BuildConfig.SENTRY_ANDROID_SDK_NAME}/${BuildConfig.VERSION_NAME}" assertEquals(clientName, sentryOptions.sentryClientName) } @@ -22,16 +22,16 @@ class SentryAndroidOptionsTest { assertNotNull(sentryOptions.sdkVersion) val sdkVersion = sentryOptions.sdkVersion!! - assertEquals(BuildConfig.SENTRY_CLIENT_NAME, sdkVersion.name) + assertEquals(BuildConfig.SENTRY_ANDROID_SDK_NAME, sdkVersion.name) assertEquals(BuildConfig.VERSION_NAME, sdkVersion.version) assertTrue(sdkVersion.packages!!.any { - it.name == "maven:sentry-android-core" + it.name == "maven:sentry-android-core" && it.version == BuildConfig.VERSION_NAME }) assertTrue(sdkVersion.packages!!.any { - it.name == "maven:sentry-core" + it.name == "maven:sentry-core" && it.version == BuildConfig.VERSION_NAME }) } diff --git a/sentry-android-ndk/src/test/java/io/sentry/android/ndk/SentryNdkUtilTest.kt b/sentry-android-ndk/src/test/java/io/sentry/android/ndk/SentryNdkUtilTest.kt index f64d6b097..f861cc235 100644 --- a/sentry-android-ndk/src/test/java/io/sentry/android/ndk/SentryNdkUtilTest.kt +++ b/sentry-android-ndk/src/test/java/io/sentry/android/ndk/SentryNdkUtilTest.kt @@ -1,7 +1,6 @@ package io.sentry.android.ndk import io.sentry.core.SentryOptions -import io.sentry.core.protocol.SdkVersion import kotlin.test.Test import kotlin.test.assertNull import kotlin.test.assertTrue @@ -10,9 +9,7 @@ class SentryNdkUtilTest { @Test fun `SentryNdk adds the Ndk package into the package list`() { - val options = SentryOptions().apply { - sdkVersion = SdkVersion() - } + val options = SentryOptions() SentryNdkUtil.addPackage(options.sdkVersion) assertTrue(options.sdkVersion!!.packages!!.any { it.name == "maven:sentry-android-ndk" @@ -22,7 +19,9 @@ class SentryNdkUtilTest { @Test fun `SentryNdk do not add the Ndk package into the package list`() { - val options = SentryOptions() + val options = SentryOptions().apply { + sdkVersion = null + } SentryNdkUtil.addPackage(options.sdkVersion) assertNull(options.sdkVersion?.packages) diff --git a/sentry-core/build.gradle.kts b/sentry-core/build.gradle.kts index f86852745..58bb6468a 100644 --- a/sentry-core/build.gradle.kts +++ b/sentry-core/build.gradle.kts @@ -7,6 +7,7 @@ plugins { id(Config.QualityPlugins.errorProne) id(Config.Deploy.novodaBintray) id(Config.QualityPlugins.gradleVersions) + id(Config.BuildPlugins.buildConfig) version Config.BuildPlugins.buildConfigVersion } configure { @@ -65,6 +66,18 @@ tasks { } } +buildConfig { + useJavaOutput() + packageName("io.sentry.core") + buildConfigField("String", "SENTRY_JAVA_SDK_NAME", "\"${Config.Sentry.SENTRY_JAVA_SDK_NAME}\"") + buildConfigField("String", "VERSION_NAME", "\"${project.version}\"") +} + +val generateBuildConfig by tasks +tasks.withType().configureEach { + dependsOn(generateBuildConfig) +} + //TODO: move thse blocks to parent gradle file, DRY configure { userOrg = Config.Sentry.userOrg diff --git a/sentry-core/src/main/java/io/sentry/core/SentryOptions.java b/sentry-core/src/main/java/io/sentry/core/SentryOptions.java index 582a26196..c3246f8d3 100644 --- a/sentry-core/src/main/java/io/sentry/core/SentryOptions.java +++ b/sentry-core/src/main/java/io/sentry/core/SentryOptions.java @@ -1036,5 +1036,19 @@ public SentryOptions() { integrations.add(new ShutdownHookIntegration()); eventProcessors.add(new MainEventProcessor(this)); + + setSentryClientName(BuildConfig.SENTRY_JAVA_SDK_NAME + "/" + BuildConfig.VERSION_NAME); + setSdkVersion(createSdkVersion()); + } + + private @NotNull SdkVersion createSdkVersion() { + final SdkVersion sdkVersion = new SdkVersion(); + + sdkVersion.setName(BuildConfig.SENTRY_JAVA_SDK_NAME); + String version = BuildConfig.VERSION_NAME; + sdkVersion.setVersion(version); + sdkVersion.addPackage("maven:sentry-core", version); + + return sdkVersion; } } diff --git a/sentry-core/src/test/java/io/sentry/core/SentryOptionsTest.kt b/sentry-core/src/test/java/io/sentry/core/SentryOptionsTest.kt index 9d7bff877..5068179e0 100644 --- a/sentry-core/src/test/java/io/sentry/core/SentryOptionsTest.kt +++ b/sentry-core/src/test/java/io/sentry/core/SentryOptionsTest.kt @@ -124,4 +124,28 @@ class SentryOptionsTest { val options = SentryOptions() assertNotNull(options.executorService) } + + @Test + fun `init should set SdkVersion`() { + val sentryOptions = SentryOptions() + assertNotNull(sentryOptions.sdkVersion) + val sdkVersion = sentryOptions.sdkVersion!! + + assertEquals(BuildConfig.SENTRY_JAVA_SDK_NAME, sdkVersion.name) + assertEquals(BuildConfig.VERSION_NAME, sdkVersion.version) + + assertTrue(sdkVersion.packages!!.any { + it.name == "maven:sentry-core" && + it.version == BuildConfig.VERSION_NAME + }) + } + + @Test + fun `init should set clientName`() { + val sentryOptions = SentryOptions() + + val clientName = "${BuildConfig.SENTRY_JAVA_SDK_NAME}/${BuildConfig.VERSION_NAME}" + + assertEquals(clientName, sentryOptions.sentryClientName) + } }