前言:Android Studio中把項目的lib庫提交到Jcenter倉庫中,需要使用到Bintray,Bintray是jCenter的提供商,他支持上傳lib到多個平臺,jCenter只是眾多平臺中的一個,形象的說jCenter是位于某地的倉庫,Bintray是送貨的卡車,你寫的庫就是貨了。
第一部分:在Bintray上注冊賬號,并創建package。
注冊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 generatePomFileForReleasePublicationgradlew publishReleasePublicationToMavenLocalgradlew 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的完整流程。
測試:AS引入implementation ‘com.infinitus_demo_lib:infinitus_demo_lib:1.0.2',代碼中調用演示工具類TestUtil.test(context);查看吐司是否提示,提示成功說明已經成功發布并引入jcenter包。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答