[MUSIC PLAYING]
MURAT YENER: Hi, welcome to the MAD skills
series on Gradle and the new Android Gradle plugin APIs.
In this first episode, we'll take a look
at how the Android build system works
and learn the basics of Gradle.
We'll start with Gradle's build phases,
talk about how AGP's configuration options can help
customize your builds, and what to do in order
to keep your build efficient.
Understanding how the build phases work
and how to configure the Android's Gradle plugin
can help you customize your build according
to your project's needs.
OK, enough talking.
Let's switch to Android Studio and see
how the build system works.
Gradle is a general purpose-built automation tool.
While you can use Gradle to build Android projects,
you can actually use Gradle to build any type of software.
Gradle supports single or multi project builds.
To configure a project to work with Gradle,
you need to add a built Gradle file in the project folder.
In a multiple project hierarchy, the root project
consists of a settings Gradle file,
which lists other projects included in the builds.
Android uses multi project builds
to help you modularize your app.
Gradle is able to work with different types of projects,
such as Android or Java because of plugins.
These plugins come with predefined functionality
to configure and build a specific type of project.
For example, to build an Android project,
you need to configure your Gradle build file
with Android Gradle plugin.
The Android Gradle plugin knows how
to build and package and Android project,
whether it's an app or library.
Gradle's build process revolves around work units called tasks.
You can see the list of tasks by using the terminal
or enabling the task list in the Gradle pane in Android Studio.
These tasks receive inputs, take actions to perform some work,
and produce outputs as a result of the actions they performed.
The Android Gradle plugin defines its own tasks
and knows in which order these tasks
need to run to build an Android project.
A Gradle build file consists of different parts.
The configuration syntax is called Gradle DSL
and defines a way for developers to configure plugins.
Gradle parses the Android cell block
in the build Gradle file and creates
AGP DSL objects, such as application extension and build
type.
A typical Android project has a top-level Gradle built file.
Each module in an Android project
has a separate Gradle build file.
In this project I have a single app module.
In the module level build.gradle file,
I need to declare and apply the plugins needed
to build the project.
To tell Gradle that I'm building an Android project,
I need to apply com.android.application
or com.android.library plugins.
These plugins define how to configure and build
an Android application or library, respectively.
In this case, I'm building an Android app project,
so I need to apply com.android.application plugin.
In the sample, the Kotlin-Andriod plugin
is also applied since I'll be using Kotlin.
Android Gradle plugin provides its own DSL
that you can use to configure it.
And makes this configuration available to tasks during the
build.
To configure the Android Gradle plugin,
you need to use the Android block.
In this block, you can define SDK
in two versions, application details
and other configurations for different
build types, such as debug or release.
If you want to learn more on how Gradle uses this information
to create variants and what other options are
available to you, check out the build documentation
linked in the notes below.
In the next section, you can define dependencies.
Gradle's dependency management supports
Maven and IP compatible repositories,
as well as local binaries from the file system.
You can easily convert the syntax of a Maven dependency
to Gradle.
Let's see what happens behind the scenes
when I execute a task.
Gradle evaluates and runs the build in three phases--
initialization, configuration, and execution.
In the initialization phase, Gradle
decides which projects are included in the build
and creates a project instance for each of these projects.
To decide which projects are included in the build,
Gradle first looks for the settings Gradle
file to decide between a single project or multiple project
builds.
In the configuration phase, Gradle
evaluates all build scripts for the projects included
in the builds, applies plugins, configures
the build using the cell object and finally registers tasks,
as well as their inputs lazily.
Note that the configuration phase runs regardless of which
task is requested to run.
To keep your builds short and efficient,
avoid performing any time-consuming operations
in the configuration phase.
Finally, in the execution phase, Gradle
executes the set of tasks needed for the builds.
We'll take a closer look at these phases
in the next episodes, while writing our own plugin.
Gradle DSL supports Groovy and Kotlin scripts for build files.
So far, I used Groovy DSL scripts
to configure this project build.
You can see the same build file here in Kotlin and in Groovy
script side by side.
Notice the file name for the Kotlin script has kt as suffix.
Moving from Groovy to Kotlin or the other way
doesn't change how you execute a task.
That's it-- well, at least for this episode.
Gradle and Android Gradle plugin have many capabilities
that allow you to customize your builds.
In this episode, we just scratched the surface.
And you learned about basics of Gradle tasks, build phases,
how to configure AGP, and using the DSL to customize
your builds.
In the next episode, you'll see how
to use AGP's variant API to extend your build
by writing your own plugin.
Make sure to subscribe so you don't miss it.
See you next time.
[MUSIC PLAYING]