1、 注冊
在 Bintray上注冊一個(gè)私人賬戶。有兩種注冊方式,選擇第二種该酗,F(xiàn)or an open Source Account。
2 獲取api key
點(diǎn)擊 api key频轿,輸入登錄密碼垂涯,獲取api key:
3創(chuàng)建maven倉庫
maven 倉庫配置好后,可以開始配置build.gradle
4 根目錄build.gradle
首先配置根目錄的build.gradle,在dependencies中添加jcenter插件
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
5 配置module 的build.gradle
配置完根目錄的build后航邢,然后配置需要上傳jcenter 的module的build
- 根節(jié)點(diǎn)配置plugin
//上傳jcenter
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
- 根節(jié)點(diǎn)配置version
version = 0.0.1
- 定義相關(guān)網(wǎng)址:
def siteUrl = 'https://github.com/johnnycmj/appfactory' // project homepage
def gitUrl = 'https://github.com/johnnycmj/appfactory.git' // project git
def issueUrl = 'https://github.com/johnnycmj/appfactory/issues'
- 定義group
一般在gradle中引用庫是這樣的耕赘。compile 'io.reactivex:rxjava:1.2.9',其中這個(gè)aar用:分3個(gè)部分膳殷,第一個(gè)部分是group操骡,第二個(gè)部分是 name九火,第三就是版本。
group = com.smart.app
上傳到j(luò)center至少需要四個(gè)文件册招,除了打包的aar之外岔激,還需要pom和javadoc,source是掰,否則是通不過jcenter的審核虑鼎。這些我們都可以用腳本生成。
- 打包javadocjar和sourcejar
//打包javadocjar和sourcejar
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
//增加編碼键痛,這行很重要炫彩,如果是windows系統(tǒng),同時(shí)如果在代碼中有中文的話絮短,不加這行江兢,上傳會(huì)報(bào)錯(cuò)
options.encoding="utf-8"
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
}
- 定義pom并打包aar
//定義pom并打包aar
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'Appfactory For Android'
url siteUrl
// Set your license
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer { //填寫的一些基本信息
id 'johnnycmj'
name 'johnny.chan'
email 'chmj1208@163.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
- 上傳到Jcenter倉庫
上傳到j(luò)center的網(wǎng)站Bintray,需要用戶驗(yàn)證:
一般這種屬于私有參數(shù)丁频,我們配置在local.properties,在git上傳時(shí)用git.ignore忽略上傳杉允。
bintray.user=用戶名
bintray.apikey=之前獲取的apikey
//配置bintray參數(shù)
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" //跟上面創(chuàng)建的Maven倉庫名字保持一致
name = POM_ARTIFACT_ID //發(fā)布到JCenter上的項(xiàng)目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
完全配置
//上傳jcenter
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
android {
lintOptions {
abortOnError false
}
}
buildscript {
repositories {
jcenter()
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25+'
compile'com.android.support:support-v4:25+'
compile'com.android.support:support-annotations:25+'
compile'com.android.support:design:25+'
compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4'
}
version = "0.0.1"
def siteUrl = 'https://github.com/johnnycmj/appfactory' // project homepage
def gitUrl = 'https://github.com/johnnycmj/appfactory.git' // project git
def issueUrl = 'https://github.com/johnnycmj/appfactory/issues'
group = "com.smart.app"
//打包javadocjar和sourcejar
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
//增加編碼,這行很重要席里,如果是windows系統(tǒng)叔磷,同時(shí)如果在代碼中有中文的話,不加這行奖磁,上傳會(huì)報(bào)錯(cuò)
options.encoding="utf-8"
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
}
tasks.getByPath(":appfactory:javadoc").enabled = false
artifacts {
archives javadocJar
archives sourcesJar
}
//定義pom并打包aar
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'Appfactory For Android'
url siteUrl
// Set your license
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer { //填寫的一些基本信息
id 'johnnycmj'
name 'johnny.chan'
email 'chmj1208@163.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
//上傳到Jcenter倉庫
//配置bintray參數(shù)
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" //跟上面創(chuàng)建的Maven倉庫名字保持一致
name = appfactory //發(fā)布到JCenter上的項(xiàng)目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
配置都配完世澜,準(zhǔn)備上傳bintrary,在androidstudio 中的 terminal 命令窗中輸入:
gradle install
gradle bintrayUpload
如果順利的話就會(huì)上傳成功署穗,不過這邊會(huì)有很多坑。
坑一
生成javadoc會(huì)有一大堆錯(cuò)誤嵌洼,有些是因?yàn)槲覀儗懽⑨尩臅r(shí)候沒有按javadoc的標(biāo)準(zhǔn)來寫案疲,所有生成的時(shí)候會(huì)報(bào)錯(cuò)。
Lint 檢查默認(rèn)是開啟的麻养,Lint 會(huì)檢查項(xiàng)目中的語法錯(cuò)誤褐啡,如果沒有通過則無法繼續(xù)。只需要在 Module 的 build.gradle 添加如下代碼:
android {
lintOptions {
abortOnError false
}
}
然后在去掉javadoc的task
tasks.getByPath(":appfactory:javadoc").enabled = false
其中":appfactory:javadoc" appfactory是你要上傳的module名稱鳖昌。這兩步之后即可解決上面問題备畦。
坑二
出現(xiàn) HTTP/1.1 401 Unauthorized
這個(gè)是用戶名和apikey錯(cuò)誤,導(dǎo)致無權(quán)限许昨,檢查user和 apikey即可懂盐。
坑三
出現(xiàn) HTTP/1.1 404 Not Found
檢查repo的名稱是否跟bintrary上創(chuàng)建的一樣
坑四
無法引用,上傳jcenter成功無法引用糕档,由于莉恼,上傳到j(luò)center會(huì)有一段審核時(shí)間,在審核通過后可以用一般的引用,還未審核的時(shí)候會(huì)報(bào)錯(cuò)誤:
解決:
在根build中加上自己的jcenter地址:
allprojects {
repositories {
jcenter()
maven{
url "https://dl.bintray.com/johnnychmj/maven"
}
}
}
然后編譯即可俐银。