Android 依赖管理

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

一、为工程添加依赖仓库

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

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

在这里插入图片描述

如果要为单独的 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"
}

在这里插入图片描述
在 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
    }
}

在这里插入图片描述

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

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

四、配置依赖仓库

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

  • google() 是 Google 的 Maven 仓库 ; mavenCentral() 是 Maven 中央仓库 ;
  • jcenter() 是 Jcenter 仓库 , 不过目前已经停止维护 , 尽量不要引入该仓库 , 后期会带来风险
  • Google已经将其依赖库移植到了 Maven 中央仓库 ;
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 地址 ;

repositories {
	maven {
		url 'http://repo.maven.apache.org/maven2'
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值