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的repositoriesgoogle()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 配置 ;

参考文章1文章浏览阅读7.4k次,点赞52次,收藏45次。一、settings.gradle 构建脚本分析1、Maven 远程仓库配置2、目录配置3、完整代码示例二、根目录下 build.gradle 构建脚本分析_android settings.gradlehttps://blog.csdn.net/shulianghan/article/details/129802390

(3)settings.gradle对应Settings对象实例

settings.gradle 文件对应的gradle api中Settings对象实例,其api如下

Settings对象实例Api (Gradle API 8.4)icon-default.png?t=N7T8https://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 falseid 'com.android.library' version '7.1.0-rc01' apply falseid '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// 表示你的项目可以使用低于(或等于)该版本的所有APIcompileSdk 32// 定义默认配置defaultConfig {// 工件IDapplicationId "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 falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}// 如果你用的是JDK8,那么请添加这个两个配置以提供支持compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility 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 -hiltimplementation "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 componentsimplementation "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 componentsimplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"// networkingimplementation '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"// coilimplementation("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
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/189419.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数智竞技何以成为“科技+体育”新样本?

文 | 智能相对论 作者 | 青月 “欢迎来到,钢铁突袭。” 三人一组,头戴VR设备,中国香港队和泰国队在数实融合的空间里捉对厮杀,通过互相射击对方能量铠甲获取积分。 虽然双方都展现出了极强的机动性,但显然中国香港队…

企业月结快递管理教程

什么是月结快递?员工可能不清楚,但是企业行政人员应该很熟悉。各大快递公司为了留住商企这些大客户,推出了月结协议寄件,企业可以和快递公司签订月结协议,员工寄件不需要当场结算快递费,而是将快递费挂在企…

(二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、数据集二、导入数据以及展示部分1.导入数据集以及对数据集进行处理2.展示数据(看看就好) 三(1)、搭建网络进…

jQuery UI简单的讲解

我们先进入一下问答时间,你都知道多少呢? (1)什么是jQuery UI 呢? 解答:jQuery UI 是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库。包含底层用户交互、动画、特效和可更换主题的可视控件。我们…

计算数组中每个元素的立方根numpy.cbrt()

【小白从小学Python、C、Java】 【计算机等级考试500强双证书】 【Python-数据分析】 计算数组中每个元素的立方根 numpy.cbrt() [太阳]选择题 请问以下代码中执行语句输出结果是? import numpy as np a np.array([1, 8, 27]) print("【显示】a ",a) pr…

「Java开发指南」如何在Spring中使用JAX-WS注释器?

本文将指导您如何使用JAX-WS注释器从Spring服务生成JAX-WS Web服务,在本教程中,您将学习如何: 为Spring服务启用JAX-WS部署应用程序并测试服务 所有与Spring scaffolding相关的任务都需要MyEclipse Spring或Bling授权。 MyEclipse v2023.1…

Leetcode刷题详解——斐波那契数

1. 题目链接:509. 斐波那契数 2. 题目描述: 斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) 0,F(1…

OpenAI的Whisper蒸馏:蒸馏后的Distil-Whisper速度提升6倍

1 Distil-Whisper诞生 Whisper 是 OpenAI 研发并开源的一个自动语音识别(ASR,Automatic Speech Recognition)模型,他们通过从网络上收集了 68 万小时的多语言(98 种语言)和多任务(multitask&am…

三十分钟学会zookeeper

zookeeper 一、前提知识 集群与分布式 ​ 集群:将一个任务部署在多个服务器,每个服务器都能独立完成该任务。 ​ 分布式:将一个任务拆分成若干个子任务,由若干个服务器分别完成这些子任务,每个服务器只能完成某个特…

利用IP地址查询优化保险理赔与业务风控的实用方法

随着数字化时代的到来,保险行业正逐渐采用先进的技术来改善理赔流程和强化业务风控。其中,通过IP地址查询成为一种有效的手段,为保险公司提供更精准的信息,以便更好地管理风险和提高服务效率。本文将探讨如何利用IP地址查询优化保…

WPF创建自定义控件编译通过但是找不到资源

报错: 原因: 路径写错了: 不是这样: Source"pack://application:,,,/Controls/Styles/xTabControl.xaml" 而是这样: Source"pack://application:,,,/项目名;component/Controls/Styles/xTabControl.xaml …

DevExpress中文教程 - 如何在macOS和Linux (CTP)上创建、修改报表(上)

DevExpress Reporting是.NET Framework下功能完善的报表平台,它附带了易于使用的Visual Studio报表设计器和丰富的报表控件集,包括数据透视表、图表,因此您可以构建无与伦比、信息清晰的报表。 DevExpress Reports — 跨平台报表组件&#x…