AndroidStudio將遠(yuǎn)程倉庫默認(rèn)指定為Jcenter倉庫,位于Bintray網(wǎng)站。Bintray網(wǎng)站下面還有其他好幾個倉庫。本文主要介紹如何將Android項目發(fā)布到Jcenter。
一 、注冊Bintray賬號
Bintray官網(wǎng):https://bintray.com

也可使用GitHub賬號登錄
二 、獲取APIKey
上傳項目之前我們需要兩樣?xùn)|西,一個就是用戶名,另一個是APIKey。APIKey可在這里查看。

三、插件依賴
在項目最外層的build.gradle添加“gradle-bintray-plugin”以及“android-maven-plugin”插件的依賴。
四、Module配置
在需要發(fā)布到Jcenter的module(比如library)的build.gradle里配置以下內(nèi)容:
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
version = "1.0.0" // 版本號
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
resourcePrefix "bankcardformat" // 隨意命名
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
def siteUrl = 'https://github.com/smuyyh/BankCardFormat'? ? // Git項目主頁
def gitUrl = 'https://github.com/smuyyh/BankCardFormat.git' // Git倉庫url
group = "com.yuyh.bankcardformat" // 一般為包名
install {
repositories.mavenInstaller {
// 生成POM.xml
pom {
project {
packaging 'aar'
name 'Android BankCardFormat' // 項目描述
url siteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer { // 開發(fā)者個人信息
id 'smuyyh'
name 'smuyyh'
email 'smuyyh@gmail.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
options.encoding = "UTF-8" // 設(shè)置編碼,否則中文可能會提示出錯
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = "maven" // 發(fā)布到Maven庫
name = "BankCardFormat" // 發(fā)布到JCenter上的項目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
五、配置Username和APIKey
在local.properties文件配置Username和APIKey。
bintray.user= xxxx
bintray.apikey= xxxx
正常情況下local.properties應(yīng)該加入到.gitignore文件里,因為這兩項屬于隱私信息,無需上傳到GitHub。當(dāng)然了,也可把bintray.user及bintray.apiKey配置在Gradle用戶目錄下的gradle.properties(不存在則新建),例如Windows是在C:/user/username/.gradle,OSX和Linux在~/.gradle
六、ReBuild
Rebuild一下項目,會發(fā)現(xiàn)在/build/outputs/aar下生成兩個文件,這就是library打包出來的二進(jìn)制文件。
七、上傳項目到Jcenter
在Android Studio的Terminal執(zhí)行以下命令:
gradlew javadocJar
gradlew sourcesJar
gradlew install
gradlew bintrayUpload
執(zhí)行成功之后,在Bintray個人主頁下面可以看到Maven多了一個Package。

八、申請項目加入Jcenter
九、等待審核
審核速度挺快,一般幾個小時左右會通過。Bintray會向您發(fā)送一條成功的信息。那么就完成上傳了。
項目依賴
dependencies{? ? compile'com.yuyh.bankcardformat:library:1.0.0'}
項目更新
上傳的庫進(jìn)行升級的時候,須更改build.gradle下的version、versionCode、versionName,否則無法進(jìn)行打包上傳。更改完之后重新執(zhí)行上傳項目到Jcenter步驟。上傳完成可在項目主頁下看到更新的版本號。
———-
若在上傳的時候,出現(xiàn)GBK編碼錯誤,嘗試在task javadoc(type: Javadoc) 下設(shè)置UTF-8編碼:
options.encoding = “UTF-8”
無法解決請參考以下這個Gradle文件的配置:
https://github.com/msdx/gradle-publish/blob/master/bintray.gradle