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

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 中常见的

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 方法原型如下 :

@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 2 2 个参数 和 3 3 3 个参数 两种重载函数 ,

@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 仓库中 , 也可以是本地的依赖库 ;

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/124972657