Skip to content

AGP 9 build failure: "Cannot add extension with name 'kotlin'" #79

Description

@kinggolf-hgm

Bug

Under Android Gradle Plugin 9 (com.android.tools.build:gradle:9.x), Gradle Sync fails when this plugin's android/build.gradle is loaded:

Build file '.../node_modules/@capacitor/filesystem/android/build.gradle' line: 29
A problem occurred evaluating project ':capacitor-filesystem'.
> Failed to apply plugin 'kotlin-android'.
   > Cannot add extension with name 'kotlin', as there is an extension already registered with that name.

Environment

  • Plugin version: 8.1.2
  • Capacitor: 8.x
  • Android Gradle Plugin: 9.1.1
  • Gradle: as required by AGP 9
  • Android Studio: Narwhal / Otter
  • Kotlin Gradle Plugin: 2.2.x

Root cause

Under AGP 9, the project already has an extension named kotlin registered on the subproject before @capacitor/filesystem/android/build.gradle is evaluated. The plugin's unconditional apply plugin: 'kotlin-android' then fails because the kotlin-android plugin tries to register the kotlin extension a second time.

Notably:

  • Guarding with project.plugins.hasPlugin('org.jetbrains.kotlin.android') is not sufficient. The plugin id check returns false (the kotlin-android plugin isn't applied yet) but the extension named kotlin already exists, so the apply still fails.
  • The correct guard is on the extension itself: project.extensions.findByName('kotlin').

Proposed fix

Make the kotlin-android apply conditional on the extension not already being registered:

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

This is a minimal, backwards-compatible change:

  • Under AGP 8 / older Kotlin, no kotlin extension is pre-registered, so findByName('kotlin') returns null and the apply runs as before.
  • Under AGP 9, the extension is pre-registered, the apply is skipped, and Gradle Sync succeeds.

Verified locally

We've been running this patch via patch-package in our app against AGP 9.1.1 with the plugin at 8.1.2. Builds succeed and the plugin functions correctly.

Happy to open a PR with this change if it's helpful — let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions