IDE:
Android Studio 2.0
dependencies:
com.android.tools.build:gradle:2.0.0
com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0
com.github.dcendents:android-maven-gradle-plugin:1.3
一赚瘦、申請Bintray賬號
先到 Bintray 注冊一個賬號弃衍,也可以使用 GitHub 等賬號登錄工窍。
賬號登錄后,點擊賬號名稱進入個人頁面
Profile.png
然后點擊Edit碌冶,進入編輯頁面
Profile Edit.png
接著點擊 API key
Edit API Key.png
獲取到 API KEY,記錄下來
API Key.png
二、配置Bintray的賬號信息
在項目根目錄的 local.properties 文件中添加 Bintray 的賬號信息:
bintray.user=用戶名
bintray.apikey=第一步獲取到的API key
三椒拗、聲明插件依賴
在項目的 build.gradle
中聲明 maven 和 bintray 的插件依賴:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
// maven、bintray
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
}
}
四获黔、配置Module
配置要發(fā)布的 Module 的 build.gradle 文件蚀苛,最終的文件:
apply plugin: 'com.android.library'
// 配置插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
// 設置項目的信息,需要根據需要自行修改下面的信息
ext {
bintrayRepo = 'maven' // bintray的倉庫名
bintrayName = 'projectName' // bintray上的項目名
libraryGroupId = 'GroupId' // JCenter上的GroupId
libraryVersion = '1.0.0' // 版本號
libraryPackaging = 'aar' // 上傳包的類型
libraryDescription = 'something' // 項目描述
siteUrl = 'projectUrl' // 項目的主頁
gitUrl = 'gitUrl' // Git倉庫的地址
issuesUrl = 'issuesUrl' // issues的URL地址
// 配置開發(fā)者的信息
developerId = 'Id' // ID
developerName = 'Name' // 開發(fā)者名稱
developerEmail = 'Email' // 開發(fā)者郵箱
// 設置開源證書的信息
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
group = libraryGroupId // 指定項目的GroupId
version = libraryVersion // 指定項目的版本號
install {
repositories.mavenInstaller {
// 生成pom.xml的配置信息
pom.project {
packaging libraryPackaging // 打包的類型
name bintrayName // 項目的名稱
description libraryDescription // 項目的描述
url siteUrl // 項目的主頁
// 配置開源證書信息
licenses {
license {
name licenseName
url licenseUrl
}
}
// 配置開發(fā)者信息
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
// 軟件配置管理信息
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
// 上傳源碼的task
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
// 上傳Javadoc
android.libraryVariants.all { variant ->
println variant.javaCompile.classpath.files
if (variant.name == 'release') { // 我們只需 release 的 javadoc
task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
source = variant.javaCompile.source
classpath = files(variant.javaCompile.classpath.files,
project.android.getBootClasspath())
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
title bintrayName
links "http://docs.oracle.com/javase/7/docs/api/"
linksOffline "http://d.android.com/reference",
"${android.sdkDirectory}/docs/reference"
}
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
task("javadoc${variant.name.capitalize()}Jar", type: Jar,
dependsOn: "generate${variant.name.capitalize()}Javadoc") {
classifier = 'javadoc'
from tasks.getByName("generate${variant.name.capitalize()}Javadoc")
.destinationDir
}
artifacts {
archives tasks.getByName("javadoc${variant.name.capitalize()}Jar")
}
}
}
// 讀取配置的bintray用戶的信息
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties')
.newDataInputStream())
// 配置bintray的信息
bintray {
// 讀取 local.properties 的 bintray.user
user = properties.getProperty("bintray.user")
// 讀取 local.properties 的 bintray.apikey
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = bintrayRepo
name = bintrayName
desc = libraryDescription
websiteUrl = siteUrl
vcsUrl = gitUrl
issueTrackerUrl = issuesUrl
licenses = allLicenses
labels = ['android'] // 設置標簽
publish = true
publicDownloadNumbers = true
}
}
配置完成后調用 Gradle 命令上傳即可:
gradle bintrayUpload
五玷氏、同步到JCenter
打開個人頁面堵未,找到剛剛上傳的項目
Profile Maven Repository.png
點擊后打開剛剛上傳的項目,點擊右下角的 “Add to JCenter”盏触,申請后等待審核通過
Add to JCenter.png
注:
- 上傳到 Bintray 后使用 Module 的名稱作為項目的 ArtifactId渗蟹,即使在
build.gradle
中設置了 ArtifactId 也無效。