diff --git a/packages/analytics-react-native/android/build.gradle b/packages/analytics-react-native/android/build.gradle index 6d906d9621..dde253d23f 100644 --- a/packages/analytics-react-native/android/build.gradle +++ b/packages/analytics-react-native/android/build.gradle @@ -13,7 +13,27 @@ if (findProject(':app') != null) { } apply plugin: "com.android.library" -apply plugin: "kotlin-android" +// Android Gradle Plugin 9 ships built-in Kotlin support and applies the Kotlin +// plugin itself. Applying it a second time fails the configuration phase with +// "Cannot add extension with name 'kotlin'". Apply it only when AGP is not +// providing Kotlin: AGP 8 and older, or AGP 9 with android.builtInKotlin=false. +def shouldApplyKotlinPlugin() { + def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger() + if (agpMajor <= 8) { + return true + } + // AGP 10 removes the opt-out: built-in Kotlin is always active there. + if (agpMajor >= 10) { + return false + } + def propertyVal = providers.gradleProperty("android.builtInKotlin").orNull + def builtInKotlinEnabled = propertyVal != null ? propertyVal.toBoolean() : true + return !builtInKotlinEnabled +} + +if (shouldApplyKotlinPlugin()) { + apply plugin: "kotlin-android" +} if (isNewArchitectureEnabled()) { apply plugin: "com.facebook.react" diff --git a/packages/plugin-session-replay-react-native/android/build.gradle b/packages/plugin-session-replay-react-native/android/build.gradle index b50ff4dce2..5f1c04c753 100644 --- a/packages/plugin-session-replay-react-native/android/build.gradle +++ b/packages/plugin-session-replay-react-native/android/build.gradle @@ -18,7 +18,27 @@ if (findProject(':app') != null) { } apply plugin: "com.android.library" -apply plugin: "kotlin-android" +// Android Gradle Plugin 9 ships built-in Kotlin support and applies the Kotlin +// plugin itself. Applying it a second time fails the configuration phase with +// "Cannot add extension with name 'kotlin'". Apply it only when AGP is not +// providing Kotlin: AGP 8 and older, or AGP 9 with android.builtInKotlin=false. +def shouldApplyKotlinPlugin() { + def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger() + if (agpMajor <= 8) { + return true + } + // AGP 10 removes the opt-out: built-in Kotlin is always active there. + if (agpMajor >= 10) { + return false + } + def propertyVal = providers.gradleProperty("android.builtInKotlin").orNull + def builtInKotlinEnabled = propertyVal != null ? propertyVal.toBoolean() : true + return !builtInKotlinEnabled +} + +if (shouldApplyKotlinPlugin()) { + apply plugin: "kotlin-android" +} if (isNewArchitectureEnabled()) { apply plugin: "com.facebook.react" diff --git a/packages/session-replay-react-native/android/build.gradle b/packages/session-replay-react-native/android/build.gradle index 02b727dbe7..887f186ce3 100644 --- a/packages/session-replay-react-native/android/build.gradle +++ b/packages/session-replay-react-native/android/build.gradle @@ -18,7 +18,27 @@ if (findProject(':app') != null) { } apply plugin: "com.android.library" -apply plugin: "kotlin-android" +// Android Gradle Plugin 9 ships built-in Kotlin support and applies the Kotlin +// plugin itself. Applying it a second time fails the configuration phase with +// "Cannot add extension with name 'kotlin'". Apply it only when AGP is not +// providing Kotlin: AGP 8 and older, or AGP 9 with android.builtInKotlin=false. +def shouldApplyKotlinPlugin() { + def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger() + if (agpMajor <= 8) { + return true + } + // AGP 10 removes the opt-out: built-in Kotlin is always active there. + if (agpMajor >= 10) { + return false + } + def propertyVal = providers.gradleProperty("android.builtInKotlin").orNull + def builtInKotlinEnabled = propertyVal != null ? propertyVal.toBoolean() : true + return !builtInKotlinEnabled +} + +if (shouldApplyKotlinPlugin()) { + apply plugin: "kotlin-android" +} if (isNewArchitectureEnabled()) { apply plugin: "com.facebook.react"