前言:
在經歷了從eclipse到Android studio過渡之後,引入第三方庫的操作也變得相對簡單起來,再也不是自己去下載相應的jar,而只需要在build.gradle一句簡單的配置信息即可。如下:
compile 'com.voctex.banner:banner:1.0.7'
其實Android studio在配置了這句信息之後,在build的時候會去jCenter服務器去下載相應的庫到本地引用,而不需要我們自己去下載包再引用,這麼簡單的一句代碼就幫我們做了很多事了。
我們接下來就是要把我們自己的library庫上傳到jCenter服務器上,然後讓別人也可以方便引用。
準備工作:
首先我們要先對gradle有一定的瞭解,還有Android studio這個工具有一定的熟悉,這樣操作起來纔不會老是覺得問題一大堆,而是簡簡單單的直接成功,基本功還是需要的。以下我會以我的一個項目作爲栗子。
首先新建個app項目,默認有個app模塊,再新建個module,這個module作爲library的模塊,如下圖
然後需要註冊一個可以國際通用的郵箱,在我測試之後,QQ郵箱(@qq)和網易郵箱(@163)都不行,其他沒測過,有人說要註冊谷歌郵箱(@gmail)就行了,可谷歌在天朝都被牆了,每次用都要翻牆,也不是最好的選擇,然後試了一下阿里郵箱(@aliyun),發現可以。接下來就要用到這個郵箱了。
郵箱註冊完畢之後就要註冊一個Bintray賬號了,在這裏會有一個坑,特別注意,直接搜Bintray進入官網,然後註冊的時候會是註冊組織的賬號,而且還是試用一個月的期限,這裏要通過以下鏈接去註冊一個個人賬號:
https://bintray.com/signup/oss
創建倉庫
進入了Bintray之後,因爲是個人賬號,這時候你可以自己建立一個組織,或者直接忽略這操作,因爲你是個人開發者,我就直接忽略了。
————————————圖片分割線——————————
在上面一系列操作之後,就會得到一個空的倉庫,有了倉庫就可以上傳我們的項目上去了,當然,這是下一步要操作的內容。
配置gradle
需要配置三個文件,分別是項目根目錄下的build.gradle文件,library模塊根目錄下的build.gradle文件,以及項目根目錄下的local.properties。
1、項目根目錄下的build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
//加入以下兩句配置
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
2、library模塊下的build.gradle的配置,這一步的配置信息有點多,基本只要注意那些加了註釋的就行了,然後改成你自己的信息。
apply plugin: 'com.android.library'
//插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
def siteUrl = 'https://github.com/voctex/VtBanner' // Git項目主頁
def gitUrl = 'https://github.com/voctex/VtBanner.git' // Git倉庫url
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
version = "1.0.0" //發佈版本號
group = "com.voctex.banner" //最終引用形式,如compile 'com.voctex.banner:banner:1.0.0',其中banner在後面配置
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
resourcePrefix "vt_" // 隨意命名
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
}
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
pkg {
repo = 'maven'//自己創建的倉庫名字,要跟Bintray上創建的倉庫一樣
name = 'vtbanner'//上傳到JCenter的名字,最終引用的名字
websiteUrl = siteUrl
vcsUrl = gitUrl //這個好像是必須的,我嘗試過傳空字符串好像是不成功的
licenses = ['MIT']//不能隨便寫,只能是倉庫創建時選擇的license type
// userOrg = 'mumusalibrary' //自己創建的organization名稱
publish = true // 是否是公開項目,公開別人可以引用
version {
name = '1.0.0'//版本名,上面好像已經有配置了
desc = 'voctex open library.'//描述,自己定義
released = new Date()
vcsTag = 'v1.0.0'
attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
}
}
configurations = ['archives']
}
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
// Add your description here
name 'Android Banner'//項目名
description 'open library.'//項目描述
url siteUrl
// Set your license
licenses {
license {
name 'MIT' //和之前自己定義的協議一致
url 'https://raw.githubusercontent.com/minggo620/Pluto-Android/master/LICENSE'
}
}
developers {
developer {
id 'voctex' //填寫bintray或者github的用戶名,這裏我填的是我的github名
name 'jreproxy' //姓名,這裏我填的是Bintray上的暱稱
email '[email protected]'//郵箱
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
failOnError false //必須添加以免出錯
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
}
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信息
sdk.dir=/Users/yidont/Library/Android/sdk
bintray.user=jreproxy(Bintray的名字)
bintray.apikey=xxx(這裏需要填寫你的key,需要去bintray取獲取,如下圖)
執行gradle命令
這裏先說明一下windows系統和mac系統執行命令的些許區別,比如
(windows)
gradlew -v
(mac)
./gradlew -v
因爲我用的mac電腦,所以其他人要注意區別。
還有一個要注意的點就是,要對單獨模塊進行編譯,如果在執行命令的時候不順便指定模塊的話,每一次都會默認執行所有模塊,所以這裏要養成一個好習慣。要對單獨模塊進行操作的做法就是加入模塊名,如下
(所有模塊)
./gradlew assembleRelease
(單獨模塊)
./gradlew :app:assembleRelease
然後在Android studio下面的Terminal敲gradle命令,先執行
./gradlew :banner:install
安裝一些需要的東西,然後就把該項目上傳到Bintray中的倉庫裏,執行
./gradlew :banner:bintrayUpload
然後耐心等待,等出現了BUILD SUCCESSFUL字眼就說明上傳成功了,這時候你就可以去你的Bintray上查看你的項目了,進入項目之後還有最後一步需要你操作,就是add to jCenter,然後等待審覈,有人十分鐘就搞定了,有人審覈很慢,我一般都是等了上半天才被審覈成功。
————————————圖片分割線——————————
————————————圖片分割線——————————
最後
到這裏就告一段落了,基本都很簡單,這麼簡單就可以把自己封裝的庫上傳到jCenter中,瞬間有點高大上的樣子。當然,我只是簡單的分享一下我的經驗而已,方便別人也可以簡單學會。
QQ:361561789
有事可直接加Q聯繫