成为Wheel Maker:发布Android库到MavenCentral
Xayah Lv3

一、前言

Android发展日新月异,由此诞生了许多强大的第三方库,例如GlideOkHttp。我们当然也可以发布自己的库,当一回Wheel Maker!以前大家用Jcenter作为平台,而现在大家用MavenCentral

二、步骤

1. 注册Sonatype账号

MavenCentralSonatype运营,因此我们先 注册 一个 Sonatype账号

2. 创建问题

首先填写项目问题类型

  • 项目:Community Support - Open Source Project Repository Hosting (OSSRH)
  • 问题类型:New Project 然后根据具体情况填写库信息 收到了来自工作人员回复 回复中提到我们需要验证GroupId对应的域名,我这里为了方便就选择第二种方式,将GroupId改为io.github.xayahsususu
    回到GitHub,新建一个名为OSSRH-78521(根据回复中的仓库名)的仓库

编辑这个问题,把GroupId改为io.github.xayahsususu更新,然后将状态重新更新为开放等待回复
几分钟之后,正常情况下会受到完成的回复,注意回复中提到的s01.oss.sonatype.org,这是我们管理上传库地址,可能每个时期得到的地址不同

问题状态变为已解决

3. 申请GPG密钥

发布到MavenCentral的库需要签名,因此我们 下载 相应工具来生成密钥,这里以Windows为例
安装Gpg4win,运行Kleopatra

左上角文件 - 新建密钥对 - 创建个人 OpenPGP 密钥对

完成后在主界面打开生成的密钥,记住指纹后8位(后面要用到),然后生成吊销证书保存

接下来回到主界面右键密钥 - 在服务器上发布,若网络没有问题即可发布成功

发布成功后,再次右键密钥 - 备份私钥,将私钥导出(将后缀改为.gpg)
接下来打开密钥,修改密码

4. Android库发布脚本配置

在项目根目录下新建publish-mavencentral.gradle
输入以下代码(如果上文中回复得到的地址不为s01.oss.sonatype.org,则将其改为得到的地址

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
apply plugin: 'maven-publish'
apply plugin: 'signing'

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

ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
println "Found secret props file, loading props"
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
ext[name] = value
}
} else {
println "No props file, loading env vars"
}
publishing {
publications {
release(MavenPublication) {
// The coordinates of the library, being set from variables that
// we'll set up in a moment
groupId PUBLISH_GROUP_ID
artifactId PUBLISH_ARTIFACT_ID
version PUBLISH_VERSION

// Two artifacts, the `aar` and the sources
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
artifact androidSourcesJar

// Self-explanatory metadata for the most part
pom {
name = PUBLISH_ARTIFACT_ID
description = PUBLISH_DESCRIPTION
// If your project has a dedicated site, use its URL here
url = PUBLISH_GITHUB_URL
licenses {
license {
name = PUBLISH_LICENSE_NAME
url = PUBLISH_LICENSE_URL
}
}
developers {
developer {
id = PUBLISH_DEVELOPER_ID
name = PUBLISH_DEVELOPER_NAME
email = PUBLISH_DEVELOPER_EMAIL
}
}
// Version control info, if you're using GitHub, follow the format as seen here
scm {
connection = PUBLISH_CONNECTION
developerConnection = PUBLISH_CONNECTION_DEVELOPER
url = PUBLISH_CONNECTION_URL
}
// A slightly hacky fix so that your POM will include any transitive dependencies
// that your library builds upon
withXml {
def dependenciesNode = asNode().appendNode('dependencies')

project.configurations.implementation.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
repositories {
// The repository to publish to, Sonatype/MavenCentral
maven {
// This is an arbitrary name, you may also use "mavencentral" or
// any other name that's descriptive for you
name = PUBLISH_MAVEN_NAME

def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
// You only need this if you want to publish snapshots, otherwise just set the URL
// to the release repo directly
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

// The username and password we've fetched earlier
credentials {
username ossrhUsername
password ossrhPassword
}
}
}
}
signing {
sign publishing.publications
}

打开项目根目录中的local.properties,添加以下代码

1
2
3
4
5
signing.keyId=$密钥指纹后八位(无空格)
signing.password=$密钥密码
signing.secretKeyRingFile=$导出密钥的文件路径,例如:E\:\\ProgramDesign\\AndroidProjects\\Signature\\Xayah_0xEB95FB34_SECRET.gpg
ossrhUsername=$Sonatype帐号
ossrhPassword=$Sonatype密码

打开待发布库目录下的build.gradle,行位添加以下代码(根据示例参考修改填写)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ext {
PUBLISH_GROUP_ID = "io.github.xayahsususu"
PUBLISH_ARTIFACT_ID = 'materialyoufileexplorer'
PUBLISH_DESCRIPTION = 'A file explorer with the style of Material You.'
PUBLISH_GITHUB_URL = 'https://github.com/XayahSuSuSu/Android-MaterialYouFileExplorer'
PUBLISH_VERSION = '1.0.0'
PUBLISH_LICENSE_NAME = 'GPL-3.0'
PUBLISH_LICENSE_URL = 'https://choosealicense.com/licenses/gpl-3.0/'
PUBLISH_DE_URL = 'https://choosealicense.com/licenses/gpl-3.0/'
PUBLISH_DEVELOPER_ID = 'Xayah'
PUBLISH_DEVELOPER_NAME = 'Xayah'
PUBLISH_DEVELOPER_EMAIL = 'zds1249475336@gmail.com'
PUBLISH_CONNECTION = 'scm:git:github.com/XayahSuSuSu/Android-MaterialYouFileExplorer.git'
PUBLISH_CONNECTION_DEVELOPER = 'scm:git:ssh://github.com/XayahSuSuSu/Android-MaterialYouFileExplorer.git'
PUBLISH_CONNECTION_URL = 'https://github.com/XayahSuSuSu/Android-MaterialYouFileExplorer/tree/main'
PUBLISH_MAVEN_NAME = "MaterialYouFileExplorer"

}
apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"

5. 发布Android库到MavenCentral

打开Settings - Experimental - 取消勾选 Do not build Gradle task list during Gradle sync

点击左上角File - Sync Project with Gradle Files

完成后打开右侧Gradle窗口,双击运行build - assemble

编译完成后,双击运行publishing - publishReleasePublicationTo${LibraryName}Repository 发布到https://s01.oss.sonatype.org (具体地址由上文回复中可得)

打开管理地址后,进入Staging Repositories,即可看到我们上传的库记录

打开库记录,点击Close输入描述Confirm,稍等片刻即可刷新查看,若Close成功,则点击Release发布。发布成功后,之前的问题会收到发布成功回复

假以时日,我们即可在https://repo1.maven.org/maven2/https://search.maven.org/ 中查看到我们发布的库信息

6. 使用已发布的Android库

打开需要引用库的项目的app目录下的build.gradle,输入以下代码引用
格式为:

1
implementation '$PUBLISH_GROUP_ID:$PUBLISH_ARTIFACT_ID:$PUBLISH_VERSION'

例如:

1
implementation 'io.github.xayahsususu:materialyoufileexplorer:1.0.0'

三、参考文章

  • 本文标题:成为Wheel Maker:发布Android库到MavenCentral
  • 本文作者:Xayah
  • 创建时间:2022-02-25 23:45:40
  • 本文链接:http://acmezone.top/2022/02/25/成为Wheel-Maker:发布Android库到MavenCentral/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!