Android——Gradle插件项目根目录settings.gradle和build.gradle
·
一、settings.gradle结构分析
项目根目录下的settings.gradle配置文件示例:
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.
*/
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()
flatDir {
dirs 'libs'
}
}
}
rootProject.name='TestAndroidProject'
include ':AliPay'
include ':app'
(1)pluginManagement配置块
- 对每个项目和全局的配置。
- pluginManagement{}块只能出现在两个设置中。 一个是settings.gradle文件,它必须是文件中的第一个代码块,顺序第一出现;另一个是Initialization Scripts,不在本文讨论内。
pluginManagement {
plugins { //插件配置
}
resolutionStrategy {//插件策略配置
}
repositories { //插件运行,依赖的仓库
//按照配置顺序寻找
gradlePluginPortal()
google()
mavenCentral()
//本地仓库配置
maven { url 'file://E:/libs/localMaven/' }
//远程仓库+地址配置
maven { url 'https://repo1.maven.org/maven2/' }
}
}
- 具体使用官方网址:Gradle-pluginManagement使用
- pluginManagement 脚本块中的 repositories 配置 , 对应之前的 buildscript 中的 repositories 配置
(2)dependencyResolutionManagement配置块
settings.gradle 中部分
//使用Catalog统一依赖版本 开启VERSION_CATALOG
enableFeaturePreview('VERSION_CATALOGS')
// 指定Gradle需要的用来搜索或下载【依赖dependency】的代码库
dependencyResolutionManagement {
// 然而,为了配置一些模块特定的依赖,你需要在每一个模块的模块级build.gradle文件中进行配置说明
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
// 以下是AS默认configure的repositories
google()
jcenter()
mavenCentral()
}
//使用Catalog统一依赖版本
versionCatalogs{
libs {
version('paging', '3.1.1')
version('glide', '4.14.2')
version('lifecycle', '2.4.1')
version('appcompat', '1.4.1')
alias('paging-runtime').to('androidx.paging', 'paging-runtime').versionRef('paging')
alias('paging-guava').to('androidx.paging', 'paging-guava').versionRef('paging')
alias('paging-rxjava2').to('androidx.paging', 'paging-rxjava2').versionRef('paging')
alias('glide-v4').to('com.github.bumptech.glide', 'glide').versionRef('glide')
alias('lifecycle-livedata').to('androidx.lifecycle', 'lifecycle-livedata-ktx').versionRef('lifecycle')
alias('lifecycle-viewmodel').to('androidx.lifecycle', 'lifecycle-viewmodel-ktx').versionRef('lifecycle')
alias('androidx-appcompat').to('androidx.appcompat', 'appcompat').versionRef('appcompat')
}
}
}
- repositoriesMode 模式有两种 :
RepositoriesMode.PREFER_PROJECT : 解析依赖库时 , 优先使用本地仓库 , 本地仓库没有该依赖 , 则使用远程仓库 ;
RepositoriesMode.FAIL_ON_PROJECT_REPOS : 解析依赖库时 , 强行使用远程仓库 , 不管本地仓库有没有该依赖库 ;
- dependencyResolutionManagement 脚本块中的 repositories 配置 , 对应之前的 allprojects 中的 repositories 配置 ;
(3)settings.gradle对应Settings对象实例
settings.gradle 文件对应的gradle api中Settings对象实例,其api如下
Settings对象实例Api (Gradle API 8.4)https://docs.gradle.org/current/javadoc/org/gradle/api/initialization/Settings.htmlsettings.gradle文件配置项,实际上及时调用该api中的方法和属性
二、build.gradle结构分析(根目录)
- build.gradle是gradle构建脚本文件,支持java、groovy等语言。
- 每个gradle项目或模块都会有一个build.gradle文件,该文件是项目构建的入口,可配置版本、插件依赖库等信息。
- 每个build文件都有一个对应的project实例,配置build.gradle文件,实际就是设置project实例里面的属性,或者调用里面的方法。
- 根项目的project实例可以获取到所有子项目或子模块的project实例,因此我们可以在根项目的build.gradle文件中对子项目进行统一配置,比如应用插件、依赖的maven中心仓库等,常见的build.gradle属性及方法如下所示
(1)项目根目录build.gradle
// 构建脚本
buildscript {
// 定义全局变量,常用于版本管理
// 变量在子模块的build.gradle中直接以: $NAME 的形式调用
ext {
compose_version = '1.0.1'
lifecycleVersion = '2.3.1'
kotlinVersion = '1.5.21'
ktlintVersion = '0.41.0'
coroutines = '1.5.0'
moshi_version = '1.12.0'
}
}
// 依赖URL
// 之前于settings.gradle定义的是整体的仓库位置,而这里可以视为定义具体的依赖位置
// plugins定义项目中所有模块共用的 Gradle 依赖项
// apply false 不可以用于子模块的build.gradle
plugins {
id 'com.android.application' version '7.1.0-rc01' apply false
id 'com.android.library' version '7.1.0-rc01' apply false
id 'org.jetbrains.kotlin.android' version '1.5.21' apply false
}
// 定义清理build目录的对应方法
task clean(type: Delete) {
delete rootProject.buildDir
}
-
buildscript
需要注意的是:
1)、buildscript
代码段必须是build.gradle
文件的第一个代码段;
2)、对于多模块构建,项目的buildscript
代码段声明的依赖关系可用于所有子模块的构建脚本;
3)、构建脚本的依赖可能是gradle插件
(2)子目录下build.gradle
// 子模块的plugins
// 请注意!这里就不可以定义apply false了
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'dagger.hilt.android.plugin'
}
// 应用插件 Kapt
// 这是一款kotlin专用的依赖管理插件,推荐每个项目都添加!
apply plugin: 'kotlin-kapt'
// android
// 定义所有模块构建设置
android {
// 定义编译SDK
// 表示你的项目可以使用低于(或等于)该版本的所有API
compileSdk 32
// 定义默认配置
defaultConfig {
// 工件ID
applicationId "com.example.character"
// 最低可接受SDK版本
minSdk 21
// 最高可接受SDK版本
targetSdk 32
// 给开发者看的项目版本
versionCode 1
// 给客户看的项目版本
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
// 定义构建类型
// 默认的构建类型有两种:debug(构建时会默认打上debug签名) release(构建时默认不打任何签名)
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// 如果你用的是JDK8,那么请添加这个两个配置以提供支持
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
// 构建特性
buildFeatures {
compose true
}
// compose设置
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
// 资源文件配置
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
// 在这里直接把你需要添加的依赖贴进去就好了
// 贴完后点sync即可
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
// dagger -hilt
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
kapt "androidx.hilt:hilt-compiler:1.0.0"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
// networking
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
// coil
implementation("io.coil-kt:coil-compose:1.4.0")
}
三、settings和build组合搭配方式
(1)settings.gradle全局配置,根build.gradle不做配置
-
gradle.properties配置全局变量
-
settings.gradle配置文件
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.
*/
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
resolutionStrategy {
}
plugins{
//是用来构建 apk 的 gradle 插件
id 'com.android.application' version '${agpVersion}' apply false
//是用来构建 Android Library 的 gradle 插件 (jar, aar)
id 'com.android.library' version '${agpVersion}' apply false
// 一个Gradle插件,用于将所有依赖项比如lib文件下依赖的jar包和项目代码打包到单个Jar文件中 官方地址[https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow#groovy-usage]
id 'com.github.johnrengelman.shadow' version "${agpShadow}" apply false
}
}
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.PREFER_PROJECT : 解析依赖库时 , 优先使用本地仓库 , 本地仓库没有该依赖 , 则使用远程仓库 ;
* RepositoriesMode.FAIL_ON_PROJECT_REPOS : 解析依赖库时 , 强行使用远程仓库 , 不管本地仓库有没有该依赖库 ;
* */
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
flatDir {
dirs 'libs'
}
}
}
rootProject.name='TestAndroidProject'
include ':AliPay'
include ':app'
-
根目录build.gradle文件
plugins{
//是用来构建 apk 的 gradle 插件
id 'com.android.application' apply false
//是用来构建 Android Library 的 gradle 插件 (jar, aar)
id 'com.android.library' apply false
// 一个Gradle插件,用于将所有依赖项比如lib文件下依赖的jar包和项目代码打包到单个Jar文件中 官方地址[https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow#groovy-usage]
id 'com.github.johnrengelman.shadow' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
(2)根build.gradle配置,setting.gradle仅配置基础
-
settings.gradle配置
rootProject.name='WiFiAndroidProject'
include ':DashboardView'
include ':BluetoothKitlibrary'
include ':niftydialogeffectslibrary'
include ':TwinklingRefreshLayout_library'
include ':AliPay'
include ':app'
-
build.gradle配置
//通常定义了项目中的模块使用的通用插件版本
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
jcenter()
maven { url 'https://repo1.maven.org/maven2/' }
//
}
dependencies {
//配置gradle插件
classpath "com.android.tools.build:gradle:8.1.0"
}
}
allprojects {
repositories {
flatDir {
dirs 'libs'
}
flatDir {
dirs project(':AliPay').file('libs')
}
google()
mavenCentral()
maven { url 'https://jitpack.io' }
maven { url 'https://repo1.maven.org/maven2/' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
更多推荐
已为社区贡献4条内容
所有评论(0)