通過Gradle打包發(fā)布函數(shù)庫到JCenter

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容