Understanding the settings.gradle.kts and build.gradle.kts Files in an Android Project
Jatin kumar Sahoo
Flutter Web Developer at Skillmine ( Flutter || Dart || GETX || Provider || Bloc || Android || IOS || Flutter WEB ||AWS APP SYNC || Firebase Crashlytics || Google Map SDK) Flutter Developer
What is settings.gradle.kts?
The settings.gradle.kts file sets up the structure of your project, including the modules it contains and where Gradle should look for plugins and libraries.
pluginManagement {
repositories {
google {
content {
includeGroupByRegex("com\\.android.*")
includeGroupByRegex("com\\.google.*")
includeGroupByRegex("androidx.*")
}
}
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "exampleapp"
include(":app")
Line-by-line explanation:
This block specifies where Gradle should look for plugins that are used in the project. Think of it as telling Gradle, "Here’s where to find the tools you need to build my app."
This tells Gradle to include plugins that match a specific pattern. For instance, com.android.* will include all Android SDK-related plugins, com.google.* includes Google-related plugins, and androidx.* includes AndroidX libraries.
This block defines how dependencies (libraries) are resolved across the entire project.
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS): This means that all repositories are declared at the global level. No module should declare its own repositories.
This sets the name of your main project. In this case, it’s "exampleapp".
This line includes the app module. In most Android projects, this module contains all the code for your application.
Explanation of build.gradle.kts (Project-Level Build Script)
The build.gradle.kts file in the project-level configuration defines configurations and plugins that are common to all modules in the project.
Breakdown of project-level build.gradle.kts:
领英推荐
alias(libs.plugins.android.application) apply false: Refers to the Android application plugin but doesn’t apply it at the project level. It's applied at the module level.
alias(libs.plugins.kotlin.android) apply false: Refers to the Kotlin plugin for Android but also doesn’t apply it at the project level.
By applying false, it means that the plugin is available but won't be applied automatically at this level. It will be applied in module-level build.gradle.kts files.
Explanation of build.gradle.kts (App-Level Build Script)
This is the module-level build script for the app module. This file contains configurations specific to building the Android application.
Breakdown of app-level build.gradle.kts:
plugins { ... }:
android { ... }: This block contains Android-specific configuration for the project.
This block contains Android-specific configuration for the project.
defaultConfig { ... }:
buildTypes { ... }: Defines different build types for the project.
compileOptions { ... }: Configures the Java compatibility for your project.
composeOptions { ... }:
kotlinCompilerExtensionVersion = "1.5.1": Specifies the version of Kotlin compiler extensions used for Jetpack Compose.
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }: Excludes certain license files from being packaged into the APK to reduce its size.
dependencies { ... }: The dependencies block lists the libraries and modules your project depends on.