• Android——Gradle插件项目根目录settings.gradle和build.gradle


    一、settings.gradle结构分析

    项目根目录下的settings.gradle配置文件示例:

    1. pluginManagement {
    2. /**
    3. * The pluginManagement.repositories block configures the
    4. * repositories Gradle uses to search or download the Gradle plugins and
    5. * their transitive dependencies. Gradle pre-configures support for remote
    6. * repositories such as JCenter, Maven Central, and Ivy. You can also use
    7. * local repositories or define your own remote repositories. The code below
    8. * defines the Gradle Plugin Portal, Google's Maven repository,
    9. * and the Maven Central Repository as the repositories Gradle should use to look for its
    10. * dependencies.
    11. */
    12. repositories {
    13. gradlePluginPortal()
    14. google()
    15. mavenCentral()
    16. }
    17. }
    18. dependencyResolutionManagement {
    19. /**
    20. * The dependencyResolutionManagement.repositories
    21. * block is where you configure the repositories and dependencies used by
    22. * all modules in your project, such as libraries that you are using to
    23. * create your application. However, you should configure module-specific
    24. * dependencies in each module-level build.gradle file. For new projects,
    25. * Android Studio includes Google's Maven repository and the Maven Central
    26. * Repository by default, but it does not configure any dependencies (unless
    27. * you select a template that requires some).
    28. */
    29. repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    30. repositories {
    31. google()
    32. mavenCentral()
    33. flatDir {
    34. dirs 'libs'
    35. }
    36. }
    37. }
    38. rootProject.name='TestAndroidProject'
    39. include ':AliPay'
    40. include ':app'

    (1)pluginManagement配置块

    • 对每个项目和全局的配置。
    • pluginManagement{}块只能出现在两个设置中。 一个是settings.gradle文件,它必须是文件中的第一个代码块,顺序第一出现;另一个是Initialization Scripts,不在本文讨论内。
    1. pluginManagement {
    2. plugins { //插件配置
    3. }
    4. resolutionStrategy {//插件策略配置
    5. }
    6. repositories { //插件运行,依赖的仓库
    7. //按照配置顺序寻找
    8. gradlePluginPortal()
    9. google()
    10. mavenCentral()
    11. //本地仓库配置
    12. maven { url 'file://E:/libs/localMaven/' }
    13. //远程仓库+地址配置
    14. maven { url 'https://repo1.maven.org/maven2/' }
    15. }
    16. }
    • 具体使用官方网址:Gradle-pluginManagement使用
    • pluginManagement 脚本块中的 repositories 配置 , 对应之前的 buildscript 中的 repositories 配置 

    (2)dependencyResolutionManagement配置块

    settings.gradle 中部分

    1. //使用Catalog统一依赖版本 开启VERSION_CATALOG
    2. enableFeaturePreview('VERSION_CATALOGS')
    3. // 指定Gradle需要的用来搜索或下载【依赖dependency】的代码库
    4. dependencyResolutionManagement {
    5. // 然而,为了配置一些模块特定的依赖,你需要在每一个模块的模块级build.gradle文件中进行配置说明
    6. repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    7. repositories {
    8. // 以下是AS默认configure的repositories
    9. google()
    10. jcenter()
    11. mavenCentral()
    12. }
    13. //使用Catalog统一依赖版本
    14. versionCatalogs{
    15. libs {
    16. version('paging', '3.1.1')
    17. version('glide', '4.14.2')
    18. version('lifecycle', '2.4.1')
    19. version('appcompat', '1.4.1')
    20. alias('paging-runtime').to('androidx.paging', 'paging-runtime').versionRef('paging')
    21. alias('paging-guava').to('androidx.paging', 'paging-guava').versionRef('paging')
    22. alias('paging-rxjava2').to('androidx.paging', 'paging-rxjava2').versionRef('paging')
    23. alias('glide-v4').to('com.github.bumptech.glide', 'glide').versionRef('glide')
    24. alias('lifecycle-livedata').to('androidx.lifecycle', 'lifecycle-livedata-ktx').versionRef('lifecycle')
    25. alias('lifecycle-viewmodel').to('androidx.lifecycle', 'lifecycle-viewmodel-ktx').versionRef('lifecycle')
    26. alias('androidx-appcompat').to('androidx.appcompat', 'appcompat').versionRef('appcompat')
    27. }
    28. }
    29. }

    • 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

    1. // 构建脚本
    2. buildscript {
    3. // 定义全局变量,常用于版本管理
    4. // 变量在子模块的build.gradle中直接以: $NAME 的形式调用
    5. ext {
    6. compose_version = '1.0.1'
    7. lifecycleVersion = '2.3.1'
    8. kotlinVersion = '1.5.21'
    9. ktlintVersion = '0.41.0'
    10. coroutines = '1.5.0'
    11. moshi_version = '1.12.0'
    12. }
    13. }
    14. // 依赖URL
    15. // 之前于settings.gradle定义的是整体的仓库位置,而这里可以视为定义具体的依赖位置
    16. // plugins定义项目中所有模块共用的 Gradle 依赖项
    17. // apply false 不可以用于子模块的build.gradle
    18. plugins {
    19. id 'com.android.application' version '7.1.0-rc01' apply false
    20. id 'com.android.library' version '7.1.0-rc01' apply false
    21. id 'org.jetbrains.kotlin.android' version '1.5.21' apply false
    22. }
    23. // 定义清理build目录的对应方法
    24. task clean(type: Delete) {
    25. delete rootProject.buildDir
    26. }

    • buildscript

    需要注意的是:
    1)、buildscript代码段必须是build.gradle文件的第一个代码段;
    2)、对于多模块构建,项目的buildscript代码段声明的依赖关系可用于所有子模块的构建脚本;
    3)、构建脚本的依赖可能是gradle插件

    (2)子目录下build.gradle

    1. // 子模块的plugins
    2. // 请注意!这里就不可以定义apply false了
    3. plugins {
    4. id 'com.android.application'
    5. id 'org.jetbrains.kotlin.android'
    6. id 'dagger.hilt.android.plugin'
    7. }
    8. // 应用插件 Kapt
    9. // 这是一款kotlin专用的依赖管理插件,推荐每个项目都添加!
    10. apply plugin: 'kotlin-kapt'
    11. // android
    12. // 定义所有模块构建设置
    13. android {
    14. // 定义编译SDK
    15. // 表示你的项目可以使用低于(或等于)该版本的所有API
    16. compileSdk 32
    17. // 定义默认配置
    18. defaultConfig {
    19. // 工件ID
    20. applicationId "com.example.character"
    21. // 最低可接受SDK版本
    22. minSdk 21
    23. // 最高可接受SDK版本
    24. targetSdk 32
    25. // 给开发者看的项目版本
    26. versionCode 1
    27. // 给客户看的项目版本
    28. versionName "1.0"
    29. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    30. vectorDrawables {
    31. useSupportLibrary true
    32. }
    33. }
    34. // 定义构建类型
    35. // 默认的构建类型有两种:debug(构建时会默认打上debug签名) release(构建时默认不打任何签名)
    36. buildTypes {
    37. release {
    38. minifyEnabled false
    39. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    40. }
    41. }
    42. // 如果你用的是JDK8,那么请添加这个两个配置以提供支持
    43. compileOptions {
    44. sourceCompatibility JavaVersion.VERSION_1_8
    45. targetCompatibility JavaVersion.VERSION_1_8
    46. }
    47. kotlinOptions {
    48. jvmTarget = '1.8'
    49. }
    50. // 构建特性
    51. buildFeatures {
    52. compose true
    53. }
    54. // compose设置
    55. composeOptions {
    56. kotlinCompilerExtensionVersion compose_version
    57. }
    58. // 资源文件配置
    59. packagingOptions {
    60. resources {
    61. excludes += '/META-INF/{AL2.0,LGPL2.1}'
    62. }
    63. }
    64. }
    65. // 在这里直接把你需要添加的依赖贴进去就好了
    66. // 贴完后点sync即可
    67. dependencies {
    68. implementation 'androidx.core:core-ktx:1.7.0'
    69. implementation "androidx.compose.ui:ui:$compose_version"
    70. implementation "androidx.compose.material:material:$compose_version"
    71. implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    72. implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    73. implementation 'androidx.activity:activity-compose:1.3.1'
    74. testImplementation 'junit:junit:4.13.2'
    75. androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    76. androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    77. androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    78. debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    79. // dagger -hilt
    80. implementation "com.google.dagger:hilt-android:2.38.1"
    81. kapt "com.google.dagger:hilt-android-compiler:2.38.1"
    82. kapt "androidx.hilt:hilt-compiler:1.0.0"
    83. // Lifecycle components
    84. implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
    85. implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0"
    86. implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
    87. implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
    88. // Kotlin components
    89. implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    90. implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
    91. // networking
    92. implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    93. implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
    94. implementation("com.squareup.okhttp3:okhttp:4.9.0")
    95. implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")
    96. implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
    97. kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
    98. // coil
    99. implementation("io.coil-kt:coil-compose:1.4.0")
    100. }

    三、settings和build组合搭配方式

    (1)settings.gradle全局配置,根build.gradle不做配置

    • gradle.properties配置全局变量 

    • settings.gradle配置文件

    1. pluginManagement {
    2. /**
    3. * The pluginManagement.repositories block configures the
    4. * repositories Gradle uses to search or download the Gradle plugins and
    5. * their transitive dependencies. Gradle pre-configures support for remote
    6. * repositories such as JCenter, Maven Central, and Ivy. You can also use
    7. * local repositories or define your own remote repositories. The code below
    8. * defines the Gradle Plugin Portal, Google's Maven repository,
    9. * and the Maven Central Repository as the repositories Gradle should use to look for its
    10. * dependencies.
    11. */
    12. repositories {
    13. gradlePluginPortal()
    14. google()
    15. mavenCentral()
    16. }
    17. resolutionStrategy {
    18. }
    19. plugins{
    20. //是用来构建 apk 的 gradle 插件
    21. id 'com.android.application' version '${agpVersion}' apply false
    22. //是用来构建 Android Library 的 gradle 插件 (jar, aar)
    23. id 'com.android.library' version '${agpVersion}' apply false
    24. // 一个Gradle插件,用于将所有依赖项比如lib文件下依赖的jar包和项目代码打包到单个Jar文件中 官方地址[https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow#groovy-usage]
    25. id 'com.github.johnrengelman.shadow' version "${agpShadow}" apply false
    26. }
    27. }
    28. dependencyResolutionManagement {
    29. /**
    30. * The dependencyResolutionManagement.repositories
    31. * block is where you configure the repositories and dependencies used by
    32. * all modules in your project, such as libraries that you are using to
    33. * create your application. However, you should configure module-specific
    34. * dependencies in each module-level build.gradle file. For new projects,
    35. * Android Studio includes Google's Maven repository and the Maven Central
    36. * Repository by default, but it does not configure any dependencies (unless
    37. * you select a template that requires some).
    38. */
    39. /**
    40. * RepositoriesMode.PREFER_PROJECT : 解析依赖库时 , 优先使用本地仓库 , 本地仓库没有该依赖 , 则使用远程仓库 ;
    41. * RepositoriesMode.FAIL_ON_PROJECT_REPOS : 解析依赖库时 , 强行使用远程仓库 , 不管本地仓库有没有该依赖库 ;
    42. * */
    43. repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    44. repositories {
    45. google()
    46. mavenCentral()
    47. flatDir {
    48. dirs 'libs'
    49. }
    50. }
    51. }
    52. rootProject.name='TestAndroidProject'
    53. include ':AliPay'
    54. include ':app'
    • 根目录build.gradle文件

    1. plugins{
    2. //是用来构建 apk 的 gradle 插件
    3. id 'com.android.application' apply false
    4. //是用来构建 Android Library 的 gradle 插件 (jar, aar)
    5. id 'com.android.library' apply false
    6. // 一个Gradle插件,用于将所有依赖项比如lib文件下依赖的jar包和项目代码打包到单个Jar文件中 官方地址[https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow#groovy-usage]
    7. id 'com.github.johnrengelman.shadow' apply false
    8. }
    9. task clean(type: Delete) {
    10. delete rootProject.buildDir
    11. }

    (2)根build.gradle配置,setting.gradle仅配置基础

    • settings.gradle配置

    1. rootProject.name='WiFiAndroidProject'
    2. include ':DashboardView'
    3. include ':BluetoothKitlibrary'
    4. include ':niftydialogeffectslibrary'
    5. include ':TwinklingRefreshLayout_library'
    6. include ':AliPay'
    7. include ':app'
    • build.gradle配置

    1. //通常定义了项目中的模块使用的通用插件版本
    2. // Top-level build file where you can add configuration options common to all sub-projects/modules.
    3. buildscript {
    4. repositories {
    5. gradlePluginPortal()
    6. google()
    7. mavenCentral()
    8. jcenter()
    9. maven { url 'https://repo1.maven.org/maven2/' }
    10. //
    11. }
    12. dependencies {
    13. //配置gradle插件
    14. classpath "com.android.tools.build:gradle:8.1.0"
    15. }
    16. }
    17. allprojects {
    18. repositories {
    19. flatDir {
    20. dirs 'libs'
    21. }
    22. flatDir {
    23. dirs project(':AliPay').file('libs')
    24. }
    25. google()
    26. mavenCentral()
    27. maven { url 'https://jitpack.io' }
    28. maven { url 'https://repo1.maven.org/maven2/' }
    29. }
    30. }
    31. task clean(type: Delete) {
    32. delete rootProject.buildDir
    33. }

  • 相关阅读:
    2023年最佳项目管理软件排行榜揭晓!
    笔者近期感想
    elasticsearch命令大全
    “存储随笔“官方定制python代码月饼免费领取
    算力资源A100/V100/910都不限时免费使用的开源社区,你还不知道?
    数电实验-----实现74LS153芯片扩展为8选1数据选择器以及应用(Quartus II )
    学习Opencv(蝴蝶书/C++)——1. 前言 和 第1章.概述
    Apache Spark 的基本概念
    【P15 Python基础】Pandas
    ubuntu环境搭建记录
  • 原文地址:https://blog.csdn.net/yilvqingtai/article/details/134444964