Android Studio中把項目的lib庫提交到Jcenter倉庫中,須要使用到Bintray,Bintray是jCenter的提供商,他支持上傳lib到多個平臺,jCenter只是衆多平臺中的一個,形象的說jCenter是位於某地的倉庫,Bintray是送貨的卡車,你寫的庫就是貨了。android
1.在Bintray上註冊帳號,並建立package。 註冊bintray,注意:註冊時儘可能使用國外的郵箱,避免接收不到驗證碼。例如我使用雅虎郵箱。git
2.完成註冊以後,登陸網站,而後點擊maven。 github
3.點擊Add New Package,爲咱們的library建立一個新的package。 web
4.假設你已經註冊帳你並按照上面步驟操做,或者使用我提供的帳號,登錄成功後會出現以下界面,點擊maven進入該倉庫,並點擊Add New Package建立新的包。 bash
6.操做AS項目,配置相關信息,命令行操做lib包上傳。 Android Studio安裝上傳Bintray插件和填寫相關信息:(下面選用我測試經過而且操做路徑最短的方式) 在項目的根build文件中補充以下標紅內容 這是根build源文件:maven
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.novoda:bintray-release:+' // 新增
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
tasks.withType(Javadoc) { // 新增
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}複製代碼
7.而後在lib的build文件中補充以下內容: 這是lib的源build文件:工具
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release' // 新增
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 2
versionName "1.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions { // 新增
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
publish { // 新增
userOrg = 'huangweicai' // 註冊bintray時的username
groupId = 'com.xxx_demo_lib' // 項目包名
artifactId = 'xxx_demo_lib' // 項目名
publishVersion = '1.0.2' // 發佈版本號
desc = 'Summarize the tools or methods commonly used in routine development' // 項目描述,可選項
website = 'https://github.com/huangweicai/xxx_demo_lib' // 項目站點,可選項
}複製代碼
8.在Android Studio的命令行窗口依次輸入以下命令:測試
gradlew generatePomFileForReleasePublication
gradlew publishReleasePublicationToMavenLocal
gradlew bintrayUpload -PbintrayUser=xxx -PbintrayKey=xxx -PdryRun=false
複製代碼
其中,PbintrayUser是Bintray的用戶名,PbintrayKey是Bintray的API Key。(API Key在註冊成功後,能夠在修改信息的界面找到,最好在第一次註冊成功後就記錄好) 等待執行,看到BUILD SUCCESSFUL說明上傳Bintray成功。gradle
9.進入Bintray,能夠找到咱們上傳的包,在頁面的左下角看到maven地址說明上傳內容正確,第一次在頁面的右下角會看到add to jcenter,須要咱們手動點擊一下這個add to jcenter按鈕,而後等待lib包審覈經過後,咱們就能夠引用jcenter上的包了。 以上就是Android Studio打包上傳到Jcenter的完整流程。
AS引入implementation 'com.xxx_demo_lib:xxx_demo_lib:1.0.2',代碼中調用演示工具類TestUtil.test(context);查看吐司是否提示,提示成功說明已經成功發佈並引入jcenter包。