AS發佈模塊到Jcenter倉庫

前言

Android Studio中把項目的lib庫提交到Jcenter倉庫中,需要使用到Bintray,Bintray是jCenter的提供商,他支持上傳lib到多個平臺,jCenter只是衆多平臺中的一個,形象的說jCenter是位於某地的倉庫,Bintray是送貨的卡車,你寫的庫就是貨了。

註冊準備

1.在Bintray上註冊賬號,並創建package。 註冊bintray,注意:註冊時儘量使用國外的郵箱,避免接收不到驗證碼。例如我使用雅虎郵箱。

2.完成註冊之後,登錄網站,然後點擊maven。 image.png

3.點擊Add New Package,爲我們的library創建一個新的package。 image.png

4.假設你已經註冊賬你並按照上面步驟操作,或者使用我提供的賬號,登陸成功後會出現如下界面,點擊maven進入該倉庫,並點擊Add New Package創建新的包。 image.pngimage.png

5.填寫package相關信息,如下: image.png

Android Studio配置部分

6.操作AS項目,配置相關信息,命令行操作lib包上傳。 Android Studio安裝上傳Bintray插件和填寫相關信息:(下面選用我測試通過並且操作路徑最短的方式) 在項目的根build文件中補充如下標紅內容 image.png這是根build源文件:

// 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文件中補充如下內容: image.png這是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在註冊成功後,可以在修改信息的界面找到,最好在第一次註冊成功後就記錄好) image.png等待執行,看到BUILD SUCCESSFUL說明上傳Bintray成功。

9.進入Bintray,可以找到我們上傳的包,在頁面的左下角看到maven地址說明上傳內容正確,第一次在頁面的右下角會看到add to jcenter,需要我們手動點擊一下這個add to jcenter按鈕,然後等待lib包審覈通過後,我們就可以引用jcenter上的包了。 image.png以上就是Android Studio打包上傳到Jcenter的完整流程。

最後測試

AS引入implementation 'com.xxx_demo_lib:xxx_demo_lib:1.0.2',代碼中調用演示工具類TestUtil.test(context);查看吐司是否提示,提示成功說明已經成功發佈並引入jcenter包。

發佈了57 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章