Android Studio釋出專案到Jcenter倉庫步驟
前言:Android+Studio/">Android Studio中把專案的lib庫提交到Jcenter倉庫中,需要使用到Bintray,Bintray是jCenter的提供商,他支援上傳lib到多個平臺,jCenter只是眾多平臺中的一個,形象的說jCenter是位於某地的倉庫,Bintray是送貨的卡車,你寫的庫就是貨了。
第一部分:在Bintray上註冊賬號,並建立package。
ofollow,noindex">註冊bintray ,注意:註冊時儘量使用國外的郵箱,避免接收不到驗證碼。例如我使用雅虎郵箱。
完成註冊之後,登入網站,然後點選maven。
點選Add New Package,為我們的library建立一個新的package。
假設你已經註冊賬你並按照上面步驟操作,或者使用我提供的賬號,登陸成功後會出現如下介面,點選maven進入該倉庫,並點選Add New Package建立新的包。
填寫package相關資訊,如下:
第二部分:操作AS專案,配置相關資訊,命令列操作lib包上傳。
Android Studio安裝上傳Bintray外掛和填寫相關資訊:(下面選用我測試通過並且操作路徑最短的方式)
在專案的根build檔案中補充如下標紅內容
這是根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 }
然後在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.infinitus_demo_lib'` `// 專案包名` `artifactId = ``'infinitus_demo_lib'` `// 專案名` `publishVersion = ``'1.0.2'` `// 釋出版本號` `desc = ``'Summarize the tools or methods commonly used in routine development'` `// 專案描述,可選項` `website = ``'[https://github.com/huangweicai/infinitus_demo_lib'](https://github.com/huangweicai/infinitus_demo_lib')` `// 專案站點,可選項` `}`
在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成功。
進入Bintray,可以找到我們上傳的包,在頁面的左下角看到maven地址說明上傳內容正確,第一次在頁面的右下角會看到add to jcenter,需要我們手動點選一下這個add to jcenter按鈕,然後等待lib包稽核通過後,我們就可以引用jcenter上的包了。
以上就是Android Studio打包上傳到Jcenter的完整流程。