Gradle Tutorial for Android: Getting Started – Part 1
In this Gradle Build Script tutorial, you’ll learn the basic syntax in build.gradle files generated by Android Studio. You’ll also learn about gradlew tasks, different dependency management techniques, and how to add a new dependency to your app. By Ricardo Costeira.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Gradle Tutorial for Android: Getting Started – Part 1
35 mins
- What is Gradle?
- Getting Started
- Exploring the Project-Level Files
- Moving on to Module-level Files
- Graduating to the Settings Files
- Using New(er) Gradle Configurations
- Mastering the Build: Gradle Commands
- Defining gradlew
- Executing gradlew Tasks
- gradlew assemble
- gradlew lint
- Managing Dependencies
- Using the ext Property
- Using buildSrc
- Using Version Catalogs
- Including Picasso
- Configuring Gradle Dependency
- Where to Go From Here?
In this tutorial, you’ll learn about Gradle and how you can set it up in a maintainable and scalable way. By the end of this tutorial, you’ll be able to:
- Build your Android apps from the command line.
- Read both Groovy and Kotlin Gradle build files.
- Manage dependencies with Gradle.
What is Gradle?
Gradle is an open-source build-automation system. It has the convenience of a Groovy- or Kotlin-based DSL and the advantages of Ant and Maven. With Gradle, you can easily manipulate the build process and its logic to create multiple versions of your app. It’s much easier to use and a lot more concise and flexible when compared to Ant or Maven alone.
Getting Started
Download the starter project by clicking the Download Materials link at the top or bottom of the tutorial.
Open the project in Android Studio, and take a look at its structure in the Project pane in Android Studio:
Pay attention to the files with the Gradle elephant icon and .gradle extension. These files are generated by Android Studio automatically during project creation. They are written in Groovy and responsible for processing your project’s build. They contain the necessary info about project structure, library dependencies, library versions and the app versions you’ll get as a result of the build process.
Starting from Android Studio Giraffe, Kotlin will be the default language for build configuration. Gradle files written in Kotlin have the .gradle.kts extension. You can see that there are already a few in the project, but they were manually added. These are the Kotlin equivalent to the .gradle ones. Well, more or less — they’re fairly different in behavior at this point, but you’ll understand why as you progress through the tutorial.
Exploring the Project-Level Files
Find the build.gradle file in the root directory of the project. It’s called a top-level (project-level) build.gradle file. It contains the settings that are applied to all modules of the project.
Open the file, and you’ll see the following code:
// 1
buildscript {
// 2
repositories {
google()
mavenCentral()
}
// 3
dependencies {
classpath "com.android.tools.build:gradle:8.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20"
}
}
// 4
allprojects {
repositories {
google()
mavenCentral()
}
}
// 5
tasks.register('clean', Delete) {
delete rootProject.buildDir
}
Here’s what’s going on, step by step:
- In the
buildscriptblock, you define settings needed to build your project. - In the
repositoriesblock, you add names of the repositories where Gradle should search for the plugins you use. - The
dependenciesblock contains necessary plugin dependencies — in this case the Gradle and Kotlin plugins. Don’t put your module dependencies in this block. - The structure of the
allprojectsblock is similar to thebuildscriptblock, but here you define repositories for all of your modules, not for Gradle itself. Usually you don’t define thedependenciessection forallprojects. The dependencies for each module are different and should reside in the module-level build.gradle. - A task represents a piece of work in the build process. This simple one cleans up the build files when executed. You’ll learn more about tasks later in this tutorial.
Moving on to Module-level Files
Now, go to the build.gradle file in the app module directory. It contains dependencies — libraries that a module relies on — and instructions for the build process. Each module defines its own build.gradle file.
// 1
plugins {
id "com.android.application"
id "kotlin-android"
}
// 2
android {
// 3
namespace "com.kodeco.socializify"
// 4
compileSdk 34
// 5
defaultConfig {
// 6
applicationId "com.kodeco.socializify"
// 7
minSdkVersion 23
// 8
targetSdkVersion 34
// 9
versionCode 1
// 10
versionName "1.0"
}
// 11
buildFeatures {
viewBinding true
}
// 12
kotlin {
jvmToolchain(17)
}
}
// 13
dependencies {
implementation fileTree(include: ["*.jar"], dir: "libs")
implementation "androidx.appcompat:appcompat:1.6.1"
implementation "com.google.android.material:material:1.9.0"
}
The code above does the following:
- Specifies a list of plugins needed to build the module. The
com.android.applicationplugin is necessary in order to set up the Android-specific settings of the build process. Here, you can also usecom.android.libraryif you’re creating a library module. Thekotlin-androidplugin allows you to use the Kotlin language in your module. - In the
androidblock, you place all platform-specific options of the module. - Defining a
namespaceis necessary for things like resource access. This used to be in the AndroidManifest.xml file under thepackageproperty, but has now migrated. - The
compileSdkoption indicates the API level your app will be compiled with. In other words, you can’t use features from an API higher than this value. Here, you’ve set the value to use APIs from Android Tiramisu. - The
defaultConfigblock contains options that will be applied to all build versions (e.g., debug, release, etc) of your app by default. - The
applicationIdis the identifier of your app. It should be unique so as to successfully publish or update your app on the Google Play Store. If you leave it undefined, the build system will use thenamespaceasapplicationId. - In order to set the lowest API level supported, use
minSdkVersion. Your app will not be available in the Play Store for the devices running on lower API levels. - The
targetSdkVersionparameter defines the maximum API level your app has been tested on. That is to say, you’re sure your app works properly on the devices with this SDK version, and it doesn’t require any backward-compatibility behavior. The best approach is to thoroughly test an app using the latest API, keeping yourtargetSdkVersionvalue equal tocompileSdk. -
versionCodeis a numeric value for the app version. -
versionNameis a user-friendly string for the app version. - The
buildFeaturesblock lets you enable certain features, like View binding or Compose. In this case, it’s doing the former. - Gradle 8.2 supports JVM 17 by default, so you force the project to use Java 17 through Gradle’s Java toolchain support.
- The
dependenciesblock contains all dependencies needed for this module. Later in this tutorial, you’ll find out more about managing your project’s dependencies.
