使用Gradle發(fā)布aar項目到JCenter倉庫
目的
發(fā)布自己的android library(也就是aar)到公共的jcenter倉庫,所有的人都能用gradle最簡單的方式引用。
為什么選擇jcenter,它兼容maven,而且支持更多形式倉庫,android studio最新版本已經(jīng)默認(rèn)jcenter了。
一、注冊賬號
在https://bintray.com/上注冊一個賬號。
注意: 注冊時請選擇 “For an Open Source Account Sign Up Here”
二、創(chuàng)建一個maven倉庫
- 點擊“Add New Organization”
- Name輸入“maven”,Type選擇“Maven”
- Create
三、記錄API Key
注冊后,在https://bintray.com/profile/edit頁面的左邊菜單的最下面有API Key,點擊后:
復(fù)制明文API key,后面會用到。
四、新建項目(以MatrixView為例)
在Android Studio中New一個Project。
完成之后在MatrixView下會有一個build.gradle,在buildscript/dependencies中添加兩個classpath:
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
這是兩個幫助發(fā)布項目的插件,完整的project build.gradle請查看這里。
五、新建模塊
在當(dāng)前Project中New一個Module,比如我這里叫matrixview,這個默認(rèn)matrixview其實已經(jīng)是一個完整的Android Library,就可以以aar的形式發(fā)布了。
完成之后在matrixview下也會有一個build.gradle,這個build.gradle是今天的重中之重。
六、構(gòu)建build.gradle
默認(rèn)的build.gradle大概是這樣的:
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
下面我們一步步修改補(bǔ)充這個build.gradle。
1. 添加插件
// 根節(jié)點添加
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
2. 定義版本
// 根節(jié)點添加
version = "1.0"
3. 定義相關(guān)網(wǎng)站
有單獨的官方網(wǎng)站那是最好的,沒用的話就用git網(wǎng)址就可以了。
// 根節(jié)點添加
def siteUrl = 'https://github.com/aystshen/MatrixView' // project homepage
def gitUrl = 'https://github.com/aystshen/MatrixView.git' // project git
4. 定義Group
舉個例子,當(dāng)我們引用recylerview的時候是這樣的:
compile 'com.android.support:recyclerview-v7:21.0.2'
引號內(nèi)字符串以冒號分割為三部分,第一部分就是group,第二部分是name, 第三部分是上面定義的version。
// 根節(jié)點添加
group = "com.ayst.matrixview"
5. 定義pom并打包aar
上傳到j(luò)center至少需要四個文件,除了打包的aar之外,還需要pom和javadoc,source,否則是通不過jcenter審核的。不過不用緊張,這些我們都可以用腳本生成。
// 根節(jié)點添加
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'Less Code 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 'hbshen'
name 'haibo.shen'
email 'ayst_shen@me.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
6. 打包javadocjar和sourcejar
這兩個也是上傳到j(luò)center必須要的。
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
7. 上傳到Jcenter倉庫
上傳到j(luò)center的網(wǎng)站BinTray,需要用戶驗證,需要2個值:
// 示例值, 僅供參考
// your bintray user name
// your bintray api key
bintray.user=hbshen
bintray.apikey=c5434272d522d35d1a0123459981225564155753
第二個API Key就是你在上面記錄的API Key。
因為這個屬于個人隱私,一般不能傳到網(wǎng)上去,所以需要在記錄到matrixview下的local.properties中(利用gitignore忽略這個文件到git),然后腳本再從local.properties中讀取這兩個值。
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 {
repo = "maven"
name = "MatirxView" // project name in jcenter
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
local.properties如下:
ndk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk\\ndk-bundle
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk
bintray.user=hbshen
bintray.apikey=c5434272d522d35d1a0123459981225564155753
完整的build.gradle請參考這里。
8. 執(zhí)行
根據(jù)上面的build.gradle,會產(chǎn)生幾個特別的gradle任務(wù):
javadocJar
sourcesJar
install
bintrayUpload
通過gradew執(zhí)行這些任務(wù)即可。
gradew javadocJar
gradew sourcesJar
gradew install
gradew bintrayUpload
或者通過Android Studio的最右邊欄的Gradle窗口分別可視化操作,點擊執(zhí)行:
9. 請求審核
當(dāng)bintrayUpload成功之后,
- 在https://bintray.com/hbshen主頁“Latest Activity”塊,會看到你提交了一個項目
- 從這個Latest Activity列表中點擊你的項目
- 進(jìn)入詳情頁https://bintray.com/hbshen/maven/MatrixView
- 點擊“Add to JCenter ”,填寫提交信息,就提交審核了
只要上面沒問題,一般審核很快,這次審核通過了,后面升級就不用再審核了。
七、外部項目引用此庫
dependencies {
compile 'com.ayst.matrixview:matrixview:1.0'
}