Skip to content

Make it possible to apply Flutter's Gradle plugin using the declarative plugins block #121552

@bartekpacia

Description

@bartekpacia

Use case and proposal

it'd be great if Flutter's Gradle Plugin could be applied using the declarative plugins block. It's the recommended method of applying Gradle plugins.

From Gradle docs:

This way of adding plugins to a project is much more than a more convenient syntax. The plugins DSL is processed in a way which allows Gradle to determine the plugins in use very early and very quickly. This allows Gradle to do smart things such as:

  • Optimize the loading and reuse of plugin classes.

  • Provide editors detailed information about the potential properties and values in the buildscript for editing assistance.

This requires that plugins be specified in a way that Gradle can easily and quickly extract, before executing the rest of the build script. It also requires that the definition of plugins to use be somewhat static.

and:

With the introduction of the plugins DSL, users should have little reason to use the legacy method of applying plugins.

This would let Flutter developers do:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "flutter"
}

instead of current:

plugins {
    id "com.android.application"
    id "kotlin-android"
}

// ...

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

// ...

Implementation

The plugins block is constrained and doesn't allow for the direct loading of plugins from the filesystem path like it's currently done:

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

The simplest way to get around this would be to serve Flutter Gradle plugin on Gradle Plugin Portal. Developers would then use it ("it" being "the Flutter Gradle Plugin") in their build.gradle/build.gradle.kts files like this:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "io.flutter.gradle.plugin" version 3.7.5 // Flutter version used
}

The above is unfortunately quite bad because it forces the developer to manually update the version of their Flutter Gradle Plugin every time they flutter upgrade.

It's still possible, though, by using the pluginManagement {} block in the settings.gradle/settings.gradle.kts.

// settings.gradle.kts

pluginManagement {
    val flutterVersion: String = "..." // Get it somehow. Maybe run `flutter --version` and parse output?

    repositories {
        mavenCentral()
        google()
        gradlePluginPortal() // assume that Flutter Gradle Plugin is hosted here
    }

    resolutionStrategy {
        eachPlugin {
            if (requested.id.name == "flutter") {
                useModule("dev.flutter:flutter-gradle-plugin:$flutterVersion")
            }
        }
    }

    // pluginManagement block
    plugins {
        id("flutter") version flutterVersion apply false
    }
}

Then, developers could very easily apply this plugin in build.gradle:

// app/build.gradle

plugin {
    id "com.android.application"
    id "kotlin-android"
    id "flutter"
}

See also:

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterplatform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer versiont: gradle"flutter build" and "flutter run" on AndroidtoolAffects the "flutter" command-line tool. See also t: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions