Android发布到maven central

准备1

创建上传空间并验证,用自己的github验证
https://central.sonatype.com/publishing/namespaces

准备2

生成 GPG 密钥(ai就行)

查看私钥

gpg --list-secret-keys --keyid-format LONG

输出结果

sec   rsa4096/ABCDEF1234567890 2024-01-01 [SC]

      1234567890ABCDEF1234567890ABCDEF1234

uid   [ultimate] Your Name <[you@email.com](mailto:you@email.com)>

生成签名key

gpg --export-secret-keys -a ABCDEF1234567890 > signing.key

查看短key id

gpg --list-secret-keys --keyid-format SHORT
输出结果

sec   rsa4096/F15BDBF5 2026-02-06 [SC]

      2439E7FADB65F3B96D997E9637D204E3F15BDBF5

uid           [ 绝对 ] xxxx <[xxxx@qq.com](mailto:xxxx@qq.com)>

ssb   rsa4096/F64AB908 2026-02-06 [E]

gradle.properties GPG配置

signing.keyId=短key id
signing.password=密码
signing.gnupg.useLegacyGpg=true
signing.keyFile=生成签名key地址

配置发版

项目根目录 build.gradle.kts

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.android.library) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.kotlin.compose) apply false
    //增加插件
    id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
}

nexusPublishing {
    repositories {
        sonatype {
            // 发布到 Central Publishing Portal 的 staging endpoint
            nexusUrl.set(uri("https://central.sonatype.com/api/v1/maven/"))

            // 使用 Central Publishing Portal token
            username = project.findProperty("centralUsername")?.toString() ?: ""
            password = project.findProperty("centralPassword")?.toString() ?: ""
        }
    }

    // 所有子项目的 groupId
    packageGroup.set("nameSpace")
}

lib build.gradle.kts配置

android {
.......xxxxxxxxxxxx
 publishing {
        singleVariant("release") {
            withSourcesJar()
        }
    }
}

android同级增加

afterEvaluate {
    task javadocJar(type: Jar) {
        archiveClassifier = 'javadoc'
        from "$buildDir/javadoc"
    }

    publishing {
        publications {
            release(MavenPublication) {
                groupId = 'nameSpace.lib'
                artifactId = 'id'
                version = project.property("lib_websocket_version") ?: "1.0.0"
                from components.findByName("release")

                artifact javadocJar

                pom {
                    name = project.property("pom.name")
                    description = project.property("pom.description")
                    url = project.property("pom.url")

                    licenses {
                        license {
                            name = project.property("pom.license.name")
                            url = project.property("pom.license.url")
                        }
                    }

                    developers {
                        developer {
                            id = project.property("pom.developer.id")
                            name = project.property("pom.developer.name")
                            email = project.property("pom.developer.email")
                        }
                    }

                    scm {
                        connection = project.property("pom.scm.connection")
                        developerConnection = project.property("pom.scm.connection")
                        url = project.property("pom.scm.url")
                    }
                }
            }
        }
    }

    signing {
        required = true

        def keyFile = findProperty("signing.keyFile")
        def keyPass = findProperty("signing.password")
        def keyId   = findProperty("signing.keyId")

        if (!keyFile || !keyPass || !keyId) {
            throw new GradleException(
                    "PGP signing config missing: " +
                            "keyFile=${keyFile}, keyId=${keyId}"
            )
        }

        useInMemoryPgpKeys(
                keyId,
                file(keyFile).text,
                keyPass
        )

        sign publishing.publications.release
    }
}

项目根目录 build.gradle.kts 增加配置

pom.name=test Library
pom.description=A high-performance library for Android based on Netty
pom.url=https://github.com/xxxx
pom.scm.url=https://github.com.xxxxx.git
pom.scm.connection=scm:git:https://github.com/xxxxx.git
pom.license.name=MIT License
pom.license.url=https://opensource.org/licenses/MIT
pom.developer.id=xxx
pom.developer.name=xx
pom.developer.email=xxx.com

# Library version
lib_websocket_version=1.0.0

centralUsername=xxx
centralPassword=xxx

发布到本地

image.png

本地发布路径

~/.m2/repository/<groupId路径>/<artifactId>/<version>/

依赖测试本地路径 settings.gradle.kts

pluginManagement {
    repositories {
        gradlePluginPortal() 
        google()
        mavenCentral()
        mavenLocal() //增加
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        mavenLocal() //增加
    }
}

implementation("groupId:artifactId:version")

手动发布

https://central.sonatype.com/publishing/deployments
上传 本地发布路径所有文件

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容