本文由
玉剛說寫作平臺
提供寫作贊助原作者:
Mr.S
版權宣告:本文版權歸微信公眾號
玉剛說
所有,未經許可,不得以任何形式轉載
最熟悉的陌生人
想必大家都用過這樣的或者那樣的依賴吧。
compile 'com.android.support:support-v4:24.2.1'
compile 'com.google.code.gson:gson:2.7'
compile 'com.umeng.analytics:analytics:6.1.4'
複製程式碼
有谷歌自己家的,還有第三方的比如友盟等等,我們在用的時候會感到極度的方便。不用再像以前eclipse時期需要把library下載下來再進行處理了。只需要一行程式碼,就可以得到我們想要的。
有時候感覺這種依賴離我們自己很遠,感覺這都是大神們的專屬,感覺這些很是高階,所以會感覺很陌生。其實說白了,就和你上傳到github一個效果,就是一個倉庫,我們把我們的Library放上去就可以了。本質上來說是超級簡單的。那我就手把手教你釋出自己的開源庫到Jcenter,讓別人也用用我們們的依賴庫,面試的時候也可以吹吹牛X。忽悠忽悠面試官。簡單拿高薪,贏取白富美。(醒醒,醒醒,兄弟)
Maven和Jcenter
很多人都會有這幾個疑惑:
- 什麼是Maven
- 什麼是Jcenter
- 上面這哥倆啥關係
什麼是Maven
Maven 是專門用於構建和管理Java相關專案的工具。這裡是官方介紹。Java世界中主要有三大構建工具:Ant、Maven和Gradle。經過這幾年的發展,Ant差不多已經銷聲匿跡了、Maven也差不多也快黃了,我們們的Gradle現在可是如日中天。
什麼是Jcenter
是由JFrog公司提供的Bintray中的Java倉庫。它是當前世界上最大的Java和Android開源軟體構件倉庫。 所有內容都通過內容分發網路(CDN)使用加密https連線獲取。JCenter是Goovy Grape內的預設倉庫,Gradle內建支援(jcenter()倉庫),非常易於在其他構建工具內進行配置。JCenter相比MavenCenter構件更多,效能也更好。
Maven和Jcenter啥關係

Jcenter 上傳步驟
主要分為如下幾個步驟。
建立你的 Library 工程
首先我們要建立一個Library工程。然後給一個高大上的名字。比如FireworkView:


我這裡這個Library的功能是給EditText 新增煙花效果(很簡單的一個功能)。
註冊 Bintray 賬號
前提:科學上網 咳咳咳!你懂得!
我們需要註冊一個 Bintray賬號。
官網(企業):https://bintray.com/ 個人:https://bintray.com/signup/oss
這裡我們要說明一下,一定要點選個人這個連結,官網的註冊連結只能註冊企業使用者,坑坑坑~~~

註冊成功之後,我們要建立一個Maven倉庫。


好到此為止,我們需要的必要材料都準備齊了。
配置 build.gradle
1、builde.grade(專案根目錄下)
buildscript {
repositories {
jcenter()
}
// dependencies表明專案依賴對應版本的Gradle構建工具
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
//加上這兩行
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
}
allprojects {
repositories {
jcenter()
}
}
複製程式碼
至於最新版本號,大家可以去官網自行更改:
- Maven:github.com/dcendents/a…
- Jfrog Bintray:github.com/bintray/gra…
2、builde.grade(Library目錄下)
// apply plugin表明應用的外掛的型別,
apply plugin: 'com.android.library'
//新增這兩行
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
android {
compileSdkVersion 26
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
//專案主頁
def siteUrl = 'https://github.com/shishaoyan/FireworksView'
//專案的git地址
def gitUrl = 'https://github.com/shishaoyan/FireworksView.git'
//釋出到JCenter上的專案名字
def libName = "FireworksView"
//釋出到組織名稱名字,必須填寫
//(com.ssy.fireworkstextview)這樣寫是不好的,專案名會拼上去
//我手欠然後最後就是這樣了,大家引以為戒
//compile 'com.ssy.fireworkstextview:fireworkstextview:1.0.0'
group = "com.ssy"
// 版本號,下次更新是隻需要更改版本號即可
version = "1.0.0"
//上面配置後上傳至JCenter後的編譯路徑是這樣的: compile 'me.songning.CircleView:library:1.0.0'
//生成原始檔
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
//生成Javadoc文件
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
//文件打包成jar
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
//拷貝javadoc檔案
task copyDoc(type: Copy) {
from "${buildDir}/docs/"
into "docs"
}
//上傳到JCenter所需要的原始碼檔案
artifacts {
archives javadocJar
archives sourcesJar
}
// 配置maven庫,生成POM.xml檔案
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
//專案描述,隨意填
name 'Input text with fireworks.'
url siteUrl
licenses {
license {
//開源協議
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
//開發者的個人資訊
id 'baidianxiao'
name 'ShiShaoYan'
email 'baidianxiao@gmail.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
//上傳到JCenter
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user") //讀取 local.properties 檔案裡面的 bintray.user
key = properties.getProperty("bintray.apikey") //讀取 local.properties 檔案裡面的 bintray.apikey
configurations = ['archives']
pkg {
//注意:這裡的repo值必須要和你建立Maven倉庫的時候的名字一樣
repo = "ssy"
//釋出到JCenter上的專案名字
name = libName
//專案描述
desc = 'Input text with fireworks'
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options{
//如果你的專案裡面有中文註釋的話,必須將格式設定為UTF-8,不然會出現亂碼
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
複製程式碼
3、local.properties
將user和key寫到這個檔案之中。
ndk.dir=/Users/shishaoyan/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/shishaoyan/Library/Android/sdk
##下面這兩個
bintray.apikey=93200ffcf836**********c8e78425e9cf9967a
bintray.user=baidianxiao
複製程式碼
通過如下步驟可以在bintray上面獲取user和apikey:

然後點選Edit


執行上傳命令
1、開啟Android Studio底部工具欄的Terminal,輸入命令:
windows:
gradlew install
mac:
./gradlew install

2、上傳到Bintray。
windows:
gradlew bintrayUpload
mac:
./gradlew bintrayUpload


OK,完美,成功了嗎?

再點進去

compile 'com.ssy:fireworkstextview:1.0.0'
複製程式碼
我們迫不及待的要加入我們的專案引用一下了,且慢,你還沒有釋出呢?看右邊的Add to JCenter按鈕,釋出成功後才可以讓大家來使用,直接點選Send傳送就可以了。


//這兩個都可以使用,但是存在命名規範的問題
//字首 com.ssy.fireworkstextview 多餘的失誤
compile 'com.ssy.fireworkstextview:fireworkstextview:1.0.0'
//Library命名單詞錯誤的失誤
compile 'com.ssy:fireworkstextview:1.0.0'
複製程式碼
不知道大家有沒有發現了我的一個錯誤:fireworkstextview這個單詞是我建立專案的時候打錯的,本來應該是FireworksView。

//釋出到JCenter上的專案名字
def libName = "FireworksView"
複製程式碼

終於把步驟全部走完了。我們感覺貌似有點麻煩啊。能不能更簡單點呢?答案是肯定的。
更簡單的方案
1、新建一個倉庫

2、我們可以看到Import from GitHub (前提是你已經關聯Github賬號,按步驟來就可以了很簡單)直接可以引用Public的專案,私有的是需要Bintray賬號付費升級的。

3、我們選擇我們的專案(我這裡隨便選了一個)


總結
兩種方法,一個複雜,一個簡單,其實我是推薦大家使用Github這種方式,一來版本好控制,二來方便快捷。
其實就是這麼簡單,沒有我們想的那麼難。當然,大家肯定會遇到一些莫名其妙的坑,有坑的同學可以看看這篇文章Android 釋出專案到 JCenter 遇到的各種坑。我遇到了java 10 .0.1 區分不出來版本號的問題,無奈只能JDK降級,換了8的,解決了問題。

-
本文配套原始碼: https://github.com/shishaoyan/FireworksView
-
VirtualAPK的Jcenter上傳指令碼: https://github.com/didi/VirtualAPK/blob/master/CoreLibrary/upload.gradle
參考文章
一步一步教你在JCenter釋出開源庫: https://www.jianshu.com/p/275fc9d54e4a
