Skip to content

fix(android): skip explicit Kotlin plugin when AGP registers the kotlin extension - #446

Open
gabrieldonadel wants to merge 2 commits into
rive-app:mainfrom
gabrieldonadel:fix/agp9-built-in-kotlin
Open

gabrieldonadel wants to merge 2 commits into
rive-app: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 registers the kotlin extension itself. When a library also applies kotlin-android
explicitly, the two collide and configuration fails before anything compiles. AGP
words it two ways, both the same problem:

> Failed to apply plugin 'kotlin-android'.
   > Cannot add extension with name 'kotlin', as there is an extension already registered with that name.
> The 'kotlin-android' plugin is no longer required for Kotlin support since AGP 9.0.

The apply is unconditional in this file, so on an AGP 9 project it 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

Apply the plugin only when nothing has registered the kotlin extension yet:

if (project.extensions.findByName('kotlin') == null) {
    apply plugin: 'kotlin-android'
}

Files changed:

  • android/build.gradle

Why this shape

Earlier revisions of this PR derived the answer from the AGP version and the
android.builtInKotlin property. Asking for the extension directly is better on
three counts:

  • It tests the condition that actually fails. The collision is "something already
    registered kotlin", so that is what the guard checks. No AGP version table to keep
    in sync.
  • It cannot be fooled. android.builtInKotlin is a global switch, but built-in
    Kotlin can also be enabled per module with the com.android.built-in-kotlin plugin,
    so the global value can disagree with the module. Reading
    com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION has its own trap: it resolves
    against the buildscript classpath, which is not always the AGP that ends up running.
  • It covers AGP 10 for free. The android.builtInKotlin opt-out is removed there,
    and this guard needs no special case for it.
AGP android.builtInKotlin kotlin extension explicit apply
8.x unset or false absent yes (unchanged)
9.x unset or true registered by AGP no
9.x false absent yes
10+ n/a (removed) registered by AGP no

The guard sits after apply plugin: 'com.android.library' in every file it touches,
so AGP has already registered its extensions by the time it runs. I checked that
ordering per file rather than assuming it.

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. That
    run used the earlier version-based guard; the extension guard in this revision is
    the shape now used across the rest of this sweep and already merged in several of
    those repos.
  • Syntax-checked this file with Groovy's Phases.CONVERSION.
  • Not run: this repo's own CI or example app. 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.

This guard shape was suggested by the RevenueCat maintainers on
RevenueCat/react-native-purchases#1934,
who had already hit the same issue in their Capacitor and Flutter SDKs. I have
since standardised on it across this sweep.

…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 835b7d2 to 1c97e44 Compare September 2, 2026 21:03
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>
@gabrieldonadel gabrieldonadel changed the title fix(android): skip explicit Kotlin plugin when AGP provides built-in Kotlin fix(android): skip explicit Kotlin plugin when AGP registers the kotlin extension Sep 14, 2026
@gabrieldonadel

Copy link
Copy Markdown
Author

Pushed a revision that changes how the guard decides, not what it does.

Before, it derived the answer from the AGP version and the android.builtInKotlin
property. It now asks the question directly:

if (project.extensions.findByName('kotlin') == null) {
    apply plugin: 'kotlin-android'
}

Three reasons this is the better shape:

  • It tests the condition that actually fails, so there is no AGP version table to keep
    in sync.
  • It cannot be fooled. android.builtInKotlin is global, but built-in Kotlin can also
    be switched on per module with com.android.built-in-kotlin, and
    com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION resolves against the buildscript
    classpath, which is not always the AGP that ends up running.
  • It covers AGP 10, where the android.builtInKotlin opt-out is removed, with no
    special case.

This shape was suggested by the RevenueCat maintainers on
RevenueCat/react-native-purchases#1934
and is what the rest of this sweep now uses. Behaviour on AGP 8 and older is unchanged.
Same files as before; title and description updated to match. Sorry for the extra
notification.

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