接續 Day 9,接著來看 dependencyResolutionManagement{}
的作用。
pluginManagement {
repositories {
...
}
}
dependencyResolutionManagement {
/**
* 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 {repositories {...}} 區塊
* 配置專案內所有模組的儲存庫(repositories)及依賴項(dependencies)。
* 然而,你應該要在每一個模組層級(module-level)的 build.gradle 檔案
* 配罝專屬該模組的依賴項。
* 在 Android Studio 建立的新專案,預設配置了 Google's Maven 儲存庫
* 和 Maven Central 儲存庫。
* 除非你選擇了需求依賴項的範本,才會有配置依賴項。
*/
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include ‘:app’
那在 Gradle 的文件上可以再次看到一樣的說明,dependencyResolutionManagement{}
允許幫所有 projects 配置依賴項解決方案。
Allows configuring dependency resolution for all projects of the build
repositoriesMode.set()
則定義了如何在構建過程中安裝依賴項。
The repository mode configures how repositories are setup in the build.
預設傳入 RepositoriesMode.FAIL_ON_PROJECT_REPOS
,表示任何在專案中宣告的依賴項,不論是直接宣告或透過 plugin 引入,都會觸發構建過程錯誤模式。
If this mode is set, any repository declared directly in a project, either directly or via a plugin, will trigger a build error.