我們經(jīng)常在gradle引用,卻不知道是什么原理,今天跟著代碼君,教你開源自己的項(xiàng)目供別人使用,像這樣
compile 'com.jzp:DemoJCenterLibrary:1.0.0'
參考文獻(xiàn):鴻洋大神的博客
鴻洋大神寫的博客比較簡潔干練,可能不適合初級入門者,所以在此進(jìn)行補(bǔ)充,把我踩過的坑,都寫出來,供大家借鑒參考
說明
- 上傳項(xiàng)目用的插件有兩種,gradle-bintray-plugin 和 bintray-release網(wǎng)上有很多資料是介紹gradle-bintray-plugin的,但是代碼君親測了一下,gradle-bintray-plugin步驟非常繁瑣,踩了很多坑,等把文章寫完,在末尾把踩過的坑填一下
- 本文主要介紹的是 bintray-release ,這個(gè)步驟比較簡單,和gradle-bintray-plugin比較,已經(jīng)可以算是傻瓜式上傳啦。
- 流程大致是這樣的,首先創(chuàng)建自己android studio 的項(xiàng)目,然后上傳到bintray倉庫,最后上傳同步到j(luò)Center倉庫,供別人使用自己的庫文件,好啦,下面開始劃重點(diǎn)啦!
一、新建一個(gè)項(xiàng)目,就是要開源的項(xiàng)目
新建工程(new project)UploadjcenterTest
新建自己要共享的庫(new model)mylibrary
在庫文件里新建一個(gè)mToast類,此類的作用是開源自己寫的toast提示框
-
如果有自己的庫要上傳,可以省略1,2,3步驟,用自己的庫Create library
二、注冊bintray.com賬號,搭建自己的倉庫
為什么要注冊這個(gè)賬號呢,因?yàn)閖center()屬于bintray旗下的一個(gè)倉庫。所以我們要上傳jCenter就必須要有bintray賬號
- 進(jìn)入https://bintray.com/,注冊賬號。
- 注冊完成后,需要郵箱激活;也可以選擇第三方登錄。
注冊成功后進(jìn)入準(zhǔn)備工作,這些后期在配置android studio中的gradle要用到
-
獲取API keyAPI key
記住自己的用戶名和密碼,后面上傳要用
創(chuàng)建一個(gè)組織(organization)和maven項(xiàng)目,這個(gè)是必須的,缺少此步驟,后面會報(bào)錯(cuò),先創(chuàng)建出來,后面我再解釋
-
創(chuàng)建organization--------jzplibrary (隨意命名
jzplibrary -
創(chuàng)建maven庫-----maven(必須叫maven而且是小寫)
find maven
create maven -
創(chuàng)建成功效果圖
success
三、Android studio gradle的配置,及上傳
- 在你的項(xiàng)目的build.gradle添加bintray-release的classpath,注意是項(xiàng)目的build.gradle,不是module的,即UploadJcenterTest/build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.novoda:bintray-release:0.3.4' //添加的
}
}
- 在Module:mylibrary(你要上傳的庫)的build.gradle中配置參數(shù)
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'//添加
android {
//保持不變
}
dependencies {
//保持不變
}
//添加
publish {
userOrg = 'jzplibrary'//bintray.com自己創(chuàng)建的組織名稱
groupId = 'com.jzp'//項(xiàng)目路徑
artifactId = 'toastlibrary'//項(xiàng)目名稱
publishVersion = '1.0.0'//版本號
desc = 'Oh hi, this is a nice description for a project, right?'//描述,不重要,隨便寫
website = 'https://github.com/jiangzepeng/MyModel'//隨便寫;當(dāng)然有g(shù)ithub地址最好了
}
- 按照上面填寫的,最后如果成功我們引入的方式為
compile 'groupId : artifactId : publishVersion' //引入原理
compile 'com.jzp:toastlibrary:1.0.0
-
以上步驟做完就可以開始上傳了,上傳的代碼,執(zhí)行在android studio的終端上terminal,
PbintrayUser--填寫bintray.com用戶名
PbintrayKey--填寫bintray.com中的API Key
Mac版
./gradlew clean build bintrayUpload -PbintrayUser=jzp -PbintrayKey=xxxxxxxxxxxxx -PdryRun=false
Window版
gradlew clean build bintrayUpload -PbintrayUser=jzp -PbintrayKey=xxxxxxxxxxxxx -PdryRun=false
- 上傳成功會提示BUILD SUCCESSFUL
四、bintray.com 加入到j(luò)center中
-
登錄到bintray.com,成功的效果圖
bintray.com -
把項(xiàng)目添加到j(luò)Center中,點(diǎn)擊add to jCenter
jCenter -
add to jcenter
add to jcenter 好了,到此基本算成功了,只需等待bintray的工作人員審核,我試過,大概半天差不多就有結(jié)果了,審核后會發(fā)送站內(nèi)消息和郵箱通知你
審核通過后,可以根據(jù)你上傳的groupId,訪問該網(wǎng)站https://jcenter.bintray.com/加上你的groupId;例如訪問https://jcenter.bintray.com/com/jzp/就可以找到自己的庫了
如果只想內(nèi)部引用的話在項(xiàng)目的gradle中配置自己倉庫的url,Maven的url為https://dl.bintray.com/jcenterupload/maven/,然后compile到自己項(xiàng)目中
compile 'com.jzp:toastlibrary:1.0.0'
- 引入成功的效果圖
番外篇
一、bintray-release方式踩過的坑
- HTTP/1.1 404 Not Found [message:Repo ‘maven’ was not found]
原因:注冊完bintray.com,未創(chuàng)建jcenteruploadTest組織和maven
解決方案:執(zhí)行上面的第二點(diǎn)的第三步驟
參考文獻(xiàn):http://blog.csdn.net/tmac2000/article/details/53261141
二、gradle-bintray-plugin方式踩過的坑
- Error:No service of type Factory available in ProjectScopeServices.
原因:classpath com.github.dcendents:android-maven-gradle-plugin:1.3版本太低
解決:更新到1.4.1以上就可以解決問題
參考文獻(xiàn):http://www.itdecent.cn/p/c4f4894ad215
- 執(zhí)行終端命令是,提示command not found,可能沒編譯進(jìn)來,重新rebuild一下就好
三、參考文獻(xiàn)
- 英文文獻(xiàn)(很多基本都是參考著兩篇)
- 中文文獻(xiàn)
- http://www.itdecent.cn/p/e01f2005893e(bintray-release手動(dòng)上傳)
- http://www.itdecent.cn/p/d75759435833(gradle-bintray-plugin方式上傳)