前言
將自己寫的庫上傳到Jcenter或者M(jìn)aven提供給自己或者別人使用呻率,在構(gòu)建項(xiàng)目的時候只要寫上一行如下類似的引用代碼即可引用自己的庫
compile 'com.sus.library:imagelib:1.0.0'
看到這篇文章的同學(xué)可能之前已經(jīng)踩了不少坑泊交,希望下面的介紹可以幫你解惑
如果有什么問題歡迎提出!趁熱乎哈聪舒!
下面將逐步介紹如何將Library發(fā)布到JCenter
具體案例為:ImageLoaderUtil
所有下文涉及到的文件和配置都包含在其中,如果你覺得對你有用虐急,麻煩STAR一下
1箱残、進(jìn)入Bintray官網(wǎng)
- 這里選擇“For an Open Source Account Sign Up Here”,而非“START YOUR FREE TRIAL”,如果選擇 “START YOUR FREE TRIAL”戏仓,可能會碰到下面的問題 Bintray link to jcenter missing
- 問題結(jié)論就是:"Add To JCenter" is not enabled for Enterprise Trial users. You need to be OSS or Premium organization/user in order to link your packages to JCenter.
2疚宇、注冊賬號
填寫相關(guān)信息,郵箱盡量使用Gmail郵箱地址(國內(nèi)郵箱有可能無法注冊或者注冊成功無法收到激活郵件)赏殃,注冊完成之后到你填寫的郵箱里面去激活Bintray賬號即可
舉例:
- First Name : Shuai
- Last Name : Su
- Username : su2008shuai
- Password : xxxxxxxxxxxxxx
- Emai Address : su2008shuai@gmail.com
- Select Country : China
3敷待、創(chuàng)建代碼倉庫
點(diǎn)擊上圖所示的“Add NewRepository”按鈕,添加代碼倉庫仁热,點(diǎn)擊后就會跳轉(zhuǎn)到下圖的界面
舉例:
- Name : me
- Type : Maven
4榜揖、獲取頭像對應(yīng)的API KEY
點(diǎn)擊右上角個人頭像進(jìn)入到個人信息主界面,點(diǎn)擊Edit按鈕即可進(jìn)入到下圖所示的界面抗蠢。點(diǎn)擊“API KEY”举哟,輸入Bintray本賬號的登陸密碼,即可查看到本賬號的API KEY
5迅矛、Add New Package
舉例:
- Name : imagelib
- License : Apache-2.0
- Version control : https://github.com/soulrelay/ImageLoaderUtil
6妨猩、Project的build.gradle添加如下信息
在Project的build.gradle添加如下信息:
//用于打包Maven所需文件
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
//用于上傳Maven生成的文件到Bintray
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
我的Project的build.gradle的完整信息:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
//用于打包Maven所需文件
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
//用于上傳Maven生成的文件到Bintray
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
name 'glide-snapshot'
url 'http://oss.sonatype.org/content/repositories/snapshots'
}
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
7、配置并應(yīng)用bintrayUpload.gradle秽褒,配置bintray.properties和project.properties
7.1 在imagelib Module的根目錄下創(chuàng)建bintrayUpload.gradle文件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//加載屬性文件
Properties properties = new Properties()
File localPropertiesFile = project.file("bintray.properties");
if (localPropertiesFile.exists()) {
properties.load(localPropertiesFile.newDataInputStream())
}
File projectPropertiesFile = project.file("project.properties");
if (projectPropertiesFile.exists()) {
properties.load(projectPropertiesFile.newDataInputStream())
}
//讀取屬性
def projectRepositoryName = properties.getProperty("project.repositoryName")
def projectName = properties.getProperty("project.name")
def projectGroupId = properties.getProperty("project.groupId")
def projectArtifactId = properties.getProperty("project.artifactId")
def projectVersionName = android.defaultConfig.versionName
def projectPackaging = properties.getProperty("project.packaging")
def projectSiteUrl = properties.getProperty("project.siteUrl")
def projectGitUrl = properties.getProperty("project.gitUrl")
def projectVersionDesc = properties.getProperty("project.versiondesc")
def projectVersionVcsTag = properties.getProperty("project.versionvcstag")
def developerId = properties.getProperty("developer.id")
def developerName = properties.getProperty("developer.name")
def developerEmail = properties.getProperty("developer.email")
def bintrayUser = properties.getProperty("bintray.user")
def bintrayApikey = properties.getProperty("bintray.apiKey")
def bintrayOrganizationId = properties.getProperty("bintray.organizationId");
def javadocName = properties.getProperty("javadoc.name")
group = projectGroupId
// 配置生成POM.xml文件的參數(shù)
install {
repositories.mavenInstaller {
pom {
project {
name projectName
groupId projectGroupId
artifactId projectArtifactId
version projectVersionName
packaging projectPackaging
url projectSiteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection projectGitUrl
developerConnection projectGitUrl
url projectSiteUrl
}
}
}
}
}
//生成sources.jar
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
//生成javadoc.jar
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
//javadoc的配置
javadoc {
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version projectVersionName
links "http://docs.oracle.com/javase/7/docs/api"
title javadocName
}
}
bintray {
user = bintrayUser
key = bintrayApikey
configurations = ['archives']
pkg {
//userOrg = bintrayOrganizationId
repo = projectRepositoryName
name = projectName
websiteUrl = projectSiteUrl
vcsUrl = projectGitUrl
licenses = ["Apache-2.0"]
publish = true
version {
name = projectVersionName
desc = projectVersionDesc
vcsTag = projectVersionVcsTag
}
}
}
7.2 在imagelib Module的build.gradle中應(yīng)用上面創(chuàng)建的bintrayUpload.gradle文件壶硅,添加如下代碼
apply from: "bintrayUpload.gradle"
這里注意會遇到一個奇葩問題:
- Where:
Script '/Users/sus/share/ImageLoaderUtil/imagelib/bintrayUpload.gradle' line: 85
*What went wrong:
A problem occurred evaluating script.
android.compileSdkVersion is missing!
其實(shí)就是「 android.compileSdkVersion is missing!」 這個問題很奇葩,需要把
apply from: "bintrayUpload.gradle"這句話放在最下面销斟,如下完整文件信息所示庐椒,我碰到這個問題的時候是直接把這句話放在apply plugin: 'com.android.library'的后面了
完整文件信息:
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion '25.0.0'
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.squareup.okhttp3:okhttp:3.4.2'
compile 'com.github.bumptech.glide:glide:3.8.0-SNAPSHOT'
compile 'com.github.bumptech.glide:okhttp-integration:1.5.0-SNAPSHOT'
}
apply from: "bintrayUpload.gradle"
7.3 在imagelib Module的根目錄下創(chuàng)建7.1要讀取的配置文件
創(chuàng)建bintray.properties用于配置bintray和開發(fā)者信息
#配置bintray賬號相關(guān)信息
#bintray用戶名,不是登陸郵箱,是個人中心右上角顯示的名字
bintray.user=su2008shuai
#bintray的ApiKey
bintray.apiKey=xxxxxxxxxxxxx
#bintray的Organization Id
#bintray.organizationId=soulrelay
#配置開發(fā)者信息
#昵稱
developer.id=sushuai
#姓名
developer.name=sushuai
#郵箱
developer.email=su2008shuai@gmail.com
創(chuàng)建project.properties用于配置項(xiàng)目信息
#project
#倉庫名稱,就是在bintray官網(wǎng)建立的倉庫的名稱
project.repositoryName=me
#項(xiàng)目名稱
project.name=imagelib
#項(xiàng)目組id
project.groupId=com.sus.library
#項(xiàng)目id,一般同project.name
project.artifactId=imagelib
#打包類型
project.packaging=aar
#項(xiàng)目官方網(wǎng)站地址
project.siteUrl=https://github.com/soulrelay/ImageLoaderUtil
#項(xiàng)目git地址
project.gitUrl=https://github.com/soulrelay/ImageLoaderUtil
#生成的javadoc名稱
javadoc.name=imagelib
project.versiondesc = 1.0.0 normal
project.versionvcstag = 1.0.0 tag
7.4 在Terminal窗口下輸入如下指令上傳到Bintray
gradlew install
gradlew bintrayUpload
期間可能會碰到如下問題
- -bash: gradlew: command not found
解決方案:
gradlew is not in your global path. To execute the 'clean' task (or any task for that matter) using the gradle wrapper (gradlew) in your project directory in your terminal, specify the current directory with the './':
- ./gradlew clean
Running mac, you also have to do "chmod 755 gradlew" on the file before to make it executable.
歸結(jié)起來的話:
- chmod 755 gradlew
- ./gradlew install
- ./gradlew bintrayUpload
若出現(xiàn)BUILD SUCCESSFUL則說明成功上傳到了Bintray(有時候在執(zhí)行./gradlew bintrayUpload的時候報錯蚂踊,但這時去bintray官網(wǎng)查看它也上傳成功了约谈,可能是緩存的問題,可以Invalidate Caches or clean restart一下試試)
8、添加imagelib Package到JCenter
進(jìn)入到Bintray網(wǎng)站棱诱,找到剛才上傳的項(xiàng)目泼橘,點(diǎn)擊右下角的“Add To JCenter”按鈕
然后填寫項(xiàng)目描述點(diǎn)擊“Send”提交審核即可(這里可以什么都不干,直接點(diǎn)擊Send按鈕)军俊,如果審核成功侥加,它會給你發(fā)送一封站內(nèi)信(同時你注冊的郵箱優(yōu)也會收到通知,1天之內(nèi)肯定可以收到通知)通知你粪躬。
訪問這個鏈接:https://jcenter.bintray.com/com/sus/library/imagelib/1.0.0/
9、其它問題和說明
- Could not upload to 'https://api.bintray.com/content/su2008shuai/me/ImageLoaderUtil/1.0/ImageLoaderUtil/imagelib/1.0/imagelib-1.0-javadoc.jar': HTTP/1.1 400 Bad Request [message:Failed to resolve package name]
- Error:Could not find ImageLoaderUtil.jar (com.sus.library:ImageLoaderUtil:1.0.1).
Searched in the following locations:
https://jcenter.bintray.com/com/sus/library/ImageLoaderUtil/1.0.1/ImageLoaderUtil-1.0.1.jar
類似上面的問題的原因都是配置不對昔穴,bintray.properties和project.properties上的配置一定要和bintray線上的配置一致镰官,否則會報各種找不到xx的問題
- 我的CSDN博客地址:http://blog.csdn.net/s003603u
- 我的GitHub地址:https://github.com/soulrelay
- 我的簡書地址:http://www.reibang.com/u/514ca03bbc17
- 我的掘金地址:https://juejin.im/user/56f3d9d1816dfa00522b8f20
- 我的個人站點(diǎn): http://sushuai.tech/