最近更新到android studio 2020.3.1之后,发现变化挺大的,默认gradle都变成了7.0.2,我直接升级到最近版gradle7.1。突然发现之前6.9来创建maven库的方法没法用了。jitpack.io也不能在线编译了。提交maven*仓库变化也很大。提交最新的私服nexus3上面,变化还是较小的。
gradle7.0之前的方法,请参考这位大佬。↓
Android库发布至MavenCentral流程详解
在这儿我主要写变化后的gradle怎么配置。我也研究折腾了好久才搞定的。
直接上代码。
1. gradle文件配置(C:\Users\用户名.gradle\gradle.properties)
# MavenCentral
signing.keyId=后6位
signing.password=密码
signing.secretKeyRingFile=../gradles/私钥.gpg
ossrhUsername=账号
ossrhPassword=密码
2.在工程根目录创建了gradles文件夹
我把私钥和 publish-maven.gradle 放入这个目录的
然后在根目录 .gitignore 添加忽略 /gradles/ (一定要忽略别把私钥上传到git上面了。)
3.在library工程中,最后一行加入引用
apply from: "${getRootDir().absolutePath}/gradles/publish-maven.gradle"
4.publish-maven.gradle文件内容(直接复制稍微改一下就OK)
apply plugin: 'maven-publish'
apply plugin: 'signing'
def GROUP_ID = 'com.kotlinx'
def ARTIFACT_ID = 'yhttp'
def VERSION_NAME = '1.0.8'
def DESCRIPTION = '这儿是说明'
def GIT_LOCATION = 'github.com/yutils/yhttp'
def GIT_LOCATION_URL = 'https://'+GIT_LOCATION
//仓库地址
def MAVEN_URL = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
def nexusUsername = '账号'
def nexusPassword = '密码'
task sourceJar(type: Jar) {
if (hasAndroidPlugin()) {
println "======> Android"
from android.sourceSets.main.java.srcDirs
//noinspection GroovyAccessibility
archiveClassifier = 'sources'
} else if (hasJavaPlugin()) {
println "======> Java"
from sourceSets.main.allSource
//noinspection GroovyAccessibility
archiveClassifier = 'sources'
}
}
afterEvaluate { project ->
tasks.all { Task task ->
if (task.name.equalsIgnoreCase('publishAarPomPublishPublicationToSnapshotsRepository')) {
task.dependsOn tasks.getByName('assemble')
}
}
}
def hasJavaPlugin() {
if (plugins.hasPlugin("java-library")) {
return true
}
return false
}
def hasAndroidPlugin() {
if (plugins.hasPlugin("com.android.library")) {
return true
}
return false
}
publishing {
println "======> ${project.name}"
println "====> the aar path is " + "$buildDir/outputs/aar/${project.name}-release.aar"
println "====>" + MAVEN_URL
println "====>" + VERSION_NAME
publications {
aarPomPublish(MavenPublication) {
groupId GROUP_ID
artifactId ARTIFACT_ID
version VERSION_NAME
artifact(sourceJar)
artifact("$buildDir/outputs/aar/${project.name}-release.aar")
pom {
packaging 'aar'
name = ARTIFACT_ID
description = DESCRIPTION
url = GIT_LOCATION_URL
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'yujing'
name = 'yujing'
email = '85161441@qq.com'
}
}
scm {
connection = 'scm:git:'+GIT_LOCATION+'.git'
developerConnection = 'scm:git:ssh://'+GIT_LOCATION+'.git'
url = GIT_LOCATION_URL
}
}
pom.withXml {
//asNode().appendNode('测试').appendNode('自定义key', '自定义value')
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
if (it.version != "unspecified") { // 过滤项目内library引用
println "=====>" + it.properties
println "=====>" + it.group + " " + it.name + it.version
if (it.group == null || it.name == null || it.version == null) return
if (it.group == group) return
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.excludeRules.size() > 0) {
def exclusionsNode = dependencyNode.appendNode('exclusions')
it.excludeRules.each { rule ->
def exclusionNode = exclusionsNode.appendNode('exclusion')
exclusionNode.appendNode('groupId', rule.group)
exclusionNode.appendNode('artifactId', rule.module)
}
}
}
}
}
}
}
repositories {
maven {
name "snapshots"
url MAVEN_URL
credentials {
username = nexusUsername
password = nexusPassword
}
}
}
}
// signing 必须在 publishing 配置之后
signing {
sign publishing.publications.aarPomPublish
}
5.发布
双击直接到maven库,后续就不赘述了。
如果你的android studio 没有这个gradle Tasks 选项
1.取消这个勾勾。
2.再同步一下就出来了。
这是我的github地址
https://github.com/yutils
这是安卓官方说明
https://developer.android.com/studio/build/maven-publish-plugin
这是gradle官方说明
https://docs.gradle.org/current/userguide/publishing_maven.html