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'

 
 
  • 1

所以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 pomArtifactId
                        version pomVersion
                        description pomDescription
                        artifactId pomArtifactId
                        groupId pomGroupId
                        packaging '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.release
groupId = pomGroupId
artifactId = pomArtifactId
version = pomVersion
description = 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_USERNAME
                    password = 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"

 
 
  • 1

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

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

gradle uploadArchives

 
 
  • 1

gradle uploadArchives

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

gradle publish

 
 
  • 1

gradle publish

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

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

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

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

 
 
  • 1
  • 2

目录格式如下:
将aar以项目方式引用

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

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

 
 
  • 1
  • 2
  • 3

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 = pomGroupId
artifactId = pomArtifactId
version = pomVersion
description = pomDescription

            artifact 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_USERNAME
                    password = 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值