-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
This issue is somewhat of a sibling to #123934, but that issue is about the internal implementation of the Flutter Gradle Plugin. This one is about the template file that, upon flutter create, becomes android/build.gradle.
It is nothing very important, but IMHO, it's good to take note of this nonetheless. It can be useful to whoever might be working on Flutter x Gradle in the future.
Description
Using subprojects and allprojects in Gradle build scripts is considered to be a rather bad practice – it can lead to some problems and limitations in certain scenarios.
Excerpt from the build.gradle file template for an app:
allprojects {
repositories {
google()
mavenCentral()
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}The subprojects in this snippet is reasonable and I don't know how we could get rid of it easily, but as for the allprojects, it's unneeded. I tested this by removing it in a few of my Flutter projects and nothing happened.
The subprojects and allprojects closures are used to apply configuration or tasks to all sub-projects or to all projects in a multi-project build. While this can be useful for applying common settings or tasks to multiple projects, it makes the buildscript less flexible.
One of the main drawbacks of using subprojects or allprojects is that it can limit the flexibility and customizability of individual sub-projects. If a sub-project needs to deviate from the common configuration or tasks, it may be difficult to do so without modifying the global build script. This can lead to conflicts and version control issues if multiple developers are working on the same build script.
Possible alternative
So I'd suggest removing it, because plugins also declare these repositories. If the current behavior is to be kept, Gradle has a new feature for this - dependencyResolutionManagement {}:
Instead of declaring repositories in every subproject of your build or via an allprojects block, Gradle offers a way to declare them in a central place for all project.
Repositories used by convention by every subproject can be declared in the settings.gradle(.kts) file:
dependencyResolutionManagement {
repositories {
mavenCentral()
google()
}
}Learn more here about this feature.
FWIW, Android Studio updated its templates to use dependencyResolutionManagement – learn more here.