[Note] 2021-04-19 Android Studio 发布多渠道aar到maven

已有环境

  1. macOS Big Sur 11.2.3
  2. JDK: 1.8.0_242-release
  3. Android Studio 开发环境

更新日志

2022-04-28 10:45:58
pom 的方式存在依赖丢失问题,所以手动打包各个依赖(包括多渠道依赖)到 pom 文件中

后续计划更新新篇章:Android Gradle 6.0+ 的 GMM 方式

本地Maven私服搭建

下载 Nexus Repository OSS

https://help.sonatype.com/repomanager3/download,OSX archive

配置

解压下载文件到某个目录,cd 到 nexus 的 bin 目录下(当然也可以配置到PATH中),执行 ./nexus run 启动 nexus
打开Nexus管理后台页面:http://localhost:8081/,登陆,默认用户名admin,默认密码保存在 ../nexus-3.30.0-01-mac/sonatype-work/nexus3/admin.password 登陆进来后根据步骤进行配置,nexus-3.30.0-01 的版本的设置已经做成网页,直接在网页操作便可

Setting.png

仓库类型:

  • maven-central 为代理,用来代理远程仓库,也就是可以缓存远程仓库(github上的库等)
  • maven-releases/maven-snapshots 为宿主仓库,分别用来部署团队内部的发布版本组件和快照版本组件(也就是仅在内网部可用仓库)

上传单个渠道到Maven

在 library 模块下的 build.gradle 最外层添加

apply plugin: 'maven'

uploadArchives {
    repositories.mavenDeployer {
        name = 'mavenCentralReleaseDeployer' //名称固定
        repository(url: "http://localhost:8081/repository/maven-releases/") { //仓库地址
            authentication(userName: "admin", password: "admin123") //仓库用户名和密码
        }
        pom.version = '1.0.1'  //版本号
        pom.artifactId = "testlibrary" //aar名称
        pom.groupId = "com.lunix.testlibrary" //包名
        pom.name = "sdk"
        pom.packaging = 'aar' //打包格式,aar固定
    }
}

上传多个渠道

为 lib 配置多渠道和上传配置

android {
...
    flavorDimensions("env")
    productFlavors {
        cpu {
            dimension "env"
        }

        npu {
            dimension "env"
        }
    }
    sourceSets {
        main {
            java.srcDirs += ["${projectDir}/src/main"]
        }
        cpu {
            java.srcDirs += ["${projectDir}/src/cpu"]
        }
        npu {
            java.srcDirs += ["${projectDir}/src/npu"]
        }
    }
}

...

import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact //导入类

//配置要上传的文件信息
artifacts {
    // 执行上传的包和类型
    archives new DefaultPublishArtifact("npu", "aar", "aar", null, new Date(), new File("${getBuildDir().absolutePath}/outputs/aar", "testlibrary-cpu-release.aar"))
    archives new DefaultPublishArtifact("cpu", "aar", "aar", null, new Date(), new File("${getBuildDir().absolutePath}/outputs/aar", "testlibrary-cpu-release.aar"))
}

uploadArchives {
    repositories.mavenDeployer {
        name = 'mavenCentralReleaseDeployer' //名称固定
        repository(url: "http://localhost:8081/repository/maven-releases/") {
            authentication(userName: "admin", password: "admin123")
        }

        addFilter('npu') { artifact, file ->
            artifact.name.contains("npu")
        }
        addFilter('cpu') { artifact, file ->
            artifact.name.contains("cpu")
        }

        pom('npu').groupId = "com.lunix.testlibrary" //包名
        pom('npu').artifactId = "testlibrary" //aar名称
        pom('npu').version = '1.0.3-npu'  //版本号
        // 2022-04-28 10:40:02 更新,pom 的方式只能提交一个包含依赖库的aar包,这里需要单独处理
        // dependences 的依赖问题,即手动打包各个依赖到 pom 中
        pom('npu').withXml {
            def dependenciesNode = asNode().appendNode('dependencies')
            configurations.implementation.allDependencies.each { dependency ->
                def dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', dependency.group)
                dependencyNode.appendNode('artifactId', dependency.name)
                dependencyNode.appendNode('version', dependency.version)
                dependencyNode.appendNode('scope', 'implementation')
            }
            // just for: npu,如果有 npuImplementation 依赖方式的库
            // configurations.npuImplementation.allDependencies.each { dependency ->
            //     def dependencyNode = dependenciesNode.appendNode('dependency')
            //     dependencyNode.appendNode('groupId', dependency.group)
            //     dependencyNode.appendNode('artifactId', dependency.name)
            //     dependencyNode.appendNode('version', dependency.version)
            //     dependencyNode.appendNode('scope', 'compile')
            // }
        }

        pom('cpu').groupId = "com.lunix.testlibrary" //包名
        pom('cpu').artifactId = "testlibrary" //aar名称
        pom('cpu').version = '1.0.3-cpu'  //版本号
        // 2022-04-28 10:40:02 更新,pom 的方式只能提交一个包含依赖库的aar包,这里需要单独处理
        // dependences 的依赖问题,即手动打包各个依赖到 pom 中
        pom('cpu').withXml {
            def dependenciesNode = asNode().appendNode('dependencies')
            configurations.implementation.allDependencies.each { dependency ->
                def dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', dependency.group)
                dependencyNode.appendNode('artifactId', dependency.name)
                dependencyNode.appendNode('version', dependency.version)
                dependencyNode.appendNode('scope', 'implementation')
            }
            // just for: cpu,如果有 cpuImplementation 依赖方式的库
            // configurations.cpuImplementation.allDependencies.each { dependency ->
            //     def dependencyNode = dependenciesNode.appendNode('dependency')
            //     dependencyNode.appendNode('groupId', dependency.group)
            //     dependencyNode.appendNode('artifactId', dependency.name)
            //     dependencyNode.appendNode('version', dependency.version)
            //     dependencyNode.appendNode('scope', 'compile')
            // }
        }
    }
}

然后执行编译和上传

# 项目目录终端
$ ./gradlew :testlibrary:aR # 编译
$ ./gradlew :testlibrary:uploadArchives # 上传

补充:另外一个写法


apply plugin: 'maven-publish'

publishing {
    publications {
        npu(MavenPublication) {
            groupId = "com.lunix.testlibrary" //包名
            artifactId = "testlibrary" //aar名称
            version = '1.0.3-npu'  //版本号
            artifact "${getBuildDir().absolutePath}/outputs/aar/testlibrary-npu-release.aar"
        }
        cpu(MavenPublication) {
            groupId = 'com.lunix.testlibrary' //包名
            artifactId = "testlibrary" //aar名称
            version = '1.0.3-cpu'  //版本号
            artifact "${getBuildDir().absolutePath}/outputs/aar/testlibrary-cpu-release.aar"
        }
    }

    repositories {
        maven {
            url = 'http://localhost:8081/repository/maven-releases/'
            credentials {
                username 'admin'
                password 'admin123'
            }
        }
    }
}

编译上传

# 项目目录终端
$ ./gradlew :testlibrary:aR # 编译
$  ./gradlew :testlibrary:publish # 上传

后台管理页面


flavors

单独上传某个库的,注释掉其中一个 archives

库导入/使用

  • 配置私服地址,根目录 build.gradle 在 repositories 节点增加
allprojects {
    repositories {
        ...
        maven { url = 'http://localhost:8081/repository/maven-releases/' }
    }
}
  • 需要使用到库的模块的 build.gradle 配置
implementation 'com.lunix.testlibrary:testlibrary:1.0.3-cpu'

你可能感兴趣的:([Note] 2021-04-19 Android Studio 发布多渠道aar到maven)