上传aar上传到maven,基于AGP7.0

android gradle plugin 7.0后maven插件更新到maven-publish,需要一些修改

发布任务

//module.gradle
plugins {
    id 'maven-publish'
}

....

afterEvaluate {
    publishing {
        publications {
            //定义一个叫myPublication的发布,名字可修改
            myPublication(MavenPublication) {
                //该发布依赖于release任务,多渠道为xxRelease的形式
                from components.release
               
                groupId = 'com.xx.xxx'
                artifactId = 'xxx'
                version = '1.0'
            }
            
          //可以添加多个不同名字的发布任务

        }

        //需要发布的目标仓库
        repositories {
            maven {
                //仓库别名,方便自己识别
                name = 'aliyun'
                if (version.endsWith('SNAPSHOT')) {
                    url = '仓库地址'
                } else {
                    url = 仓库地址'
                }
                //如果需要密码认证添加
                credentials {
                    username = "xxx"
                    password = "xxxx"
                }
            }
        }
    }
}

为发布的依赖添加源码和文档

android{
    ...
   //配置所有变体都添加源码文档
    publishing {
        //release和第一步中 from components.release相同
        //都表示buildtype,如果有多渠道或者debug,做相应修改
        singleVariant("release") {
            withSourcesJar()
            withJavadocJar()
        }
    }
}

发布

会根据上面配置的名字,生成相对应的命令publish[xx]PublicationTo[xxx]Repository,执行即可。

image.png

相关资料

https://android.googlesource.com/platform/tools/base/+/refs/heads/mirror-goog-studio-main/build-system/gradle-api/src/main/java/com/android/build/api/dsl/PublishingOptions.kt#50

https://developer.android.com/studio/publish-library/upload-library#groovy

https://developer.android.com/studio/releases/gradle-plugin#publish-sources-jar

https://docs.gradle.org/current/userguide/publishing_setup.html

你可能感兴趣的:(上传aar上传到maven,基于AGP7.0)