Skip to main content

Publish an Android library to Maven with AAR ,android studio maven aar nexus 上传aar android上传aar到Maven Central 2022年最新最全

1. Github

1.1 注册github

点击下面链接进行注册

2. 注册sonatype账号

2.1 注册


输入Email,Full name,Username,Password,输入验证码,最后点击Sign up就可以注册了。
required



2.2 登录




2.3 新建group id

登录之后点击新建



按照下图中的步骤一步一步来即可,最后别忘记点击新建。

2.4 github新建相关仓库


新建完成之后我们需要在github新建一个仓库,证明你是这个github的使用者(例如这个是我的 https://github.com/996dev)


等待审核通过之后,我们就可以往 mavenCentral 上传aar或者jar包了

3. GPG

3.1 下载软件


下载对应系统的文件



下载完成之后点击安装,直到完成。


3.2 生成密钥

创建密钥命令,在创建的时候一定要记住设置的密码,后面要用的
gpg --full-generate-key
导出私钥,这个后面会用的到,使用下面命令进行导出
gpg --export-secret-keys -o secring.gpg

3.3 上传公钥到服务器

上传到这个三个地方keyserver.ubuntu.com,pgp.mit.edu,keys.openpgp.org

gpg --keyserver hkp://xxxx.xxx --send-keys YOUR KEY ID

3.3.1上传

使用下面命令进行上传

gpg --keyserver keyserver.ubuntu.com --send-keys YOUR KEY ID
gpg --keyserver pgp.mit.edu --send-keys YOUR KEY ID
gpg --keyserver keys.openpgp.org --send-keys YOUR KEY ID

keys.openpgp.org 命令行出问题使用下面的网页直接上传
https://keys.openpgp.org/uploa

3.3.2 验证上传是否成功

使用下面的命令进行验证是否上传成功
gpg --keyserver keyserver.ubuntu.com --recv-keys YOUR KEY ID
gpg --keyserver keys.openpgp.org --recv-keys YOUR KEY ID
gpg --keyserver pgp.mit.edu --recv-keys YOUR KEY ID


4. android gradle

4.1 publish-mavencentral.gradle 

新建publish-mavencentral.gradle文件


apply plugin: 'com.android.library'

apply plugin: 'maven-publish'
apply plugin: 'signing'

//生成文档注释
task androidJavadocs(type: Javadoc) {
    failOnError = false
    source = android.sourceSets.main.java.srcDirs
    ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
    classpath += files(ext.androidJar)
}
//将文档打包成jar
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    archiveClassifier.set('javadoc')
    from androidJavadocs.destinationDir
}
//将源码打包
task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    from android.sourceSets.main.java.srcDirs
}


artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}


//加载资源
Properties properties = new Properties()
InputStream inputStream = project.rootProject.file('gradle.properties').newDataInputStream();
properties.load(inputStream)

// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                // You can then customize attributes of the publication as shown below.
                groupId = properties.getProperty('GROUP_ID')
                artifactId = project.artifactId
                version = project.artifactVersion
                // Adds Javadocs and Sources as separate jars.
//                artifact file("$buildDir/outputs/aar/${project.name}-release.aar")
                artifact androidSourcesJar
                artifact androidJavadocsJar

                pom {
                    name = project.artifactName
                    packaging = 'aar'
                    description = project.artifactDescription
                    url = project.githubUrl

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

                    developers {
                        developer {
                            id = '996dev'
                            name = '996dev'
                            email = '996dev@996.dev'
                        }
                    }

                    scm {
                        url = project.githubUrl
                        connection = "scm:git@${project.githubHttps}"
                        developerConnection = "scm:${project.githubSSH}"
                    }

                }
            }
        }

        repositories {
            maven {
                def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
                def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                credentials {
                    username = properties.getProperty('ossrhUsername')
                    password = properties.getProperty('ossrhPassword')
                }
            }
        }
    }

    signing {
        sign publishing.publications.release
    }
}



4.2 moudle中使用上传gradle



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

ext {
    artifactId = "toast"
    artifactName = 'toast'
    artifactDescription = "androidx toast"
    artifactVersion = "0.0.1"
    githubUrl = "https://github.com/996dev/toast"
    githubHttps = "https://github.com/996dev/toast.git"
    githubSSH = "git@github.com:996dev/toast.git"
}


5. 配置账号密码

在gradle.propertis 填写下面的信息


signing.keyId=后八位
signing.password=刚刚生成的密钥时输入的密码
signing.secretKeyRingFile=D\:\\Key\\secring.gpg 上面到处私钥

ossrhUsername=sonatype账号
ossrhPassword=sonatype密码

GROUP_ID=io.github.996dev

6.上传aar

点击publishing中的task进行上传



7. maven center处理

打开下面的网址
https://s01.oss.sonatype.org/#welcome

登录账号


处理刚刚上传的aar




8.查看验证是否成功

在下面网址中搜索看看有没有上传的aar
https://search.maven.org



Popular posts from this blog

MediaStore.Images.保存图片时生成两张图片的问题 并且在oppo手机上时间是1970-01-01 08:00:00

MediaStore.Images.保存图片时生成两张图片的问题 并且在oppo手机上时间是1970-01-01 08:00:00 正确方式如下 使用方式一 private void saveImage( String originFilePath) { File appDir = new File ( Environment . getExternalStorageDirectory() . getPath() + " /leikebaijing/images " ); if ( ! appDir . exists()) appDir . mkdirs(); String fileName; long dateTaken = System . currentTimeMillis(); if (originFilePath . contains( " / " )) { fileName = originFilePath . substring(originFilePath . lastIndexOf( " / " ) + 1 ); } else { fileName = dateTaken + " jpg " ; } File file = new File (appDir, fileName); if ( ! file . exists()) { try { file . createNewFile(); } catch ( IOException e) { e . printStackTrace(); }