Skip to content

fix(android): skip explicit Kotlin plugin when AGP provides built-in Kotlin - #1966

Open
gabrieldonadel wants to merge 1 commit into
amplitude:mainfrom
gabrieldonadel:fix/agp9-built-in-kotlin
Open

fix(android): skip explicit Kotlin plugin when AGP provides built-in Kotlin#1966
gabrieldonadel wants to merge 1 commit into
amplitude:mainfrom
gabrieldonadel:fix/agp9-built-in-kotlin

Conversation

@gabrieldonadel

@gabrieldonadel gabrieldonadel commented Sep 2, 2026

Copy link
Copy Markdown

Problem

Android Gradle Plugin 9 ships built-in Kotlin support and enables it by default, so AGP
applies the Kotlin plugin itself. When a library also applies kotlin-android explicitly, the
two collide and configuration fails before anything compiles:

> Failed to apply plugin 'kotlin-android'.
   > Cannot add extension with name 'kotlin', as there is an extension already registered with that name.

The apply is unconditional in these files, so on an AGP 9 project they cannot be built
at all. There is no consumer-side workaround short of patching the file — setting
android.builtInKotlin=false project-wide just to build one dependency is not a
reasonable ask, and that escape hatch is removed in AGP 10.

Change

Keep the explicit apply, but skip it when AGP is already providing Kotlin:

def shouldApplyKotlinPlugin() {
    def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
    if (agpMajor <= 8) {
        return true
    }
    def propertyVal = providers.gradleProperty("android.builtInKotlin").orNull
    def builtInKotlinEnabled = propertyVal != null ? propertyVal.toBoolean() : true
    return !builtInKotlinEnabled
}

if (shouldApplyKotlinPlugin()) {
    apply plugin: "kotlin-android"
}

Files changed:

  • packages/analytics-react-native/android/build.gradle
  • packages/plugin-session-replay-react-native/android/build.gradle
  • packages/session-replay-react-native/android/build.gradle

Why the condition has this shape

Both halves are load-bearing:

  • AGP 8 and older always apply. Built-in Kotlin only exists from AGP 9.0. On older
    AGP nothing else applies the Kotlin plugin, so skipping would break the build in the
    opposite direction.
  • Absent is treated as enabled. android.builtInKotlin defaults to on in AGP 9, so
    only an explicit false means "AGP is not providing Kotlin, apply it yourself". This
    mirrors AGP's own default.
AGP android.builtInKotlin explicit apply
8.x unset or false yes (unchanged)
9.x unset no — AGP provides it
9.x true no — AGP provides it
9.x false yes — consumer opted out

com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION requires no new plugin resolution,
and the apply already happens after com.android.library, so AGP is on the classpath.

What I verified, and what I did not

  • Verified end to end on a real Expo SDK 58 / React Native 0.87 project with AGP
    9.2.1 and Gradle 9.4.1: :app:assembleDebug succeeds both with
    -Pandroid.newDsl=true -Pandroid.builtInKotlin=true and with both flags off. Same
    guard, both configurations green.
  • Syntax-checked these files with Groovy's Phases.CONVERSION.
  • Not run: this repo's own CI or example app. The AGP <= 8 path is unchanged by
    construction, since shouldApplyKotlinPlugin() short-circuits to true there — but a
    CI run is the real confirmation and I could not do that from outside.

Found while sweeping 157 popular React Native libraries for AGP 9 new-DSL compatibility.
34 failed with the new DSL enabled, and 29 of those failed on exactly this — it is the
most common blocker by a wide margin.


Note

Low Risk
Build-time Gradle plugin wiring only; AGP 8 behavior is unchanged and there is no runtime or application logic impact.

Overview
Fixes Gradle configuration failures on Android Gradle Plugin 9+ where these React Native Android libraries unconditionally applied kotlin-android while AGP already registers Kotlin, causing "Cannot add extension with name 'kotlin'" during configuration.

Each affected android/build.gradle (analytics-react-native, plugin-session-replay-react-native, session-replay-react-native) now applies kotlin-android only when AGP is not supplying Kotlin: always on AGP 8 and below, never on AGP 10+, and on AGP 9 only when the android.builtInKotlin property is explicitly false (unset defaults to built-in Kotlin on, so the explicit apply is skipped).

Reviewed by Cursor Bugbot for commit 2078973. Bugbot is set up for automated code reviews on this repo. Configure here.

@gabrieldonadel
gabrieldonadel requested a review from a team as a code owner September 2, 2026 20:36
…Kotlin

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.
@gabrieldonadel
gabrieldonadel force-pushed the fix/agp9-built-in-kotlin branch from d2f94d8 to 2078973 Compare September 2, 2026 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant