Exploring the Relationship Between Gradle, AGP, and Android API Levels in Android
Connection between API level, AGP version, gradle version and Open JDK version.
While doing android app development, very often we get some difficulties of version related issues. Like we get warning to update our SDK version (compile SDK version) to the latest version.
When we change this compile SDK version to the latest, we get another requirement to update Android Gradle Plugin (AGP) version and Gradle Version. For example: If we set our compile SDK version to 34 we must update our AGP version to at least 8.1.1 and the Gradle version to 8.1.1
So how these version are connected? Before that lets understand what is gradle version and android gradle plugin version.
Gradle Version
gradle is a open source build automation tool which is widely used for building variety of projects. Android studio build system is also based on gradle( but gradle is not specific to only android). The main responsibility of gradle is to download dependencies, manage project configration and run various build tasks. It is specified in the `gradle-wrapper.properties` file in your project or in the `gradle/wrapper/gradle-wrapper.properties` file. For example:
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Android Gradle Plugin (AGP) Version
Gradle has a lot of plugins. Android Gradle Plugin (AGP) is one of them by Google for building Android applications. It’s responsible for tasks like compiling code, packaging resources, and generating APK files. The Android Gradle Plugin is specified in project’s build.gradle file within the “buildscript” block, like this:
plugins {
id 'com.android.application' version '7.4.2'
}
Gradle version and Android Gradle Plugin version are not same thing. The versions are independent to each other but there is a compatibility relationship between this two version. For specific Android Gradle Plugin version there is specific requirement of gradle version. For example:
AGP version 7.2 .0 — require gradle version at least 7.3.3 or higher
AGP version 8.1 .1 — require gradle version at least 8.0 or higher.
Here we saw that gradle version depends on AGP version in android. So it is always recommended to follow the documentation/release notes of the specific AGP version to ensure compatibility between android gradle plugin version and gradle plugin version.
Connection between Api level, AGP version, Gradle Version and Open JDK version
AGP version is based on Compile SDK version. for example
Api level 34 — min required AGP version is 8.1.1
api level 33 — min required AGP version is 7.2
On the other hand, Gradle version and open JDK version both are based on AGP version.
For specific AGP version we will found compatible Gradle version and JDK version from their release notes. for example
AGP version 7.2.0 — min gradle version 7.3.3, min JDK version 11
AGP version 7.4.0 — min gradle version 7.5, min JDK version 11
This is how api level, AGP vesion, Gradle version and Open JDK version are connected. So remember this-
(on change API level) — Check minimum required AGP version
(on change AGP version) — Check minimum required Gradle version and open JDK version.
Bonus!
When we build an Android project with Gradle, the Android Gradle Plugin (AGP) manages everything. It handles tasks like compilation of source code, resource processing, and APK creation.
AGP uses different tools for these tasks, and one important tool is the Android Asset Packaging Tool (AAPT).
AGP invokes AAPT to take care of packaging and processing all the resource under res directory. Then AAPT compiles all resources (XML layout files, images etc) under res directory into binary format, generates the R.java
file, and packages everything into the final APK.
To know more about AAPT and android R class you can follow this article.
Happy Coding !