一文搞定Android库打包成aar/jar发布至Maven Central

前置:

  1. 本文使用的AGP7.0+
  2. 发布插件使用【maven-publish】,并非【maven】,【maven】已经逐渐废弃,AGP7.0+后已经不支持
  3. 使用Android studio 2021.1.1 patch1
  4. 使用Mac OS系统;Windows仅GPG 命令、文件路径的差异

1.申请SonaType账号

在平台用邮箱注册,后面步骤依赖这个邮箱接收最新回复、问题进展

Sign up for Jira - Sonatype JIRA

2.创建Issue,验证GitHub

Log in - Sonatype JIRA

a.步骤如下图123,勾选配置域,不清楚就无脑全勾上,附件不用勾。


b.填充问题表单,签名项目及类型,保持如下图一致即可,填写后面的带*选项

c. 填写:groupId可以是自己拥有的域名,可以是GitHub,这里举例GitHub。

其中,【yourname】 要替换成你自己的GitHub用户名

d. 创建完毕后,会有专人跟进,留意时差对方是否在上班时间,可以关注自己注册的邮箱,有进度、回复,会邮件提醒到。

e. 按要求,做验证操作

3.配置GPG

总的就是三个步骤:安装GPG、生成key、创建secretKeyRingFile

3.1 安装GPG

命令行窗口安装gpg ,window安装可以参考:Android库发布至MavenCentral流程详解_GitLqr的博客-CSDN博客

mac 未安装 homebrew ,是无法使用brew 命令的,可以参考教程:MacOS安装和使用Homebrew图文详解_Cupster的博客-CSDN博客_mac安装brew

brew install gpg
// 如果中途遇到某个库安装失败,就执行 brew install xxx,单独安装它,然后再重新执行 brew install gpg

安装成功执行 【gpg --version】会有版本信息

gpg --version

3.2 生成key,设置密码,生成secret.gpg

gpg --full-gen-key
# 需要记住输入的密码,后面发布aar需要配置到 signing.password

中间会有询问key 类型,都选择默认【default】即可,最后记得保存好生成的信息,将来会用到。

GnuPG needs to construct a user ID to identify your key.

Real name: orixxxx
Email address: orixxxxx@163.com
Comment: orixxxx
You selected this USER-ID:
    "orixxxxx (orixxxx) <orixxxxx@163.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: /Users/oriqu/.gnupg/trustdb.gpg: trustdb created
gpg: directory '/Users/oriqu/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/Users/oriqu/.gnupg/openpgp-revocs.d/431CBFCDB13DEF3C8C3A2798XXXXYYYYZZZZAAAA.rev'
public and secret key created and signed.

pub   ed25519 2022-09-16 [SC] [expires: 2032-09-13]
      431CBFCDB13DEF3C8C3A2798XXXXYYYYZZZZAAAA
uid                      orixxxxxx (orixxxxx) <orixxxx@163.com>
sub   cv25519 2022-09-16 [E] [expires: 2032-09-13]

生成secret.gpg的文件

gpg --export-secret-keys -o secret.gpg
//记录文件路径后续要用
/Users/oriqu/.gnupg/secret.gpg
 

3.3 记录【发布要素】

3.3.1 signing.password

        在3.2中,生成key过程中设置的密码,就是signing.password

3.3.2 signing.keyId

        在3.2中,生成的文件中,有一个.rev文件,其名字的末尾8位就是signing.keyId

        然后我们要把这个key id 【ZZZZAAAA】上传到服务器,告诉服务器我们的keyId

gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys ZZZZAAA

↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

+++【注意】这步骤必须做,否则后面会在【nexus repository】无法close,会报签名id无法验证成功的错误❌
+++++++++++++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++++++++++++

3.3.3 signing.secretKeyRingFile

        在3.2中,最后一个命令生成的secret.gpg,记录的路径,就是signing.secretKeyRingFile

3.3.4 最后结果

        这个后面gradle.properties要配置

# signing info
signing.keyId=ZZZZAAAA
signing.password=xxxxxxx
signing.secretKeyRingFile=/Users/oriqu/.gnupg/secret.gpg

4.配置gradle

        配置如下,注意修改成自己的信息

4.1 配置gradle.properties

#+++++++++++++++++++++++   上传到maven  ++++++++++++++++++++++++
#其中用到的信息,就是签名步骤生成、获取,记录的,最后两个是自己的SonaType账号
# signing info
# 替换成自己的
signing.keyId=ZZZZAAAA
# 替换成自己的
signing.password=xxxxxxx
# 替换成自己的
signing.secretKeyRingFile=/Users/oriqu/.gnupg/secret.gpg

# sonatype account
# 替换成自己的
sonaUsername=Orixxxx
# 替换成自己的
sonaPassword=xxxxxxxxx

4.2 编写gradle发布脚本

        在当前项目根目录编写【maven-publish.gradle】

publishing {
    publications {
        release(MavenPublication) {
            pom {
                // 库信息
                name = 'ori-kv'// 替换成自己的
                description = 'A tool about android Key-Value'// 替换成自己的
                url = 'https://github.com/Orixxxx/ori-kv'// 替换成自己的
            	// 开放许可证书,可以自己换其他的
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
            	// 开发者信息
                developers {
                    developer {
                        id = 'orixxxx'// 替换成自己的
                        name = 'Orixxxx'// 替换成自己的
                        email = 'orixxxx@163.com'// 替换成自己的
                    }
                }
            	// scm
                scm {
                    connection = 'https://github.com/Orixxxx/ori-kv.git'// 替换成自己的
                    developerConnection = 'https://github.com/Orixxxx/ori-kv.git'// 替换成自己的
                    url = 'https://github.com/Orixxxx/ori-kv'// 替换成自己的
                }
            }
        	// aar包依赖信息
            groupId "io.github.orixxx"// 替换成自己的
            artifactId "ori-kv"// 替换成自己的
            version "0.0.1-alpha01"// 替换成自己的

            afterEvaluate {
                from components.release
            }
        }
    }
    repositories {
        maven {
            url "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            credentials {
                username sonaUsername // sonatype username
                password sonaPassword // sonatype password
            }
        }
    }
}

signing {
    sign publishing.publications.release
}

4.3 目标lib模块的build.gradle

        在lib模块的build.gradle 中引用4.2的发布脚本

plugins {
    ....
    // 添加maven-publish and signing gradle plugin
    id 'maven-publish'
    id 'signing'
}
// 应用本地发布到maven的gradle脚本
apply from: '../maven-publish.gradle'

4.4 sync

        最后sync 同步一下,task 就有发布aar 到maven的入口,也可命令行直接执行.

        如果在gralde里没有Task 列表,自行百度,设置一下Android Studio ,有个开关勾选,就会出现task列表

5. 发布到maven并在新项目引用

5.1 执行发布脚本

        如4.4.执行publish后,可以在如下链接,看到发布的lib_xxx.aar

Nexus Repository Manager

5.2 close包并release

        选择对应lib 包,点击Close ,执行成功后,点击release就会发布。通常会有30分钟延迟

        然后可在如下链接,按自己一开始设置的groupid,找到对应的aar 包

        Index of /repositories/releases

        Btw:

                想在https://mvnrepository.com/搜索到,需要要等上一段时间,时间有长有短。

5.3 引用这个库

        1.在新的项目中,引用.

implementation 'io.github.orixent:ori-kv:0.0.1-alpha1'

        2.点击sync ,即可在左侧项目结构 External Libraries找到对应的导包

总结:

       至此,一个Android库发布到maven central的流程完整走完了。后续仅需要配置新lib 名,版本信息即可发布到maven central。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值