Skip to content

Change language of Gradle scripts in templates from Groovy to Kotlin #151166

@bartekpacia

Description

@bartekpacia

Use case

Since 3.22, Flutter supports writing Gradle buildscripts in Kotlin, in addition to Groovy.

Using Kotlin instead of Groovy in Gradle buildscripts has many advantages and has been recommended by Gradle since April 2023 (link).

I think it makes sense to make Kotlin the default language, to bring those advantages to every Flutter developer, many of whom are understandably not experts in Gradle, Groovy, and their quirks.

Proposal

Scope:

  1. Change the language of files in Gradle templates from Groovy to Kotlin
  2. Revise existing documentation on flutter/website to make sure it works equally well for both Groovy and Kotlin templates.

As for bullet point (2), it's worth noting that in many cases, Kotlin syntax in Gradle is a subset of Groovy syntax, so we in many cases we can just show Kotlin syntax and it'll work the same in Groovy, e.g.:

Example 1

This works only in Groovy:

dependencies {
    implementation "com.example:my-artifact:1.2.3"
}

but the following works both in Groovy and in Kotlin:

dependencies {
    implementation("com.example:my-artifact:1.2.3")
}

Example 2

This works only in Groovy:

android {
    namespace "pl.baftek.discoverrudy"
    compileSdk 34

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget "11"
    }

    defaultConfig {
        applicationId "pl.baftek.discoverrudy"
        minSdk 23
        targetSdk 34
        versionCode flutter.versionCode()
        versionName flutter.versionName()
    }
}

but this works in both:

android {
    namespace = "pl.baftek.discoverrudy"
    compileSdk = 34

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = "11"
    }

    defaultConfig {
        applicationId = "pl.baftek.discoverrudy"
        minSdk = 23
        targetSdk = 34
        versionCode = flutter.versionCode()
        versionName = flutter.versionName()
    }
}

Alternatively, we might decide to provide users with a switch when calling flutter create, e.g. flutter create --android-build-language kotlin and flutter create --android-build-language groovy.

See also

#148586

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterfyi-toolFor the attention of Flutter Tool teamplatform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer versiont: gradle"flutter build" and "flutter run" on Androidteam-androidOwned by Android platform teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-androidTriaged by Android platform team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions