Skip to content

Commit 3500cb6

Browse files
fix(android): check the kotlin extension instead of the AGP version
Replace the AGP version / android.builtInKotlin check with a direct test for the registered kotlin extension. The version check reads com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION, which can resolve to a different classpath entry than the AGP actually in use, and the global android.builtInKotlin property can be overridden per module by the com.android.built-in-kotlin plugin -- so both inputs can disagree with reality. Asking whether the kotlin extension exists tests the condition that actually fails, needs no AGP version table, and covers AGP 10 where the opt-out is removed. Co-authored-by: gabrieldonadel <11707729+gabrieldonadel@users.noreply.github.com>
1 parent 2f15506 commit 3500cb6

1 file changed

Lines changed: 7 additions & 19 deletions

File tree

‎android/build.gradle‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,13 @@ def isNewArchitectureEnabled() {
2222
}
2323

2424
apply plugin: "com.android.library"
25-
// Android Gradle Plugin 9 ships built-in Kotlin support and applies the Kotlin
26-
// plugin itself. Applying it a second time fails the configuration phase with
27-
// "Cannot add extension with name 'kotlin'". Apply it only when AGP is not
28-
// providing Kotlin: AGP 8 and older, or AGP 9 with android.builtInKotlin=false.
29-
def shouldApplyKotlinPlugin() {
30-
def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
31-
if (agpMajor <= 8) {
32-
return true
33-
}
34-
// AGP 10 removes the opt-out: built-in Kotlin is always active there.
35-
if (agpMajor >= 10) {
36-
return false
37-
}
38-
def propertyVal = providers.gradleProperty("android.builtInKotlin").orNull
39-
def builtInKotlinEnabled = propertyVal != null ? propertyVal.toBoolean() : true
40-
return !builtInKotlinEnabled
41-
}
42-
43-
if (shouldApplyKotlinPlugin()) {
25+
// AGP 9 ships built-in Kotlin support and registers the `kotlin` extension
26+
// itself. Applying the Kotlin plugin on top of it fails configuration with
27+
// "Cannot add extension with name 'kotlin'". Asking for the extension covers
28+
// every way built-in Kotlin can be turned on -- the global default, the
29+
// android.builtInKotlin property, and the per-module com.android.built-in-kotlin
30+
// plugin -- without reading an AGP version number.
31+
if (project.extensions.findByName('kotlin') == null) {
4432
apply plugin: "kotlin-android"
4533
}
4634

0 commit comments

Comments
 (0)