Android Maven仓库使用文档

Author Avatar
夜孤黎 1月 04, 2018
  • 在其它设备中阅读本文章

为了方便与简化Android依赖库的发布与使用,建议将依赖库打包成aar格式并push至局域网内搭建的maven仓库,这样使用者只需要在android studio中增加对该库的远程依赖即可使用。aar既可打包java代码,也可同时打包资源文件与jni文件。

1、简介:

目前,部门内android maven仓库地址为:

1、http://192.165.56.51:8081/artifactory/libs-snapshot

2、http://192.165.56.51:8081/artifactory/libs-release

2、打包并上传至Maven:

  1. 在工程根目录下的build.gradle文件中添加依赖:classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"

  2. 在打包的module目录中的build.gradle文件中进行如下配置:

    apply plugin: 'com.android.library'
    
    /**这里引入打包必须的plugin*/
    apply plugin: 'com.jfrog.artifactory'
    apply plugin: 'maven-publish'
    
    //包名
    def packageName = 'com.xxx.xxxx'
    //版本名称,这里为SNAPSHOT包
    def libraryVersion = 'v1.x.x-SNAPSHOT'
    //maven仓库地址
    def artifactory_url = 'http://192.165.56.51:8081/artifactory'
    //maven账户名
    def artifactory_username = 'admin'
    //maven账户密码
    def artifactory_password = 'sumavisionrd'
    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.2"
    
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 25
            versionCode 1
            versionName libraryVersion
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        //这部分代码可以定义打包出的aar文件的文件名           
        //示例码生成的aar文件名称为"android_platform v1.x.x-SNAPSHOT.aar"
        android.libraryVariants.all {
            variant ->
                variant.outputs.all {
                    output ->
                        def outputFile = output.outputFile
                        if (outputFile != null
                            &&  outputFile.name.endsWith('.aar')
                            && 'release'.equals(variant.buildType.name)) 
                        {                             
                        outputFileName = new File("android_platform${defaultConfig.versionName}.aar")
                        }
                }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile 'com.android.support:support-v4:25.1.0'
    }
    
    //这里配置pom.xml的参数
    publishing {
        publications {
            aar(MavenPublication) {
                groupId packageName
                version = libraryVersion
                artifactId project.getName()
    
                // Tell maven to prepare the generated "*.aar" file for publishing
                artifact("$buildDir/outputs/aar/android_platform ${libraryVersion}.aar")
    
                //The publication doesn't know about our dependencies, so we have to manually add them to the pom
                pom.withXml {
                    //Creating additional node for dependencies
                    def dependenciesNode = asNode().appendNode('dependencies')
    
                    //Defining configuration names from which dependencies will be taken (debugCompile or releaseCompile and compile)
                    def configurationNames = ["releaseCompile", 'compile']
                    configurationNames.each { configurationName ->
                        project.configurations.getByName(configurationName).allDependencies.each {
                            if (it.group != null && it.name != null) {
                                def dependencyNode = dependenciesNode.appendNode('dependency')
                                dependencyNode.appendNode('groupId', it.group)
                                dependencyNode.appendNode('artifactId', it.name)
                                dependencyNode.appendNode('version', it.version)
    
                                if (it.hasProperty('optional') && it.optional) {
                                    dependencyNode.appendNode('optional', 'true')
                                }
    
                                //If there are any exclusions in dependency
                                if (it.excludeRules.size() > 0) {
                                    def exclusionsNode = dependencyNode.appendNode('exclusions')
                                    it.excludeRules.each { rule ->
                                        def exclusionNode = exclusionsNode.appendNode('exclusion')
                                        exclusionNode.appendNode('groupId', rule.group)
                                        exclusionNode.appendNode('artifactId', rule.module)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    //这里配置maven服务器信息
    artifactory {
        contextUrl = artifactory_url
        publish {
            repository {
                // The Artifactory repository key to publish to
                repoKey = libraryVersion.endsWith('SNAPSHOT') ? 'libs-snapshot-local' : 'libs-release-local'
    
                username = artifactory_username
                password = artifactory_password
            }
            defaults {
                // Tell the Artifactory Plugin which artifacts should be published to Artifactory.
                publications('aar')
                publishArtifacts = true
    
                // Properties to be attached to the published artifacts.
                properties = ['qa.level': 'basic', 'dev.team': 'core']
                // Publish generated POM files to Artifactory (true by default)
                publishPom = true
            }
        }
    }
    
  3. sync后依次执行gradle命令:

  4. 完成上传。

3、远程依赖Maven中的aar文件:

  1. 在项目根目录下的build.gradle文件中添加maven仓库地址;

    repositories {    
        google()  
        jcenter()    
        maven { url "http://192.165.56.51:8081/artifactory/libs-snapshot"}
    }
    
  2. 在app目录下的build.gradle文件中添加对aar远程仓库的依赖:

    compile 'com.sumavision.android_platform:android_platform:v1.x.x-SNAPSHOT'

  3. sync后完成依赖。

4,其它:

由于你我都懂的原因,国内连接部分国外代码仓库并下载依赖库时会出现下载速度慢甚至于无法下载的问题,因此可以采用翻墙的方式优化。我在可以翻墙的本机上搭建了远程仓库,针对于MavenCenter()、jCenter()与google()这三个代码仓库可以实现下载与缓存,连接该远程仓库可以优化大家的下载体验(前提是我的电脑开机了),仓库地址为:

1、http://192.165.56.59:8081/artifactory/libs-release

This blog is under a CC BY-NC-SA 3.0 Unported License
本文链接:http://yeguli.cn/2018/01/04/Android-Maven仓库使用文档/