Skip to content

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

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

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

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 all 2 modules below, 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
  • rive-debug-utils/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 @rive-app/react-native 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.
@mfazekas

Copy link
Copy Markdown
Collaborator

Thanks Gabriel! The guard is correct and changes nothing on AGP 8. I pushed a small commit shortening the comment to one line.

We tried it with AGP 9.1.0, Gradle 9.3.1 and RN 0.80.3. With built-in Kotlin on, the guard gets past configuration, but compileDebugKotlin then fails on the Nitro-generated types (Unresolved reference 'ResolvedReferencedAsset', the Hybrid*Spec classes), since built-in Kotlin ignores the java.srcDirs that rive+autolinking.gradle adds. Registering them in kotlin.srcDirs fixes it. With built-in Kotlin off it builds, but the guard isn't exercised.

RN 0.87 ships AGP 9 with built-in Kotlin and the new DSL off by default, so two questions:

  1. Which AGP/Gradle versions did you test with, and was built-in Kotlin on or off?
  2. From your nitro-speech work, does rive need the other two pieces for a full built-in-Kotlin build (Nitrogen kotlin.srcDirs registration and the prefab fix for libraryVariants under the new DSL), or are you seeing the Nitro sources compile some other way?

Happy to add those here or in a follow-up.

Built-in Kotlin only compiles kotlin.srcDirs, so the Kotlin sources that
were reachable only through java.srcDirs (src/new/java or src/legacy/java,
and the Nitrogen-generated android/kotlin dir added by
rive+autolinking.gradle) failed with unresolved references.
@gabrieldonadel

Copy link
Copy Markdown
Author

Thanks for testing it properly, and for the comment fix. You're right, and I owe you a correction.

1. What I tested. The "verified end to end" section in the description doesn't hold for Rive. This PR came out of a sweep on Expo SDK 58 / RN 0.87.0, AGP 9.2.1, Gradle 9.4.1. In that sweep, Rive never got as far as compiling: with the flags on it hit the kotlin extension collision, and with them off it failed with Project with path ':react-native-nitro-modules' could not be found, because the harness didn't install Nitro. So the guard was only checked at configuration time. Sorry about that.

I've now reproduced it properly: same setup, @rive-app/react-native@0.5.4, react-native-nitro-modules@0.36.5, newArchEnabled=true. With builtInKotlin=true, I get exactly what you saw: 68 errors in compileDebugKotlin, starting with Unresolved reference 'ResolvedReferencedAsset'.

2. The other two pieces.

  • Kotlin source dirs: yes, Rive needs this, and it's more than the Nitrogen dir. src/new/java and src/legacy/java are also Kotlin and also only on java.srcDirs. I pushed cac8165, which registers both in android/build.gradle:

    def riveSrcDir = useLegacy ? "src/legacy/java" : "src/new/java"
    java.srcDirs += ["generated/java", "generated/jni", riveSrcDir]
    kotlin.srcDirs += [riveSrcDir,
                       "${project.projectDir}/../nitrogen/generated/android/kotlin"]

    I put it here rather than in rive+autolinking.gradle because that file is regenerated. The lasting fix belongs in Nitrogen's createGradleExtension.ts, which still emits only java.srcDirs on margelo/nitro main.

  • Prefab / libraryVariants: not in Rive. Under newDsl=true, the failure is in react-native-nitro-modules/android/fix-prefab.gradle:31, and Rive has no prefab script of its own, so that one belongs in Nitro. For the record, the nitro-speech version of both fixes is fix(android): AGP 9 compatibility (kotlin guard, prefab, nitrogen srcDirs) NotGeorgeMessier/nitro-speech#17, which the maintainer wrote on top of my guard in feat: referenced assets #16. I didn't write that part.

Verified on the setup above with cac8165. Nitro-modules still needs margelo/nitro#1579 and its own kotlin.srcDirs for src/newarch, so I patched both locally to get past it. I also patched its fix-prefab.gradle locally for the newDsl=true row only.

android/build.gradle newDsl / builtInKotlin :rive-app_react-native:assembleDebug
before cac8165 off / on fail: 68 × unresolved Nitro types
before cac8165 off / off pass
cac8165 off / on pass
cac8165 on / on pass
cac8165 off / off pass
cac8165, USE_RIVE_LEGACY=true off / on pass

ResolvedReferencedAsset, HybridRiveViewSpec and com.rive.RiveReactNativeView are all in the debug AAR. :app:assembleDebug also passes in all three flag combinations.

So for a full built-in-Kotlin build on RN 0.87, Rive also needs a Nitro release that includes #1579 plus kotlin.srcDirs for its own src/newarch. The prefab fix is only needed if the app turns on newDsl. I'm happy to send the Nitrogen generator change upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants