After reading this article you will learn:
-
-
- Settinggradle
- Buildgradle in the home directory
- Buildgradle under the module
- Note
-
- Note that ApplicationID and package name are actually not a thing
- Summarize
When we create a new project with Android Studio, 3 Gradle files are automatically generated:
The next step is to describe the effects of these three files.
1. Setting.gradle
Previous article: Why Gradle introduced in such a fire,
A Gradle build typically consists of three phases: initialization, configuration, and execution.
The Setting.gradle file is executed during initialization , and the builder knows which modules will be built through the contents of the Setting.gradle file, and the following indicates that in addition to the app module in the current project, there is another one called " Shixinlibrary "Depends on the module:
Include ': App ', ': Shixinlibrary '
Note: A single module project does not necessarily require a setting file, but once you have multiple modules, you must have a setting file, and also specify all the modules to be built, otherwise gradle will not build modules that are not included.
2. Build.gradle in the home directory
Look at the comments in the Gradle file:
Top-level build file where can configuration options common to all sub-projects/modules.
The Build.gradle file in the home directory is the topmost build file, which configures the configuration information common to all modules.
The default top-level Build.gradle file includes two code blocks (Buildscript and allprojects):
Buildscript
As you can see from the name, Buildscript is the build script configuration for all projects, mainly including the dependent repositories and dependent gradle versions.
The repositories code block configures Jcenter as a warehouse, and Jcenter is a well-known Maven repository. Once the dependent warehouses have been identified, we can add the dependent, in the dependencies code block, the packages in the Jcenter repository.
The dependencies code block is used to configure the dependencies in the build process, note that this is used for the build process , so you cannot say that the libraries you need to rely on in your application module are added here.
The only dependencies that are used by default in the build process are Gradle for Android plug-ins. We can also add some other plugins for building, such as Retrolambda, APT, freeline and so on.
allprojects
The Allprojects code block is used to declare the properties that will be used for all modules, note that all modules are. It is common to configure warehouse addresses (Jcenter, custom maven repositories, etc.), and you can create tasks in allprojects, which are eventually applied to all modules,
The official recommendation is to add as few properties as possible for all modules, because this means strong coupling, and once the main project is not built, your submodule is likely to fail because of the lack of properties of all modules.
3. Build.gradle under the module
The Build.gradle file under the module applies only to the current module, and you can overwrite the contents of the Build.gradle in the home directory.
Take my practice project as an example:
The main sub-three modules: Apply plugin, Android, dependencies.
Apply Plugin
Application plugin declares which plug-ins to use Next, indicating that the Androd plugin is used, and that the reason for using the Android plugin here is that the Gradle for Android is declared in the home directory.
So when we need to use other plugins, such as RETROLAMBDA, we first need to add a dependency in the home directory Build.gradle file and then declare the use of the RETROLAMBDA plug-in in the module Build.gradle.
Note: The default Android plugin is officially maintained by Google, providing us with the ability to build, test, and package Android apps. In addition, we can also customize the plugin. After gradually deepening the understanding of Gradle, we will try to write a Gradle plugin.
Android
After declaring the Android plugin, we can build the configuration using the content provided by the Android plugin.
There must be two versions in the Android build configuration:
- Compilesdkversion: Compiling app's Android API version
- Buildtoolsversion: Build tool version
- Build tools including AAPT, Zipalign, Renderscript, etc.
- Used to generate various intermediates during packaging, you can download the build tool from the SDK Manager
The Defaultconfig code block is used to configure the default properties of the app and can override properties in the Androidmanifest.xml, such as:
- ApplicationID: Covers the package name in the Androidmanifest
- Minsdkversion: Overrides the properties in Androidmanifest, configures the minimum API to run the app
- Targetsdkversion: Same, for notifying the system that the current application has been tested by this version, and the previous compilesdkversion is not related
- Versioncode: Same, the version number of the app
- Versionname: Version Name
Defaultconfig can also add signatures, placeholders, and so on, which are only listed here.
Buildtypes is used to define how to build and package different types of applications, often testing and production. Specific content after the introduction.
Android can also configure other information, such as signatures, channels, etc., you can see in the Project Structure panel visual, add, or use the code to add, which we later detailed description:
Dependencies
The dependency configuration can be seen outside of the Android code block, in fact the dependency configuration is the underlying function of the Gradle configuration, meaning that other types of items, such as Java EE, can be used in addition to Android.
We can add the libraries we want to use in the dependent configuration, and of course we can add the local jar packages. Depending on the configuration content, we follow in-depth introduction.
Notes Note:ApplicationID and package name are actually not a thing.
Before building with Gradle, the package name actually has two functions:
- Use as an application in the R file
- Unique indication of the application
We know that an app with the same package name on an Android phone can only have one. But when we want to install a different version of an application at the same time, such as a test of a production, then you need to modify the package name, but the resource code and R files require the use of the packages name can not be changed, otherwise all your source files will change with the build version. What do we do?
After the advent of Gradle, the Android tools team decoupled two different uses of package name, proposing the concept of ApplicationID:
- The package defined in the Manifest file continues to be used for source code and R file labeling
- ApplicationID is used as a unique identifier for devices and Google Play
This means that ApplicationID covers part of the package name's responsibilities.
Summarize
This article provides an overview of the role and content of the Gradle file in an Android project, with many details such as custom builds, dependency management, configuration of multiple types of builds, and more. Next, we'll delve into these things.
Related reading:
Gradle for Android series: why Gradle so hot
Gradle for Android Series: initial knowledge of Gradle files