Android打包上传AAR文件到Maven仓库

4,090 阅读1分钟

1、创建 Android 库

按以下步骤在项目中创建新的库模块:

  1. 依次点击 File > New > New Module
  2. 在随即显示的 Create New Module 窗口中,依次点击 Android Library 和 Next。
  3. 为您的库命名,并为库中的代码选择一个最低 SDK 版本,然后点击 Finish。

2、上传aar包至Maven私服

打开新模块 build.gradle 文件,按如下说明修改:
plugins {
    id 'com.android.library'    // 库模块
    id 'kotlin-android'      
    id 'maven'			// 引入maven plugin
}

def snapshotVersionCode = 101
def snapshotVersion = "1.0.1"

/* 此处省略 android{} 相关配置 */

dependencies {
    // 友盟基础组件库(所有友盟业务SDK都依赖基础组件库)
    implementation "com.umeng.umsdk:common:9.3.6"
    implementation "com.umeng.umsdk:asms:1.2.0"
    implementation "com.umeng.umsdk:apm:1.1.1"
}

/*快照版 maven上传*/
uploadArchives {
    configuration = configurations.archives
    repositories {
        mavenDeployer {
            repository(url: 'http://nexus.xxxxx.com/repository/maven-snapshots') {
                authentication(userName: 'userNameXXXX', password: 'passwordXXXXX')
            }

            pom.project {
                version snapshotVersion + '-SNAPSHOT'
                artifactId 'lib-umeng'
                groupId 'com.xxxxx'
                packaging 'aar'
                description 'lib-umeng Initial submission'
            }
        }
    }
}
上传aar 到maven

选择右侧Gradle > Module Name > upload ,双击uploadArchives运行

image-20210319111842830.png

3、其他项目使用

Project build.gradle添加 maven

allprojects {
    repositories {

	/* 此处省略了其他配置 */
     
        maven { url 'https://dl.bintray.com/umsdk/release' }   // umeng.umsdk相关maven
        maven { url 'https://nexus.xxxxx.com/repository/maven-snapshots' }   // 刚刚aar上传的maven
    }
}

Module 中引用,build.gradle添加如下引用

dependencies {
    api ('com.xxxxx:lib-umeng:1.0.1-SNAPSHOT@aar') {		// 刚刚生成的aar
        implementation "com.umeng.umsdk:common:9.3.6"		// 注意,aar implementation的依赖需要重新引用
        implementation "com.umeng.umsdk:asms:1.2.0"
        implementation "com.umeng.umsdk:apm:1.1.1"
    }
}

4、QA

  1. maven上传报错:

    Execution failed for task ':lib-umeng:uploadArchives'.
    \> Could not publish configuration 'archives'
      \> Failed to deploy artifacts: Could not transfer artifact com.xxxxx:lib-umeng:aar:1.0.1 from/to remote (http://nexus.xxxxx.asia/repository/maven-snapshots): Failed to transfer file: http://nexus.xxxxx.asia/repository/maven-snapshots/com/xxxxx/lib-umeng/1.0.1/lib-umeng-1.0.1.aar. Return code is: 400, ReasonPhrase: Repository version policy: SNAPSHOT does not allow version: 1.0.1.
    

    解决:version snapshotVersion + '-SNAPSHOT' 标记:-SNAPSHOT

参考:developer.android.com/studio/proj…