Android Studio使用本地maven管理库文件


很多时候在开发中,我们的多个项目要同时用到同一个库,而当我们要修改库或者库有bug需要统一修复时你常常会顾此失彼造成项目出现问题。因此为了避免这种情况发生,我们可以运用本地maven来管理库文件。


第一步,下载nexus

下载地址http://www.sonatype.com/download-oss-sonatype

关于Maven私服搭建可以参考一下这篇文章:http://www.cnblogs.com/luotaoyeah/p/3791966.html


第二步,搭建好环境后我们就要开始在本地使用了


1.上传库到Maven私服

首先我们要在Studio 上建好自己的库项目,接下来就是我们的gradle配置了,在配置文件gradle.properties中写入

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

#本地库
RELEASE_REPOSITORY_URL=http://localhost:8081/nexus/content/repositories/XXX-Release
SNAPSHOT_REPOSITORY_URL=http://localhost:8081/nexus/content/repositories/XXX-Snapshot

#对应maven的groupId值
GROUP=com.xxx.xxx
#登录nexus oss的用户名
NEXUS_USERNAME=admin
#登录nexus oss的密码
NEXUS_PASSWORD=admin123
# type
TYPE = arr
# description
DESCRIPTION = util lib
VERSION_NAME=0.0.2-SNAPSHOT
POM_ARTIFACT_ID=util


然后在build.gradle文件中这样写

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

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

uploadArchives{
    configuration = configurations.archives
    repositories{
        mavenDeployer{
            pom.groupId = GROUP
            pom.artifactId = POM_ARTIFACT_ID
            pom.version = VERSION_NAME
            repository(url: RELEASE_REPOSITORY_URL) {
                authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
            }
            snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) {
                authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
            }


        }
    }
}
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}
artifacts {
    archives androidSourcesJar
//        archives androidJavadocsJar
}

至此,我们的配置就完成了,最后双击stuido右侧的Gradle Project中相应项目文件下的upload文件夹中uploadArchives上传到本地私服,然后在浏览器中打开上方配置文件中的URL能看到我们项目的文件就大功告成了。


2.在项目中使用我们本地maven仓库的库

相比上传来说,使用就太简单了,只要在你的主工程build.gradle中加入下面中间的部分就可以使用了

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.afollestad.material-dialogs:core:0.8.6.1'

    repositories {
        maven { url "http://你的本地Ip/nexus/content/repositories/XXX-Snapshot" }
        maven { url "http://你的本地Ip/nexus/content/repositories/XXX-Release" }
    }
    compile 'com.XXX.XXX:XXX:0.0.1-SNAPSHOT'   

}

com.xxx.xxx是你的groupID,跟在后面的xxx是你的POM_ARTIFACT_ID

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值