舊專案的 repositories 竟然高達 4 個區塊,真是令人不敢置信。
這麼多的區塊導致分不清哪些儲存庫來源是真正在使用的。
先來看一下原本的模樣:
buildscript {
repositories {
maven { url 'https://plugins.gradle.org/m2/'}
mavenCentral()
}
}
repositories {
maven { url 'https://maven.google.com' }
maven { url "https://jcenter.bintray.com" }
}
apply plugin: ...
...
buildscript {
ext.kotlin_version = "1.5.21"
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
...
}
}
allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
...
這麼多看起來重複的區塊,我們要出動 settings.gradle(Project Settings) 檔案來幫忙整理。
settings.gradle 可以讓我們配置指定的儲存庫(repositories)以方便尋找 plugins。
在這個檔案可定義專案名稱,不需跟專案放置的資料夾名稱相同。
官方文件是這麼說的:
settings.gradle 檔案 (適用於 Groovy) 或 settings.gradle.kts 檔案 (適用於 Kotlin 指令碼) 位於專案根目錄中。這個設定檔可定義專案層級 (Project Level) 的存放區 (repositories) 設定,並告知 Gradle 應在建構應用程式時納入哪些模組 (plugins)。多模組專案必須指定應加入至最終版本的所有模組 (即 include 的作用)。
官方範例:
pluginManagement {
/**
* 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 {repositories {...}} 區塊配置供 Gradle 尋找
* 或下載 Gradle plugins 及相關的依賴項。
* Gradle 針對遠端儲存庫 (例如:JCenter、Maven Central、Ivy) 支援預先配置。
* 你也可以使用本地儲存庫或定義你擁有的遠端儲存庫。下列程式碼定義 3 個儲存庫,
* Gradle 會在這些指定的儲存庫中尋找 Gradle plugins 所使用的依賴項。
*/
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
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).
*/
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include ‘:app’
我處理完成的專案:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
maven { url "https://jitpack.io/" }
maven { url "https://jcenter.bintray.com" }
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
// APNG file for animation
maven { url "https://jitpack.io/" }
// 'io.github.youth5201314:banner:2.2.2' 依賴項 使用
maven { url "https://s01.oss.sonatype.org/content/groups/public" }
// 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1' 依賴項 使用
maven { url "https://jcenter.bintray.com" }
}
}
rootProject.name = "com.my.work"
include ':app'
pluginManagement {}
:管理 plugin 之用。
repositories {}
:定義要使用的 plugin 儲存庫。
gradlePluginPortal()
:讓我們可以套用任何第三方 plugins。
明天再來研究 dependencyResolutionManagement{}
做了什麼事。
資料來源
Gradle.org - Gradle Settings
Gradle Build Bible: The ultimate guide to mastering Gradle projects by Tom Gregory