Android aar包发布到maven仓库总结

library module配置

library module的build.gradle文件,生成aar包含源码、注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
apply plugin: 'maven'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'

// 支付宝 SDK AAR 包所需的配置
implementation 'com.alipay:alipaySdk:15.7.4@aar'
}

uploadArchives { //新增,因为Android Studio gradle 支持maven插件,所以可以添加此task
//configuration = configurations.archives
repositories {
mavenDeployer {
repository(url: 'https://repo.yezhou.me/repository/maven-releases/') {
authentication(userName: '账号', password: '密码')
}
snapshotRepository(url: 'https://repo.yezhou.me/repository/maven-snapshots/') {
authentication(userName: '账号', password: '密码')
}
pom.project {
//version '1.0.0'
version '1.0.0-SNAPSHOT'
artifactId 'paySdk'
groupId 'me.yezhou.pay'
packaging 'aar'
description 'initial version' //更新描述

licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
}
}
}
}

task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}

artifacts {
//aar包增加源码
//archives androidSourcesJar
//aar包增加注释
//archives androidJavadocsJar
}

最后在task中,点击uploadArchives上传

aar引用

问题:解决远程依赖传递失效问题

若远程依赖失效,引用增加{transitive=true} //可选项

本地引用

1
implementation(name: 'aarlibrary', ext: 'aar') {transitive=true}

远程依赖

app moudle的gradle中配置内部maven库路径

1
2
3
4
5
6
7
8
9
10
11
repositories {
maven {
url "https://repo.yezhou.me/repository/maven-public/"
credentials {
username = "账号"
password = "密码"
}
}
}

implementation ('me.yezhou.pay:paySdk:1.0.0-SNAPSHOT@aar') {transitive = true}

拉取不到最新版本解决

在 Gradle 引用SNAPSHOT库版本时,若遇到已经上传最新版本,但是好像没有作用时。请先到项目目录下,使用以下命令清理一下Gradle的缓存,再且试试:

1
2
3
4
5
//Windows
gradlew build --refresh-dependencies

//Mac
./gradlew build --refresh-dependencies

配置封装

在 Library Moudle 中创建一个gradle.properties文件,记录 Nexus 的私服的用户名称和密码,要上传的目标仓库地址,以及上传构件的信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//上传构件的信息
GROUP_ID=me.yezhou.pay
ARTIFACT_ID=paySdk
VERSION=1.0.0
#VERSION=1.0.0-SNAPSHOT 快照库
#VERSION=1.0.0  Release库

//上传的目标仓库地址
SNAPSHOT_REPOSITORY_URL=http://localhost:8081/repository/maven-snapshots/
RELEASE_REPOSITORY_URL=http://localhost:8081/repository/maven-releases/

//Nexus 的私服的用户名称和密码
NEXUS_USERNAME=admin
NEXUS_PASSWORD=admin123

创建nexus_publish.gradle文件,用来生成sources.jar源码包和javadoc.jar方法文档和创建上传的 Gradle Task

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
apply plugin: 'maven'

// 判断版本是Release or Snapshots
def isReleaseBuild() {
return VERSION.toUpperCase().contains("SNAPSHOT") == false
}
def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
def getRepositoryUrl() {
return isReleaseBuild() ? RELEASE_REPOSITORY_URL : SNAPSHOT_REPOSITORY_URL
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = GROUP_ID
pom.artifactId = ARTIFACT_ID
pom.version = VERSION
repository(url: getRepositoryUrl()) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
}
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}

//解决 JavaDoc 中文注释生成失败的问题
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
options.addStringOption('charSet', 'UTF-8')
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
}

然后在 Library Moudle 的build.gradle中引用nexus_publish.gradle

1
apply from: 'nexus_publish.gradle'

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :