为了解决什么问题?
一直以来安卓终端会编译一些common的aar包,然后在项目中引用、那么一般使用aar包需要几个步骤呢?
- copy 到项目的 lib 目录下
- 项目中 build.gradle 中声明使用lib位置
- 使用命令进行引用
以上方法除了繁琐之外,还存存在A更换了引用包、B 在使用时需要重新 rebuild 才能使用,否则会报错
相遇
初次是看到有人利用github搭建了一个公有的 maven 仓库、后来就在想能否在 gitlab 实现此功能、此处涉及到 github 是公开的库, gitlab 是私有的库。
实现步骤
一、打包配置发布
//打包发布配置开始
apply plugin: 'maven'
uploadArchives {
repositories.mavenDeployer {
def mavenDirPath = file('../') // 本地存放地址
repository(url: "file://${mavenDirPath.absolutePath}")
pom.project {
groupId "com.amos.module.bluetooth" // 包名
artifactId "bluetoothmodule" // module的名字
version '1.0.0'// 版本号
}
}
}
// 源代码一起打包
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
}
注意: groupId 和 artifactId 均为小写
二、生成后的文件上传 git 仓库
将生成到的包上传到 git 项目的根目录、
git add .
git commit -m "Task~"
git push
三、gitlab 后台 生成access_token
- 点击个人中心
- 点击 settings
- Access Tokens
- 输入 Name 、勾选 api 和 read_user 得到生成后的 access_tooken 并记录
四、项目中配置
repositories {
maven {
url "https://<gitlab-url>/api/v4/groups/<group>/-/packages/maven"
name "GitLab"
credentials(HttpHeaderCredentials) {
name = 'Deploy-Token'
value = '<deploy-token>'
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
经典的maven引用、对项目而言,如何找到 url 会比较困难
https://${gitlab}/root/test/raw/master
name 和 value 的值分别为
name = 'Private-Token'
value = '在 gitlab 上获取的 access_token'
五、项目中引用
api 'com.amos.module.bluetooth:bluetoothmodule:1.0.0'
运行成功
本文介绍了如何使用Gitlab Access Token来解决安卓项目中引用aar包的繁琐过程,通过打包发布、上传到Git仓库、生成access_token并在项目中配置,实现私有Maven仓库的功能。
11

被折叠的 条评论
为什么被折叠?



