一、 打包并发布项目
1. 编写gradle脚本
在需要发布为公共类库的module的build.gradle文件引入maven插件:
在此gradle文件根部添加发布代码:
apply plugin: 'maven'
def MAVEN_LOCAL_PATH = 'http://127.0.0.1:8888/repository/android-release/' //服务端ip地址端口及放置的目录
def ARTIFACT_ID = 'checksign' //artifactId
def VERSION_NAME = '1.0.0' //版本名称
def GROUP_ID = 'com.eaway.corelib' //groupId
def PROJECT_NAME = 'corelib'
def ACCOUNT = 'admin'
def PASSWORD = 'admin123'
//脚本:将Lib打成aar包上传至maven私有库
uploadArchives {
repositories {
mavenDeployer {
repository(url: MAVEN_LOCAL_PATH) {
authentication(userName: ACCOUNT, password: PASSWORD)
}
pom.project {
name PROJECT_NAME
groupId GROUP_ID
artifactId ARTIFACT_ID
version VERSION_NAME
packaging 'aar'
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}
}
}
2. 打包发布
在后台窗口进入所在目录,输入指令gradle uploadArchives(前提是先配置gradle环境变量)
3. 在服务端查看是否已生成并上传成功
二、引用已发布的公共类库
1. 在项目根目录下的build.gradle文件中的allprojects -> repositories中添加maven服务端地址:
allprojects {
repositories {
jcenter()
maven { url "http://127.0.0.1:8888/repository/android-release/" }
}
}
2. 在需要引用公共类库的module的build.gradle中的dependencies加入
compile 'groupId:artifactId:version'
如
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.0.1'
testCompile 'junit:junit:4.12'
compile 'com.eaway.corelib:checksign:1.0.0'
}