diff --git a/packages/core/android/build.gradle b/packages/core/android/build.gradle index 1b3238fa2..93163231a 100644 --- a/packages/core/android/build.gradle +++ b/packages/core/android/build.gradle @@ -36,7 +36,28 @@ apply plugin: 'com.android.library' if (isNewArchitectureEnabled()) { apply plugin: 'com.facebook.react' } -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; AGP 9 only when android.builtInKotlin=false (AGP 10 always provides Kotlin). +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' +} + apply plugin: 'org.jlleitschuh.gradle.ktlint' diff --git a/packages/internal-testing-tools/android/build.gradle b/packages/internal-testing-tools/android/build.gradle index 276d3e508..f2b211975 100644 --- a/packages/internal-testing-tools/android/build.gradle +++ b/packages/internal-testing-tools/android/build.gradle @@ -31,7 +31,28 @@ apply plugin: 'com.android.library' if (isNewArchitectureEnabled()) { apply plugin: 'com.facebook.react' } -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; AGP 9 only when android.builtInKotlin=false (AGP 10 always provides Kotlin). +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' +} + apply plugin: 'org.jlleitschuh.gradle.ktlint' /** diff --git a/packages/react-native-session-replay/android/build.gradle b/packages/react-native-session-replay/android/build.gradle index f9f1b3da3..34a278072 100644 --- a/packages/react-native-session-replay/android/build.gradle +++ b/packages/react-native-session-replay/android/build.gradle @@ -34,7 +34,28 @@ apply plugin: 'com.android.library' if (isNewArchitectureEnabled()) { apply plugin: 'com.facebook.react' } -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; AGP 9 only when android.builtInKotlin=false (AGP 10 always provides Kotlin). +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' +} + apply plugin: 'org.jlleitschuh.gradle.ktlint' /** diff --git a/packages/react-native-webview/android/build.gradle b/packages/react-native-webview/android/build.gradle index e145fb2cd..6f165cf20 100644 --- a/packages/react-native-webview/android/build.gradle +++ b/packages/react-native-webview/android/build.gradle @@ -32,7 +32,28 @@ apply plugin: 'com.android.library' if (isNewArchitectureEnabled()) { apply plugin: 'com.facebook.react' } -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; AGP 9 only when android.builtInKotlin=false (AGP 10 always provides Kotlin). +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' +} + apply plugin: 'org.jlleitschuh.gradle.ktlint' apply plugin: "io.gitlab.arturbosch.detekt"