Skip to content

Commit 8a5abf2

Browse files
fix(android): skip explicit Kotlin plugin when AGP provides built-in Kotlin (#1058)
AGP 9 enables built-in Kotlin by default and applies the Kotlin plugin itself. Applying it again fails configuration with "Cannot add extension with name 'kotlin'". Guard the explicit apply so it only runs when AGP is not providing Kotlin: AGP 8 and older, or AGP 9 with android.builtInKotlin=false. AGP 10 removes that opt-out, so built-in Kotlin is always active there and the explicit apply must never run.
1 parent dcc4611 commit 8a5abf2

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

‎android/build.gradle‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,28 @@ def isNewArchitectureEnabled() {
1818
}
1919

2020
apply plugin: 'com.android.library'
21-
apply plugin: 'org.jetbrains.kotlin.android'
21+
// Android Gradle Plugin 9 ships built-in Kotlin support and applies the Kotlin
22+
// plugin itself. Applying it a second time fails the configuration phase with
23+
// "Cannot add extension with name 'kotlin'". Apply it only when AGP is not
24+
// providing Kotlin: AGP 8 and older, or AGP 9 with android.builtInKotlin=false.
25+
def shouldApplyKotlinPlugin() {
26+
def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
27+
if (agpMajor <= 8) {
28+
return true
29+
}
30+
// AGP 10 removes the opt-out: built-in Kotlin is always active there.
31+
if (agpMajor >= 10) {
32+
return false
33+
}
34+
def propertyVal = providers.gradleProperty("android.builtInKotlin").orNull
35+
def builtInKotlinEnabled = propertyVal != null ? propertyVal.toBoolean() : true
36+
return !builtInKotlinEnabled
37+
}
38+
39+
if (shouldApplyKotlinPlugin()) {
40+
apply plugin: 'org.jetbrains.kotlin.android'
41+
}
42+
2243
if (isNewArchitectureEnabled()) {
2344
apply plugin: "com.facebook.react"
2445
}

0 commit comments

Comments
 (0)