Skip to content

Commit 4a24b29

Browse files
fix(android): skip explicit Kotlin plugin when AGP registers the kotlin extension (#1453)
Co-authored-by: Daniel Williams <me@dannyhw.com>
1 parent 09e7375 commit 4a24b29

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎.changeset/agp9-builtin-kotlin.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Fix Android configuration on AGP 9, where built-in Kotlin support registers the
6+
`kotlin` extension itself and the explicit `kotlin-android` apply failed with
7+
"Cannot add extension with name 'kotlin'". The plugin - and the `kotlinOptions`
8+
block it contributes - is now applied only when nothing has registered that
9+
extension, leaving AGP 8 and `android.builtInKotlin=false` projects unchanged.

‎packages/repack/android/build.gradle‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ def isNewArchitectureEnabled() {
3131
}
3232

3333
apply plugin: 'com.android.library'
34-
apply plugin: 'kotlin-android'
34+
// AGP 9 ships built-in Kotlin support and registers the `kotlin` extension
35+
// itself. Applying the Kotlin plugin on top of it fails configuration with
36+
// "Cannot add extension with name 'kotlin'". Only apply it when nothing has
37+
// registered that extension yet.
38+
def appliesKotlinPlugin = project.extensions.findByName('kotlin') == null
39+
if (appliesKotlinPlugin) {
40+
apply plugin: 'kotlin-android'
41+
}
3542

3643
if (isNewArchitectureEnabled()) {
3744
apply plugin: "com.facebook.react"
@@ -130,11 +137,13 @@ android {
130137
}
131138
}
132139

133-
kotlinOptions {
134-
if (REACT_NATIVE_MINOR_VERSION < 69) {
135-
jvmTarget = JavaVersion.VERSION_1_8
136-
} else if (REACT_NATIVE_MINOR_VERSION < 73) {
137-
jvmTarget = JavaVersion.VERSION_11
140+
if (appliesKotlinPlugin) {
141+
kotlinOptions {
142+
if (REACT_NATIVE_MINOR_VERSION < 69) {
143+
jvmTarget = JavaVersion.VERSION_1_8
144+
} else if (REACT_NATIVE_MINOR_VERSION < 73) {
145+
jvmTarget = JavaVersion.VERSION_11
146+
}
138147
}
139148
}
140149

0 commit comments

Comments
 (0)