前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android Gradle 插件】Android 依赖管理 ② ( 为工程配置依赖仓库 | 为工程构建添加依赖仓库 | classpath 引入依赖库 | 配置依赖仓库 )

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

作者头像
韩曙亮
发布2023-03-30 17:29:07
1.5K0
发布2023-03-30 17:29:07
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、为工程配置依赖仓库


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

代码语言:javascript
复制
allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}
在这里插入图片描述
在这里插入图片描述

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

代码语言:javascript
复制
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"
}
在这里插入图片描述
在这里插入图片描述

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

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

代码语言:javascript
复制
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 依赖 就是 工程构建过程中使用到的 , 工程本身并没有调用这些依赖库 ;

代码语言:javascript
复制
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
    }
}
在这里插入图片描述
在这里插入图片描述

三、classpath 引入依赖库


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

代码语言:javascript
复制
    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 自定义插件 , 仅在构建过程中使用 , 显然 该依赖库 不需要打包到 Apk 安装包中 , 工程运行并不需要 AGP 插件 ;

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

四、配置依赖仓库


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

  • google() 是 Google 的 Maven 仓库 ;
  • mavenCentral() 是 Maven 中央仓库 ;
  • jcenter() 是 Jcenter 仓库 , 不过目前已经停止维护 , 尽量不要引入该仓库 , 后期会带来风险 ; Google 已经将其依赖库移植到了 Maven 中央仓库 ;
代码语言:javascript
复制
allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

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

在这里插入图片描述
在这里插入图片描述

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

代码语言:javascript
复制
repositories {
	maven {
		url 'http://repo.maven.apache.org/maven2'
	}

设置 ivy 仓库 :

代码语言:javascript
复制
repositories {
	ivy {
		url 'http://xxx'
	}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、为工程配置依赖仓库
  • 二、为工程构建添加依赖仓库
  • 三、classpath 引入依赖库
  • 四、配置依赖仓库
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档