Android aar包发布到maven仓库总结

library module配置

library module的build.gradle文件,生成aar包含源码、注释

apply plugin: 'maven'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'

    // 支付宝 SDK AAR 包所需的配置
    implementation 'com.alipay:alipaySdk:15.7.4@aar'
}

uploadArchives { //新增,因为Android Studio gradle 支持maven插件,所以可以添加此task
    //configuration = configurations.archives
    repositories {
        mavenDeployer {
            repository(url: 'https://repo.yezhou.me/repository/maven-releases/') {
                authentication(userName: '账号', password: '密码')
            }
            snapshotRepository(url: 'https://repo.yezhou.me/repository/maven-snapshots/') {
                authentication(userName: '账号', password: '密码')
            }
            pom.project {
                //version '1.0.0'
                version '1.0.0-SNAPSHOT'
                artifactId 'paySdk'
                groupId 'me.yezhou.pay'
                packaging 'aar'
                description 'initial version' //更新描述

                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
            }
        }
    }
}

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    //aar包增加源码
    //archives androidSourcesJar
    //aar包增加注释
    //archives androidJavadocsJar
}

最后在task中,点击uploadArchives上传

aar引用

问题:解决远程依赖传递失效问题

若远程依赖失效,引用增加{transitive=true} //可选项

本地引用

implementation(name: 'aarlibrary', ext: 'aar') {transitive=true}

远程依赖

app moudle的gradle中配置内部maven库路径

repositories {
    maven {
        url "https://repo.yezhou.me/repository/maven-public/"
        credentials {
            username = "账号"
            password = "密码"
        }
    }
}

implementation ('me.yezhou.pay:paySdk:1.0.0-SNAPSHOT@aar') {transitive = true}

拉取不到最新版本解决

在 Gradle 引用SNAPSHOT库版本时,若遇到已经上传最新版本,但是好像没有作用时。请先到项目目录下,使用以下命令清理一下Gradle的缓存,再且试试:

//Windows
gradlew build --refresh-dependencies

//Mac
./gradlew build --refresh-dependencies

配置封装

在 Library Moudle 中创建一个gradle.properties文件,记录 Nexus 的私服的用户名称和密码,要上传的目标仓库地址,以及上传构件的信息。

//上传构件的信息
GROUP_ID=me.yezhou.pay
ARTIFACT_ID=paySdk
VERSION=1.0.0
#VERSION=1.0.0-SNAPSHOT  快照库
#VERSION=1.0.0  Release库

//上传的目标仓库地址
SNAPSHOT_REPOSITORY_URL=http://localhost:8081/repository/maven-snapshots/
RELEASE_REPOSITORY_URL=http://localhost:8081/repository/maven-releases/

//Nexus 的私服的用户名称和密码
NEXUS_USERNAME=admin
NEXUS_PASSWORD=admin123

创建nexus_publish.gradle文件,用来生成sources.jar源码包和javadoc.jar方法文档和创建上传的 Gradle Task

apply plugin: 'maven'

// 判断版本是Release or Snapshots
def isReleaseBuild() {
    return VERSION.toUpperCase().contains("SNAPSHOT") == false
}
def getRepositoryUsername() {
    return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
    return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
def getRepositoryUrl() {
    return isReleaseBuild() ? RELEASE_REPOSITORY_URL : SNAPSHOT_REPOSITORY_URL
}
afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                pom.groupId = GROUP_ID
                pom.artifactId = ARTIFACT_ID
                pom.version = VERSION
                repository(url: getRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
            }
        }
    }
    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }
    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = 'javadoc'
        from androidJavadocs.destinationDir
    }
    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.sourceFiles
    }

   //解决 JavaDoc 中文注释生成失败的问题
    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addStringOption('encoding', 'UTF-8')
        options.addStringOption('charSet', 'UTF-8')
    }
    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}

然后在 Library Moudle 的build.gradle中引用nexus_publish.gradle

apply from: 'nexus_publish.gradle'

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/29/summary-of-android-aar-package-release-to-maven-repository/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android aar包发布到maven仓库总结
library module配置 library module的build.gradle文件,生成aar包含源码、注释 apply plugin: 'maven' dependencies { implementation fileT……
<<上一篇
下一篇>>
文章目录
关闭
目 录