Skip to content

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

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

fix(android): skip explicit Kotlin plugin when AGP registers the kotlin extension#384
gabrieldonadel wants to merge 1 commit into
mParticle:mainfrom
gabrieldonadel:fix/agp9-built-in-kotlin-mparticle

Conversation

@gabrieldonadel

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 module, so on an AGP 9 project this 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

This tests the condition that actually fails, so there is no AGP version table to
keep in sync, and it covers AGP 10 — where the android.builtInKotlin opt-out is
removed — without a special case.

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.
  • Confirmed both branches actually execute rather than one path always winning: with
    the flags off, compileDebugKotlin runs from the explicitly applied plugin; with
    them on the build completes without it.
  • Every changed file passes a Groovy Phases.CONVERSION syntax check.
  • Not run: this repo's own CI or example app.

Where this came from

A sweep of 1000 popular React Native libraries against the AGP 9 defaults. 281 failed
with the new DSL enabled, and 269 of those failed on exactly this collision — by
far the most common blocker. Affects react-native-mparticle here.

The same guard shape was accepted in
RevenueCat/react-native-purchases#1934,
at that maintainer's suggestion.

…in extension

AGP 9 ships built-in Kotlin support and registers the kotlin extension
itself. Applying the Kotlin plugin again fails configuration with
"Cannot add extension with name 'kotlin'". Check for the extension
directly, which needs no AGP version table and covers AGP 10, where the
android.builtInKotlin opt-out is removed.
Copilot AI lite review requested due to automatic review settings September 5, 2026 16:06
@gabrieldonadel
gabrieldonadel requested a review from a team as a code owner September 5, 2026 16:06
@cursor

cursor Bot commented Sep 5, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Gradle-only guard around plugin application; no runtime or API behavior changes, with backward-compatible behavior on older AGP.

Overview
Fixes Android configuration failures on AGP 9+ where built-in Kotlin already registers the kotlin extension and an unconditional kotlin-android apply triggers Cannot add extension with name 'kotlin'.

In android/build.gradle, kotlin-android is applied only when project.extensions.findByName('kotlin') is null, after com.android.library so AGP has registered extensions first. AGP 8.x and opt-out AGP 9 builds keep the explicit plugin; AGP 9+ with built-in Kotlin skip it without maintaining an AGP version matrix.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is narrowly scoped, matches the stated AGP 9+ failure mode, and the guard is placed after com.android.library where the extension would already be registered.

Pull request overview

Updates the Android Gradle build script to avoid a configuration-time conflict on Android Gradle Plugin (AGP) 9+ where AGP registers the kotlin extension itself, causing an explicit kotlin-android apply to fail.

Changes:

  • Conditionally applies kotlin-android only when the kotlin extension is not already registered.
  • Adds an explanatory comment describing the AGP 9+ behavior and why the guard exists.
File summaries
File Description
android/build.gradle Guards apply plugin: 'kotlin-android' behind an extensions.findByName('kotlin') check to prevent AGP 9+ extension collisions.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

2 participants