Android-私有库搭建

2019-08-07  本文已影响0人  JerrySi

Android私有库搭建网上已经有很多教程了,这里是根据网上的文章结合自己搭建流程做下记录。

私有库搭建目的

每个公司或者每个项目都有公用的模块,比如网络框架、通用工具栏、通用组件。 如果把这部分抽取出来放在某个远端,需要用的项目,直接在Gradle里面配置下,远程拉取这部分公用的模块,从而达到统一性,复用性,避免多个项目维护多个共用库。

  1. 安装Maven的Web管理工具nexus
    image.png
    下完以后是一个压缩包,压缩出来,会看到两个目录 nexus.xx.xx 和 sonatype-work,运行下面命令:
# 切换到自己的目录
cd /usr/local/bin/nexus-2.14.0-01-bundle/nexus-2.14.0-01/bin/
./nexus start

在浏览器执行 localhost:8081/nexus,你会看到nexus,代表成功,最右上角有个登陆,默认账户 admin admin123

  1. 配置nexus
    按照下面图所示配置即可:


    image.png
  2. 上传aar到内网Maven
    a. 在项目的库工程下面加maven.gradle:

apply plugin: 'maven'

def getRepositoryUsername() {
    return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "admin"
}

def getRepositoryPassword() {
    return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "admin123"
}

uploadArchives {
    repositories {
        mavenDeployer {
            // pom.groupId 一般配置 项目的包名
            pom.groupId = MAVEN_GROUP
            // 随意写,不过最好 写库工程的名字
            pom.artifactId = MAVEN_ARTIFACT_ID
            // 版本号
            pom.version = '1.0.0'

            //上传到内网的Maven仓库, 配置内网maven地址,一般不用动
            repository(url: "http://localhost:8082/nexus/content/repositories/releases") {
                authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
            }
        }
    }
}

b. 执行下面命令,成功后,在localhost:8081/nexus下面的release库应该就有上传的aar。

gradle uploadArchives
  1. 如何引用公共库
    a. 同步公共库
    在项目工程的build.gradle里面添加下面链接:
allprojects {
    repositories {
         /***/
        // url 填写你对应服务器的地址
        maven { url 'http://xxx.xxx.xxx.xxx:8082/nexus/content/repositories/releases/' }
    }
}

b. 使用公共库

    compile '包名:包路径:版本号' 

如果断网或者本地如何调试

// 在上面上传地方添加以下逻辑
if (project.hasProperty('local')) {
    repository(url: "file://localhost/" + System.getenv("ANDROID_HOME")
            + "/extras/android/m2repository/")
}
// 执行 ./gradlew uploadArchives -Plocal 进行调试,调试完成以后在本地的aar最好删除掉
上一篇 下一篇

猜你喜欢

热点阅读