當(dāng)我們使用RxJava,retrofit時(shí),我們使用的姿勢是這樣的
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
使用很方便有木有,如果我們自己寫的一些可重復(fù)使用的組件或者框架也可以這樣使用豈不是很酷,下面將介紹如何將自己的庫發(fā)布到j(luò)center。
第一步:注冊jcenter賬號,這里有幾個(gè)坑,我當(dāng)時(shí)就踩了一個(gè),當(dāng)時(shí)用的是谷歌郵箱注冊的,第一次注冊了企業(yè)版,后面項(xiàng)目發(fā)布進(jìn)行不下去,主要企業(yè)版和個(gè)人版注冊頁面長的一模一樣,坑爹啊,請擦亮你的雙眼,認(rèn)準(zhǔn)個(gè)人版的注冊地址,重要的地址貼3遍
https://bintray.com/signup/oss
https://bintray.com/signup/oss
https://bintray.com/signup/oss
可以使用github郵箱快速登錄,但是qq郵箱,163郵箱是注冊不了的,我是使用谷歌郵箱注冊的,注冊好了你會看到下面這個(gè)界面

將鼠標(biāo)懸浮到右上角的圖標(biāo),你會看到一個(gè)Edit Profile,點(diǎn)進(jìn)去,你會看到右下方有一個(gè)Api key,點(diǎn)擊后再次輸入密碼就能看到Api key了,記住后面這個(gè)Api key用得著,到時(shí)能快速找到。
第二步、配置自己需要發(fā)布的工程,引入bintray-release
2.1在你的項(xiàng)目的build.gradle添加bintray-release的classpath,注意是項(xiàng)目的build.gradle,不是module,如下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.novoda:bintray-release:0.8.0'//引入
}
}
這樣使用到了github的一個(gè)插件,極大的簡話了發(fā)布流程,插件地址,https://github.com/novoda/bintray-release
2.2在你需要上傳到j(luò)center倉庫的module的build.gralde添加
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'//添加
android {
//保持不變
}
buildTypes {
//保持不變
}
}
//添加
publish {
userOrg = 'coollkkz'//bintray.com用戶名
groupId = 'com.cool'//jcenter上的路徑
artifactId = 'elasticdialog'//項(xiàng)目名稱
publishVersion = '1.0.0'//版本號
desc = 'One line of code turns on the elasticity dialog'//描述,不重要
website = 'hhttps://github.com/lkkz/ElasticDialog'//網(wǎng)站,不重要,如果有自己的網(wǎng)站就放上去吧
}
dependencies {
//保持不變
}
按上面這種格式寫,你將會發(fā)現(xiàn)你將來會以這種形式來使用
compile 'com.cool: elasticdialog:1.0.0'
哇!這樣的感覺真不錯
第三步,配置jcenter倉庫
3.1 創(chuàng)建Maven倉庫
點(diǎn)擊 Add New Repository,我這里已經(jīng)添加好了一個(gè)

點(diǎn)擊 Add New Repository進(jìn)入下面頁面

- Name填maven
- Type 選Maven
- Default Licenses (Optional)選Apache-2.0
最后點(diǎn)擊Create,這樣就創(chuàng)建好了一個(gè)Maven倉庫了
3.2 添加項(xiàng)目
點(diǎn)擊剛剛創(chuàng)建好的Maven倉庫,再點(diǎn)擊Add New Package 按鈕,填寫好Name和Description,這個(gè)Name和你在libary中配置的一致,如圖

第四步 執(zhí)行命令上傳代碼到j(luò)center倉庫
4.1 打開androidStudio的Terminal,執(zhí)行下面命令
./gradlew clean build bintrayUpload -PbintrayUser=coollkkz -PbintrayKey=xxxx -PdryRun=false
說明:
- PbintrayUser bintray.com用戶名
- PbintrayKey 之前看到的Api key
命令行顯示BUILD SUCCESSFUL,恭喜你,上傳成功了!?。?/li>
4.2打開jcenter后臺,找到Maven倉庫剛剛創(chuàng)建的elasticdialog項(xiàng)目,你會發(fā)現(xiàn)里面有東西了,接下來就是最好一步,點(diǎn)擊elasticdialog進(jìn)去,然后Add to JCenter 然后就是慢慢的等待過程了,等待審核通過,審核還是很快的。

后記:在發(fā)布過程中你可能會走好多彎路,遇到好多錯誤但是不要害怕,其實(shí)整個(gè)流程下來還是不難的。
你可能像我一樣遇到下面幾個(gè)錯誤:
Could not create version '1.0.0': HTTP/1.1 401 Unauthorized [message:This resource requires authenti
這個(gè)錯誤的原因是Api Key錯了/Users/cool/app/studyApp/ElasticDialog/elasticlibrary/src/main/java/com/cool/elasticlibrary/ElasticDialog.java:119: 警告: @param 沒有說明 * @param color
這個(gè)錯誤是你方法注釋上的@param 后面必須把注釋寫完整,如
/**
* 設(shè)置背景動畫時(shí)間
* @param duration 動畫時(shí)間
* @return this
*/
public ElasticDialog duration(long duration) {
if (duration < 0) {
this.mDuration = 1000;
}
this.mDuration = duration;
return this;
}