為了方便別人使用我們開發(fā)的Android Library,一般我們都會把Android Library打成aar包,并將aar包發(fā)布到Maven倉庫當中。如果是開源項目就可以把包發(fā)布到Maven Central倉庫或者JCenter倉庫中;如果是公司內部使用,一般公司內部會自己搭建私有Maven倉庫,就把包發(fā)布到私有Maven倉庫當中,以方便別人直接使用。
準備工作
首先得有一個Android項目。我以自己GitHub上的一個項目customprogressbar作說明。
項目結構圖:
這個項目有兩個module:customprogressbar和example。
customprogressbar
這個module是Android Library類型的,提供自定義進度條的功能
example
這個module使用了customprogressbar提供的功能
此時,由于兩個module在同一project下面,example要使用customprogressbar,只需要在example的build.gradle添加如下依賴即可。
example/build.gradle
compile project(':customprogressbar')
1、下面,我們看如何把customprogressbar發(fā)布到Maven倉庫當中。首先是使用Nexus搭建的Maven私有倉庫。
發(fā)布到私有Maven倉庫
我們使用Nexus在本地搭建了一個Maven倉庫。Nexus使用方法不介紹了,大家自己Google。
啟動Nexus,在瀏覽器輸入:http://localhost:8081/nexus/ 即可看到Maven倉庫管理界面。
下面貼上customprogressbar的build.gradle的腳本代碼
customprogressbar/build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}
apply from: './nexus-push.gradle'
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}
apply from: './nexus-push.gradle'
好,可以看到,基本上都是Android Studio自動生成的代碼,只有最后一句不是。最后一句引用了另一個gradle腳本文件,我們的項目發(fā)布代碼都寫在這個腳本文件中。
customprogressbar/nexus-push.gradle
apply plugin: 'maven'
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://localhost:8081/nexus/content/repositories/releases/") {
authentication(userName: "deployment", password: "deployment123")
}
pom.groupId = 'com.hebut.czh'
pom.artifactId = 'customprogressbar'
pom.version = '0.0.1'
pom.project {
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
}
}
}
}
首先需要引用maven插件;然后定義了2個任務:androidSourcesJar和androidJavadocsJar,這兩個任務分別用于對Java sources打包和Java doc進行打包;接著我們對uploadArchives.repositories閉包進行一些配置,包括倉庫的url地址,上傳所需的用戶名和密碼,以及pom屬性。
OK, 腳本編寫完之后,在Android Studio的Terminal面板執(zhí)行如下命令
gradlew uploadArchives
如圖所示:
發(fā)布成功后就可以在nexus查看已發(fā)布的項目:
好的,接下來看Example如何使用這個Maven倉庫中的aar包。
首先在項目根目錄下的build.gradle文件添加倉庫url地址。
build.gradle
allprojects {
repositories {
jcenter()
maven {
url "http://localhost:8081/nexus/content/repositories/releases/"
}
}
}
接下來在example的build.gradle中添加依賴即可使用。
/example/build.gradle
compile 'com.hebut.czh:customprogressbar:0.0.1'
1發(fā)布到JCenter倉庫
JCenter倉庫是由bintray提供并維護,這個倉庫是類似Maven中央倉庫。只要你能連上Internet,你就可以通過Gradle或者Maven去下載倉庫中的依賴包到你自己的項目中。
當然,我們也可以通過Android Studio把aar包發(fā)布到JCenter當中,這樣就可以讓更多的開發(fā)者使用我們提供的aar包。
把項目發(fā)布到JCenter中需要3步:
- 注冊bintray賬戶
- 編寫Gradle腳本,把項目發(fā)布到你bintray賬戶下的Maven倉庫
- 同步到JCenter倉庫
第1步和第3步都很簡單,下面先講第2步。
同樣在customprogressbar的build.gradle文件中編寫代碼,跟前邊相比就一處不一樣,這里就貼上不一樣的代碼。
customprogressbar/build.gradle
apply from: './jcenter-push.gradle'
還是一樣,上傳的主要腳本代碼寫在另一個文件當中。由于,bintray在Maven倉庫外面又加了一個package層,使用package來對倉庫進行管理,所以,上傳的方式有點不同。
customprogressbar/jcenter-push.gradle
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'
def siteUrl = 'https://github.com/codezhanghao/CustomProgressbar'
def gitUrl = 'https://github.com/codezhanghao/CustomProgressbar.git'
group = 'com.hebut.czh'
version = '0.0.1'
install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
name 'custom progressbar for android'
url siteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'czh'
name 'ZHANG.Hao'
email '809026704@qq.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
version {
name = '0.0.1'
desc = 'custom progress bar 0.0.1'
}
repo = 'maven'
name = 'custom-progressbar'
licenses = ['Apache-2.0']
vcsUrl = gitUrl
publish = true
}
}
這里首先應用了兩個插件:com.jfrog.bintray和com.github.dcendents.android-maven。
第一個插件是bintray官網提供的,用來配置bintray的Maven倉庫的特有信息,bintray的用戶名,apikey,以及提供了一個名為bintrayUpload任務用來上傳項目的bintray的Maven倉庫,對應代碼:
bintray {
...
...
}
需要注意的使用,bintray的用戶名和密碼我放在了local.properties文件里面
local.properties:
sdk.dir=D:\develop\Android\sdk
bintray.user=czh //bintray的用戶名
bintray.apikey=********* //修改成你自己的apikey,可以在bintray官網上查到
第二個插件是我Google出來的,用來配置項目的POM信息的,對應代碼:
install {
repositories.mavenInstaller {
...
...
}
}
接下來在Android Studio的Terminal面板執(zhí)行命令:
gradle bintrayUpload
如圖所示:
成功后,可以在bintray網站上的自己的Maven倉庫中查看自己的發(fā)布的項目:
至此,第2步就結束了。
至于第3步,將你的bintray下的Maven倉庫中的項目include到JCenter倉庫中,就點擊一個按鈕即可。具體就是,點擊進入你剛剛上傳項目package的詳細頁當中,在右下角有一個Add to JCenter按鈕,點擊它,然后寫上一些message,最后點擊send按鈕,就完事了。下面就是等待bintray工作人員進行審核。幾個小時后,審核通過,bintray會給你發(fā)展站內消息,通知你項目已經include到JCenter當中。
如圖所示:
OK,發(fā)布項目到JCenter即成功了。
之后,任何人任何時候,只需要添加一句依賴腳本代碼就能直接使用你編寫的customprogressbar中的功能。
compile 'com.hebut.czh:customprogressbar:0.0.1@aar'
然后Gradle會自動從JCenter倉庫中下載對應的依賴包到本地項目中。