What is settings.gradle in Gradle?

A gradle build has three important files. build.gradle, gradle.properties and settings.gradle

Gradle build lifecycle consist of three phases: initialization, configuration, and execution. settings.gradle get evaluated in the initialization phase.

  • Gradle looks at the settings.gradle file in order to identify the different projects. At the end of the initialization phase, Gradle creates an instance of org.gradle.api.Project corresponding to each of these projects.
  • The settings.gradle file is a Groovy script, just like the build.gradle file. Only one settings.gradle script will be executed in each build (in comparison to multiple build.gradle scripts in multi-project builds).
  • The settings.gradle script will be executed before any build.gradle script and even before the Project instances are created. Therefore, it is evaluated against a Settings object.

settings.gradle is very important in multi-module projects.

settings.gradle may cotains following Properties.

PropertyDescription
buildCacheThe build cache configuration.
extensionsThe container of extensions.
gradleThe Gradle instance for the current build.
pluginManagerThe plugin manager for this plugin aware object.
pluginsThe container of plugins that have been applied to this object.
rootDirThe root directory of the build. The root directory is the project directory of the root project.
rootProjectThe root project of the build.
settingsReturns this settings object.
settingsDirThe settings directory of the build. The settings directory is the directory containing the settings file.
startParameterThe set of parameters used to invoke this instance of Gradle.

settings.gradle may cotains following Methods.

Methods

MethodDescription
apply(closure)Applies zero or more plugins or scripts.
apply(options)Applies a plugin or script, using the given options provided as a map. Does nothing if the plugin has already been applied.
apply(action)Applies zero or more plugins or scripts.
buildCache(action)Configures build cache.
findProject(projectDir)Returns the project with the given project directory.
findProject(path)Returns the project with the given path.
include(projectPaths)Adds the given projects to the build. Each path in the supplied list is treated as the path of a project to add to the build. Note that these path are not file paths, but instead specify the location of the new project in the project hierarchy. As such, the supplied paths must use the ‘:’ character as separator (and NOT ‘/’).
includeBuild(rootProject)Includes a build at the specified path to the composite build.
includeBuild(rootProject, configuration)Includes a build at the specified path to the composite build, with the supplied configuration.
includeFlat(projectNames)Adds the given projects to the build. Each name in the supplied list is treated as the name of a project to add to the build.
project(projectDir)Returns the project with the given project directory.
project(path)Returns the project with the given path.
Rajesh Kumar
Follow me