[EXTERNAL] Fix build on AGP 9 by skipping kotlin-android when AGP registers the kotlin extension (#1934) via @gabrieldonadel - #1939
Merged
Conversation
…in extension (#1934) ## 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: ``` > 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 both files, so on an AGP 9 project this library 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: ```groovy if (project.extensions.findByName('kotlin') == null) { apply plugin: 'kotlin-android' } ``` Files changed: - `android/build.gradle` - `react-native-purchases-ui/android/build.gradle` This checks the exact condition that fails, so there is no AGP version table to keep in sync. It follows the approach already used in [purchases-capacitor#860](RevenueCat/purchases-capacitor#860) and [purchases-flutter#1765](RevenueCat/purchases-flutter#1765), per maintainer review on this PR. Behaviour across configurations: | 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 (opt-out removed) | registered by AGP | no | The check sits after `apply plugin: 'com.android.library'` in both files, so AGP has already registered its extensions by the time it runs. ## 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 silently always being taken: with the flags off, `:<module>:compileDebugKotlin` runs from the explicitly applied plugin; with them on, the build succeeds without it, which it could not do if the plugin were still being applied. - Syntax-checked both files with Groovy's `Phases.CONVERSION`. - **Not run:** this repo's own CI or example app. You mentioned you'd handle AGP 9 CI coverage separately. 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
External contribution from @gabrieldonadel, merged into this branch to run CI. Original PR: #1934
Note
Low Risk
Gradle plugin wiring only; no runtime or security impact, with behavior unchanged on pre-AGP-9 toolchains.
Overview
Fixes Android Gradle Plugin 9 builds that fail with "Cannot add extension with name 'kotlin'" when both AGP’s built-in Kotlin support and the
kotlin-androidplugin run together.In
android/build.gradleandreact-native-purchases-ui/android/build.gradle,kotlin-androidis no longer applied unconditionally. It is applied only whenproject.extensions.findByName('kotlin')is null, so older AGP setups still get the plugin while AGP 9 can rely on its ownkotlinextension.Reviewed by Cursor Bugbot for commit 22131fc. Bugbot is set up for automated code reviews on this repo. Configure here.