Explain how Android uses gradle to uniformly configure dependency management

Before introducing the use of gradle to uniformly configure dependency management, let's briefly introduce gradle. Gradle is a JVM based construction tool and a very flexible and powerful construction tool. It supports jcenter, Maven and ivy warehouses and transitive dependency management (that is, a depends on B and B depends on C, so a can also rely on C without relying on C alone), It does not need a remote warehouse or pom.xml and ivy.xml configuration files, and discards all kinds of cumbersome. Based on groovy, the build script is written in groovy

In our Android studio, gradle is used by default to build and manage our projects. In the process of building our projects, many modules are usually created to decouple the functions and businesses of our projects (that is, modular development). At this time, there may be a problem, That is, the versions of each module and the dependencies of some public libraries in the module may be inconsistent, including the compiled version used and the SDK version, which makes it impossible to package. Here, we can use the gradle unified configuration file to solve our problem

First, let's take a look at the build.gradle of our project directory under normal circumstances:

First look at build.gradle under the app:

Next, let's take a look at build. Gradle:

Now let's add a module library and take a look at build. Gradle:

Here, let's take a look at the difference between build.gradle and the app Directory:

The build.gradle in the app directory is: apply plugin: com.android.application

The build.gradle under the module library is: apply plugin: com.android.library

Other versions are different, and the elements are the same. Here is what we will introduce today. Here we see that the version numbers of the compiled SDK version, the compiled tools version and the minimum version supporting SDK are different. Here we need to unify them, but we can't configure them manually every time, When the number of modules increases, it is easy to make mistakes

terms of settlement:

Method 1

Unified configuration in build.gradle in the root directory of the project is as follows:

Build.gradle of the project root directory after configuration:

Next, we call build.gradle in app as follows:

Call in build.gradle of module as follows:

In this way, we have solved the problem of using gradle to uniformly configure the build.gradle dependency in the app and the build.gradle dependency in the module in the project. By analogy, more modules are also configured in this way. If the version needs to be changed in the future, we only need to go to the root directory build.gradle to modify it

Method 2

Because everyone has their own configuration habits, here we provide another configuration for your reference. Here we create config.gradle in the root directory of the main project to configure. The relevant configuration information required is as follows:

Configuration information in config.gradle:

Then we need to import config.gradle into build.gradle in the root directory. Here, we should pay special attention to the following code in build.gradle in the root directory:

The imported root directory, build.gradle, is as follows:

Next, we can introduce it into the module, as follows:

Here we have successfully introduced it into the build.gradle of the module. In the future, the introduction of each module is like this. It realizes the same function as method 1. Personally, I think the second one is better. Let's choose for ourselves. After all, each has its own advantages. Well, here we share with you the unified configuration dependency of gradle in the project. I hope it will be useful to you, I hope it will help you in your study, and I also hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>