> 文章列表 > Android上传aar到本地仓

Android上传aar到本地仓

Android上传aar到本地仓

由于最近项目结构变动,经常需要将module项目打成aar供其他项目引用,在使用过程中,遇到本地module项目上传maven后其引用的aar其他项目需要重复引入的问题,所以在此记录下,将本地module项目打包aar上传本地仓和第三方aar上传本地仓的方法.

一、将本地module项目打包成aar上传到本地仓

1.在项目根目录下新建mavenConfig.properties文件,存放本地仓配置信息

NEXUS_REPOSITORY_URL = http://localhost:8081/repository/maven-local/
NEXUS_USERNAME = admin
NEXUS_PASSWORD = admin123

2.在项目根目录新建maven_deployer.gradle,用于保存aar打包任务和配置pom信息

2.1 Gradle 7.0以下时,打包aar上传需要使用插件

apply plugin: 'maven'

所以maven_deployer.gradle内容如下

def pomGroupId = 'com.dyj.test'
def pomArtifactId = this.getName()
def pomVersion = '1.0.0'
def pomDescription = "upload aar test ${pomVersion},测试aar打包"apply plugin: 'maven'
uploadArchives {repositories {mavenDeployer {//从本地文件读取仓库账号和密码File propFile = new File('mavenConfig.properties')if (propFile.canRead()) {Properties props = new Properties()props.load(new FileInputStream(propFile))if (props != null && props.containsKey('NEXUS_REPOSITORY_URL')&& props.containsKey('NEXUS_USERNAME')&& props.containsKey('NEXUS_PASSWORD')) {def NEXUS_REPOSITORY_URL = props['NEXUS_REPOSITORY_URL']def NEXUS_USERNAME = props['NEXUS_USERNAME']def NEXUS_PASSWORD = props['NEXUS_PASSWORD']repository(url: NEXUS_REPOSITORY_URL) {authentication(userName: NEXUS_USERNAME,password: NEXUS_PASSWORD)// 上传名称 版本号 介绍 等pom.project {name pomArtifactIdversion pomVersiondescription pomDescriptionartifactId pomArtifactIdgroupId pomGroupIdpackaging 'aar'}}} else {println '请确认maven配置文件有没有问题'}}}}
}

2.2 Gradle 7.0以上时,打包aar上传需要使用插件

apply plugin: 'maven-publish'

所以maven_deployer.gradle内容如下

def pomGroupId = 'com.dyj.test'
def pomArtifactId = this.getName()
def pomVersion = '1.0.0'
def pomDescription = "upload aar test ${pomVersion},测试aar打包"apply plugin: 'maven-publish'
afterEvaluate {publishing {publications {release(MavenPublication) {from components.releasegroupId = pomGroupIdartifactId = pomArtifactIdversion = pomVersiondescription = pomDescription}}repositories {maven {Properties versionProperties = new Properties()File proFile = project.rootProject.file('mavenConfig.properties');if(proFile.canRead()){InputStream versionIs = proFile.newDataInputStream()versionProperties.load(versionIs)versionIs.close()allowInsecureProtocol true  // 如果PUBLISH_URL是http,此处必须为true,如果是https,则可以删除此项name = "Nexus"  // 配置后在Gradle > Tasks > publishing 下会显示对应的名称,容易区分url = uri(versionProperties.NEXUS_REPOSITORY_URL)credentials {username = versionProperties.NEXUS_USERNAMEpassword = versionProperties.NEXUS_PASSWORD}println("maven " + versionProperties.NEXUS_REPOSITORY_URL)println("maven " + versionProperties.NEXUS_USERNAME)println("maven " + versionProperties.NEXUS_PASSWORD)}else {println("maven properties 属性文件不存在")}}}}
}

3.在需要打包aar的module项目的build.gralde中添加引用maven_deployer.gradle

apply from: "${rootProject.rootDir}/maven_deployer.gradle"

4.执行gradle 任务,即可将module打包成aar上传本地仓

4.1 使用2.1方式引入maven插件的,可以在module的gradle的task中找到uploadArchives,双击执行,或者直接在Terminal中运行

gradle uploadArchives

Android上传aar到本地仓

4.2 使用2.2方式引入maven-publish插件的,可以在module的gradle的task中找到publish,双击执行,或者直接在Terminal中运行

gradle publish

Android上传aar到本地仓

二、将第三方aar上传到本地仓

为了方便module项目打包aar,module项目可能引入第三方aar,每次打包完module项目后其他项目还需要主动引入第三方aar比较麻烦,所以将第三方aar上传一份到本地仓.

1.将第三方aar以module项目方式添加到工程中,在对应module的build.gradle中,添加配置

configurations.maybeCreate("default")
artifacts.add("default",file('JsBridgeLib-release.aar'))

目录格式如下:
Android上传aar到本地仓

2.和普通module项目相同的方式,在项目的settings.gradle中引入各aar的module项目

include ':LocalRepo:AlipaySdk_aar'
include ':LocalRepo:Apppaysdk_aar'
include ':LocalRepo:JsBridgeLib_aar'

3.由于第三方aar已经是生成好了的aar包,所以我们只需要执行上传maven的时候指定需要上传的Publishing artifacts文件即可,所以将上面上传maven_deployer.gradle中稍微修改下,如下

def pomGroupId = 'com.dyj.third.lib'
def pomArtifactId = 'JsBridge'
def pomVersion = '1.0.0'
def pomDescription = "upload JsBridge ${pomVersion},JsBridge sdk"apply plugin: 'maven-publish'
afterEvaluate {publishing {publications {release(MavenPublication) {groupId = pomGroupIdartifactId = pomArtifactIdversion = pomVersiondescription = pomDescriptionartifact file("JsBridgeLib-release.aar")}}repositories {maven {Properties versionProperties = new Properties()File proFile = project.rootProject.file('mavenConfig.properties');if(proFile.canRead()){InputStream versionIs = proFile.newDataInputStream()versionProperties.load(versionIs)versionIs.close()allowInsecureProtocol true  // 如果PUBLISH_URL是http,此处必须为true,如果是https,则可以删除此项name = "Nexus"  // 配置后在Gradle > Tasks > publishing 下会显示对应的名称,容易区分url = uri(versionProperties.NEXUS_REPOSITORY_URL)credentials {username = versionProperties.NEXUS_USERNAMEpassword = versionProperties.NEXUS_PASSWORD}println("maven " + versionProperties.NEXUS_REPOSITORY_URL)println("maven " + versionProperties.NEXUS_USERNAME)println("maven " + versionProperties.NEXUS_PASSWORD)}else {println("maven properties 属性文件不存在")}}}}
}

4.执行同上的gradle 任务,即可将第三方aar上传本地仓

参考资料
https://developer.android.com/studio/publish-library/upload-library?hl=zh-cn