1、背景
android功能模块开发阶段功能代码频发变化,在发布到正式环境或者仓库前,团队协作,可采用此方式替代传统jar、aar包本地包依赖的方式。
2、优点
- 1、工程越来越大了之后,模块可拆分成独立工程进行开发、协作开发,拆分之后用aar库的方式依赖,独立模块由专属团队来进行维护。
- 2、技术上通过aar的方式也可大大提升编译效率,达到提高研发效率的目的。
- 3、开发阶段不用发布到线上环境,搭建本地仓库,配置与线上环境完全一致。
- 4、解决aar包的递归依赖问题。
3、构建本地仓库的配置方法:
3.1、模块中配置增加如下代码
// 1、plugin配置
plugins {
id 'com.android.library' //这个得需要是一个library,如果是application需要修改一下哦
id 'org.jetbrains.kotlin.android'
id 'maven-publish' // 引入maven-publish插件
// 说明:
// 1、或者在括号外使用 apply plugin: 'maven-publish',
// 2、旧版本是maven, 在gradle 6.0以上版本不支持拉,用maven-publish替代
}
// 2、构建仓库配置信息
afterEvaluate {
publishing {
publications {
def versionV = '0.0.3'
// Creates a Maven publication called "release".
release(MavenPublication) { // 输出release包
// Applies the component for the release build variant.
from components.release
// You can then customize attributes of the publication as shown below.
groupId = 'com.test' // 一般是包名
artifactId = 'com_test-release' //
version = versionV
}
// Creates a Maven publication called “debug”.
debug(MavenPublication) { // 输出debug包
// Applies the component for the debug build variant.
from components.debug
groupId = 'com.test'
artifactId = 'com_test-debug'
version = versionV
}
}
}
}
//3、发布地址, rootProject.projectDir是项目根目录
publishing {
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url = uri("${rootProject.projectDir}/maven-repo-local")
}
}
}
3.2、构建、发布到本地仓库
构建成功后当前目录下新增maven-repo-local目录,则发布成功。
3.3、maven仓库使用
在project(非app)的build.gradle(Android Studio新建工程是在setting.gradle)增加如下代码
maven {
url uri("${rootProject.projectDir}/maven-repo-local")
}
3.4、app.gradle添加引用
在app模块的build.gradle中添加如下依赖,如果是其他模块使用,在相应模块的build.gradle添加如下引用:
implementation 'com.test:com_test-release:0.0.2'
如上几个步骤即可完成本地仓库的搭建和使用。
在开发阶段功能不稳定的情况下,团队协作使用。
功能开发完成后,可以发布到公有仓库或者企业内部私有仓库,仅仅修改project下的build.gradle中的maven仓库地址为线上环境地址即可。
避免aar、jar包的本地依赖方式。
4、参考地址:
使用 Maven Publish 插件: developer.android.google.cn/studio/buil…