Gradle 设置文件
settings.gradle
文件(对于 Groovy)或 settings.gradle.kts
文件(对于 Kotlin 脚本)位于项目的根目录下。此设置文件会定义项目级仓库设置,并告知 Gradle 在构建应用时应将哪些模块包含在内。多模块项目需要指定应包含在最终 build 中的每个模块。
对于大多数项目,该文件默认如下所示:
pluginManagement {
//注:pluginManagement 脚本块对应之前的 buildscript 闭包
/**
* The pluginManagement {repositories {...}} block configures the
* repositories Gradle uses to search or download the Gradle plugins and
* their transitive dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use
* local repositories or define your own remote repositories. The code below
* defines the Gradle Plugin Portal, Google's Maven repository,
* and the Maven Central Repository as the repositories Gradle should use to
*look for its dependencies.
*/
/**
*pluginManagement{repositions{…}}块配置Gradle用来搜索或下载Gradle插件和
*它们的传递依赖关系的仓库。Gradle预配置支持远程存储库,如JCenter、Maven Central和Ivy。
*您也可以使用本地存储库或定义自己的远程存储库。下面的代码定义了谷歌的Maven存储库Gradle
*插件门户,和Maven中央存储库,作为Gradle用来查找依赖的仓库。
*/
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
//注:dependencyResolutionManagement 脚本块对应之前的 allprojects 闭包
/**
* The dependencyResolutionManagement {repositories {...}}
* block is where you configure the repositories and dependencies used by
* all modules in your project, such as libraries that you are using to
* create your application. However, you should configure module-specific
* dependencies in each module-level build.gradle file. For new projects,
* Android Studio includes Google's Maven repository and the Maven Central
* Repository by default, but it does not configure any dependencies (unless
* you select a template that requires some).
*/
/**
*dependencyResolutionManagement{存储库{…}}块是配置所有模块使用的仓库和依赖的位置。
*例如用于创建您的应用程序的libraries。但是,您应该在每个模块级build.gradle文件中配置模块
*独有的依赖项。对于新项目,Android Studio默认配置谷歌的Maven存储库和Maven Central仓库,但
*它不配置任何依赖项(除非您选择的模板项目需要一些)。
*/
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include ‘:app’
pluginManagement 脚本块 ,
- 用于 配置 Gradle 插件的 Maven 仓库 ,
- 配置的是 构建过程 中 , 使用的仓库 ;
dependencyResolutionManagement 脚本块 ,
- 用于 配置 依赖 的 Maven 仓库 ,
- 配置的是 工程 或 模块 下的依赖使用的仓库 ;
在 dependencyResolutionManagement 脚本块 中 定义的
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
代码的含义是 解析依赖时 , 只能使用本脚本块中的 Maven 仓库 , 不能使用 Module 子项目中的依赖 ;
repositoriesMode 模式有两种 :
- RepositoriesMode.PREFER_PROJECT : 解析依赖库时 , 优先使用本地仓库 , 本地仓库没有该依赖 , 则使用远程仓库 ;
- RepositoriesMode.FAIL_ON_PROJECT_REPOS : 解析依赖库时 , 强行使用远程仓库 , 不管本地仓库有没有该依赖库 ;