0
点赞
收藏
分享

微信扫一扫

【Android Gradle 插件】Android 依赖管理 ② ( 为工程配置依赖仓库 | 为工程构建添加依赖仓库 | classpath 引入依赖库 | 配置依赖仓库 )


文章目录

  • ​​一、为工程配置依赖仓库​​
  • ​​二、为工程构建添加依赖仓库​​
  • ​​三、classpath 引入依赖库​​
  • ​​四、配置依赖仓库​​






一、为工程配置依赖仓库


在 根目录 的 build.gradle 顶层构建脚本 中 , 配置的 allprojects 脚本块 , 是 作用于所有的工程的 ,

allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}

【Android Gradle 插件】Android 依赖管理 ② ( 为工程配置依赖仓库 | 为工程构建添加依赖仓库 | classpath 引入依赖库 | 配置依赖仓库 )_android

如果要为单独的 Module 模块配置依赖仓库 , 则在 模块下 的 build.gradle 构建脚本 中配置 repositories 脚本块配置 即可 , 如 :

plugins {
id 'com.android.application'
}

repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}

android {
compileSdkVersion 32
buildToolsVersion "32.0.0"
}

【Android Gradle 插件】Android 依赖管理 ② ( 为工程配置依赖仓库 | 为工程构建添加依赖仓库 | classpath 引入依赖库 | 配置依赖仓库 )_依赖仓库_02

在 build.gradle 构建脚本 层级配置的 repositories 配置 的作用是 为工程添加依赖仓库 ;

调用的是 Project#repositories 方法 , 方法原型如下 :

public interface Project extends Comparable<Project>, ExtensionAware, PluginAware {
void repositories(Closure var1);
}






二、为工程构建添加依赖仓库


上个章节介绍的 " 为工程添加依赖仓库 “ 与 ” 为工程构建添加依赖仓库 "



这里引入两套概念 :

  • 构建系统 : Gradle 构建过程中需要使用 仓库 和 依赖
  • 工程系统 : 工程中 配置的仓库 和 依赖 , 在代码中调用了这些依赖库的函数 ;


在根目录 build.gradle 顶层构建脚本 中 , buildscript 脚本块中也配置了一套 repositories 仓库 和 dependencies 依赖 , 二者都是在构建过程中使用的 仓库 和 依赖 , 工程中没有用到这些内容 , 是 Gradle 构建使用的 ;

如果 不使用 Gradle 构建 , 使用 Ant 或 Maven 构建工程 , 则 这些 repositories 仓库 和 dependencies 依赖 可以删除 , 但是 " 为工程添加依赖仓库 " 必须保留

下面的 buildscript 脚本块 中 , 配置的 repositories 仓库 和 dependencies 依赖 就是 工程构建过程中使用到的 , 工程本身并没有调用这些依赖库 ;

buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

【Android Gradle 插件】Android 依赖管理 ② ( 为工程配置依赖仓库 | 为工程构建添加依赖仓库 | classpath 引入依赖库 | 配置依赖仓库 )_android_03






三、classpath 引入依赖库


在 根目录 build.gradle 顶层构建脚本 中 , buildscript 脚本块 中配置的依赖库 , 使用的是 classpath 进行配置的 , 没有使用常见的 implementation 或者 compile 引入依赖库 ;

dependencies {
classpath "com.android.tools.build:gradle:4.2.1"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}

implementation 或者 compile 引入的依赖库 需要进入到 打包 , 编译 流程中 , 这些依赖库 编译完成之后 还需要打包到 Apk 文件中 ;

在 构建过程中使用的依赖库 , 如 “com.android.tools.build:gradle:4.2.1” , 这是 Google 开发的 Android Gradle Plugin 自定义插件 , 仅在构建过程中使用

使用 classpath 引入依赖库 , 只会将依赖库添加到编译构建过程中 , 不会打包到 Apk 中 ;






四、配置依赖仓库


在 根目录 build.gradle 顶层构建脚本 中 , " allprojects / repositories " 脚本块 中 配置的

  • google() 是 Google 的 Maven 仓库 ;
  • mavenCentral() 是 Maven 中央仓库 ;
  • jcenter() 是 Jcenter 仓库

allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}

设置本地仓库 : 还可以使用 mavenLocal() 配置本地 Maven 仓库 , 在 Windows 系统中 , 本地 Maven 仓库目录为 " C:\Users\用户名.m2\repository " , 如下图所示 :

【Android Gradle 插件】Android 依赖管理 ② ( 为工程配置依赖仓库 | 为工程构建添加依赖仓库 | classpath 引入依赖库 | 配置依赖仓库 )_依赖管理_04

设置 Maven 私服 : 使用 maven 方法 , 设置一个 Closure 闭包 , 在闭包中设置 url 地址 ;

repositories {
maven {
url 'http://repo.maven.apache.org/maven2'
}

设置 ivy 仓库 :

repositories {
ivy {
url 'http://xxx'
}


举报

相关推荐

0 条评论