• 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

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

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

    gpg --version

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

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

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

    1. GnuPG needs to construct a user ID to identify your key.
    2. Real name: orixxxx
    3. Email address: orixxxxx@163.com
    4. Comment: orixxxx
    5. You selected this USER-ID:
    6. "orixxxxx (orixxxx) "
    7. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
    8. We need to generate a lot of random bytes. It is a good idea to perform
    9. some other action (type on the keyboard, move the mouse, utilize the
    10. disks) during the prime generation; this gives the random number
    11. generator a better chance to gain enough entropy.
    12. We need to generate a lot of random bytes. It is a good idea to perform
    13. some other action (type on the keyboard, move the mouse, utilize the
    14. disks) during the prime generation; this gives the random number
    15. generator a better chance to gain enough entropy.
    16. gpg: /Users/oriqu/.gnupg/trustdb.gpg: trustdb created
    17. gpg: directory '/Users/oriqu/.gnupg/openpgp-revocs.d' created
    18. gpg: revocation certificate stored as '/Users/oriqu/.gnupg/openpgp-revocs.d/431CBFCDB13DEF3C8C3A2798XXXXYYYYZZZZAAAA.rev'
    19. public and secret key created and signed.
    20. pub ed25519 2022-09-16 [SC] [expires: 2032-09-13]
    21. 431CBFCDB13DEF3C8C3A2798XXXXYYYYZZZZAAAA
    22. uid orixxxxxx (orixxxxx)
    23. sub cv25519 2022-09-16 [E] [expires: 2032-09-13]

    生成secret.gpg的文件

    1. gpg --export-secret-keys -o secret.gpg
    2. //记录文件路径后续要用
    3. /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要配置

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

    4.配置gradle

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

    4.1 配置gradle.properties

    1. #+++++++++++++++++++++++ 上传到maven ++++++++++++++++++++++++
    2. #其中用到的信息,就是签名步骤生成、获取,记录的,最后两个是自己的SonaType账号
    3. # signing info
    4. # 替换成自己的
    5. signing.keyId=ZZZZAAAA
    6. # 替换成自己的
    7. signing.password=xxxxxxx
    8. # 替换成自己的
    9. signing.secretKeyRingFile=/Users/oriqu/.gnupg/secret.gpg
    10. # sonatype account
    11. # 替换成自己的
    12. sonaUsername=Orixxxx
    13. # 替换成自己的
    14. sonaPassword=xxxxxxxxx

    4.2 编写gradle发布脚本

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

    1. publishing {
    2. publications {
    3. release(MavenPublication) {
    4. pom {
    5. // 库信息
    6. name = 'ori-kv'// 替换成自己的
    7. description = 'A tool about android Key-Value'// 替换成自己的
    8. url = 'https://github.com/Orixxxx/ori-kv'// 替换成自己的
    9. // 开放许可证书,可以自己换其他的
    10. licenses {
    11. license {
    12. name = 'The Apache License, Version 2.0'
    13. url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
    14. }
    15. }
    16. // 开发者信息
    17. developers {
    18. developer {
    19. id = 'orixxxx'// 替换成自己的
    20. name = 'Orixxxx'// 替换成自己的
    21. email = 'orixxxx@163.com'// 替换成自己的
    22. }
    23. }
    24. // scm
    25. scm {
    26. connection = 'https://github.com/Orixxxx/ori-kv.git'// 替换成自己的
    27. developerConnection = 'https://github.com/Orixxxx/ori-kv.git'// 替换成自己的
    28. url = 'https://github.com/Orixxxx/ori-kv'// 替换成自己的
    29. }
    30. }
    31. // aar包依赖信息
    32. groupId "io.github.orixxx"// 替换成自己的
    33. artifactId "ori-kv"// 替换成自己的
    34. version "0.0.1-alpha01"// 替换成自己的
    35. afterEvaluate {
    36. from components.release
    37. }
    38. }
    39. }
    40. repositories {
    41. maven {
    42. url "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
    43. credentials {
    44. username sonaUsername // sonatype username
    45. password sonaPassword // sonatype password
    46. }
    47. }
    48. }
    49. }
    50. signing {
    51. sign publishing.publications.release
    52. }

    4.3 目标lib模块的build.gradle

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

    1. plugins {
    2. ....
    3. // 添加maven-publish and signing gradle plugin
    4. id 'maven-publish'
    5. id 'signing'
    6. }
    7. // 应用本地发布到maven的gradle脚本
    8. 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。

  • 相关阅读:
    CentOS虚拟机装完了,不能粘贴window命令行?不能上网?
    LeetCode-475. 供暖器-Java-easy
    (干货)小程序如何黏住千万用户!
    css 背景
    渗透测试-CNA_Bshell程序
    java mysql体检管理系统源码
    net-tools 和 iproute2 笔记221103
    VUE前端判断是电脑端还是移动端
    Texpad所见即所得
    2023-9-30 JZ34 二叉树中和为某一值的路径
  • 原文地址:https://blog.csdn.net/Cupster/article/details/126894744