在对应module目录下的build.gradle文件中
1、引入maven上传插件
plugins {
id 'maven-publish'
}
2、在最后一行复制这些脚本
def BUILD_VERSION = '0.0.1'
// 源代码一起打包
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
// from android.sourceSets.main.java.sourceFiles
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourcesJar
}
afterEvaluate {
publishing {
publications {
aar(MavenPublication) {
// from components.java
groupId "com.xxx.xxx" // 包名
artifactId "mylibrary" // module的名字 这个不配置的话,默认为 library 的名字,
version BUILD_VERSION // 版本号
artifact androidSourcesJar
artifact("$buildDir/outputs/aar/mylibrary-release.aar")
// //maven-publish does not capture dependencies from this file
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
// Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.api.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
repositories {
maven {
url "https://gitlab.com/api/v4/projects/projectsId/packages/maven"
name "Gitlab"
credentials(HttpHeaderCredentials) {
name = 'Deploy-Token'
value = 'Deploy-Token_Value'
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
}
}
然后把这些修改成你自己的就行了。
groupId:包名
artifactId:module名
artifact("$buildDir/outputs/aar/module名-release.aar")
url中的projectsId改成你自己的。比如projectsId是123.则 url "https://gitlab.com/api/v4/projects/123/packages/maven"
projectsId指的是gitlab项目下的projects Id。
Deploy-Token_Value改为你项目中的Deploy token。
Deploy token获取:
进入你项目,Settings -> Repository -> Deploy tokens ,然后创建deploy token
这里一定要复制一下deploy token ,因为页面关闭之后就再也看不到了
3、生成aar包
进入对应module下 执行clean命令,执行完成,再执行build命令
如图:
4、上传aar
执行publish命令。如上图
5、 aar成功之后,可以在Package Registry中看到。
6、引用aar,在根目录下的build.gradle文件中写入
maven {
url "https://gitlab.com/api/v4/projects/xxx/packages/maven"
name "Gitlab"
credentials(HttpHeaderCredentials) {
name = 'Deploy-Token'
value = 'xxx'
}
authentication {
header(HttpHeaderAuthentication)
}
}
xxx 需改为你的
引用aar包:
implementation 'com.xxx.xxx:mylibrary:0.0.1'