前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android Gradle 插件】Gradle 依赖管理 ② ( build.gradle 中的 dependencies 依赖配置 | DependencyHandler#add 方法介绍 )

【Android Gradle 插件】Gradle 依赖管理 ② ( build.gradle 中的 dependencies 依赖配置 | DependencyHandler#add 方法介绍 )

作者头像
韩曙亮
发布2023-03-30 15:51:45
7490
发布2023-03-30 15:51:45
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

Android Plugin DSL Reference 参考文档 :

一、build.gradle 中的 dependencies 依赖配置


org.gradle.api.Project 配置 ( build.gradle 根配置 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html

在 Android Studio 工程中的 Module 下的 build.gradle 的配置 , 其根配置就是 org.gradle.api.Project 配置 ,

build.gradle 中常见的

代码语言:javascript
复制
dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'

    // 矢量图支持库 , 支持 5.0 以下版本手机使用矢量图 , 这个是创建应用时自带的配置
    implementation 'androidx.appcompat:appcompat:1.2.0'

    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

配置 的原型是 org.gradle.api.Project 配置中的 dependencies 方法 , 传入闭包作为参数 ,

dependencies 方法原型如下 :

代码语言:javascript
复制
@HasInternalProtocol
public interface Project extends Comparable<Project>, ExtensionAware, PluginAware {
    /**
     * <p>Configures the dependencies for this project.
     *
     * <p>This method executes the given closure against the {@link DependencyHandler} for this project. The {@link
     * DependencyHandler} is passed to the closure as the closure's delegate.
     *
     * <h3>Examples:</h3>
     * See docs for {@link DependencyHandler}
     *
     * @param configureClosure the closure to use to configure the dependencies.
     */
    void dependencies(Closure configureClosure);
}

二、DependencyHandler#add 方法介绍


dependencies 闭包中的 implementation 实际上是调用了 DependencyHandler 的 add 方法 ,

DependencyHandler # add 方法原型如下 : 该 add 方法有 2 个参数 和 3 个参数 两种重载函数 ,

代码语言:javascript
复制
@Nullable
Dependency add​(String configurationName,
               Object dependencyNotation)
Adds a dependency to the given configuration.
Parameters:
configurationName - The name of the configuration.
dependencyNotation - The dependency notation, in one of the notations described above.
Returns:
The dependency, or null if dependencyNotation is a provider.


Dependency add​(String configurationName,
               Object dependencyNotation,
               Closure configureClosure)
Adds a dependency to the given configuration, and configures the dependency using the given closure.
Parameters:
configurationName - The name of the configuration.
dependencyNotation - The dependency notation, in one of the notations described above.
configureClosure - The closure to use to configure the dependency.
Returns:
The dependency, or null if dependencyNotation is a provider.

对应文档地址 : https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html

add 方法参数介绍 :

  • String configurationName 参数 , 是一个字符串 , 就是在 build.gradle#dependencies 中配置的 " implementation " , " testImplementation " , " compile " , " androidTestImplementation " 等字符串 , 表示依赖类型 ;
  • Object dependencyNotation 参数 , 指的是要加入的依赖 , 如 " ‘androidx.appcompat:appcompat:1.2.0’ " 样式的字符串 , 该依赖一般发布在远程的 maven 仓库中 , 也可以是本地的依赖库 ;
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、build.gradle 中的 dependencies 依赖配置
  • 二、DependencyHandler#add 方法介绍
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档