以前使用開源庫比較常用做法是下載源碼包,通過import library引入。但通常我們都不需要修改源碼,而且升級的時候又要去下載一次源碼再替換,顯得比較麻煩。后來轉(zhuǎn)用Android Studio之后自帶gradle構(gòu)建項目,通過依賴管理輕松實現(xiàn)更新第三方庫。我也開始把我的開源項目轉(zhuǎn)為Android Studio,并提供aar包給開發(fā)者進行依賴。現(xiàn)在就開始介紹下怎么打包并發(fā)布aar。
環(huán)境
Android Studio 1.0+
OSX 10.9+
建立項目
在Android Studio中新建一個Project。以我的PickerView控件為例,New 一個項目名叫 PickerViewDemo 的項目。
然后之后在 PickerViewDemo 下會有一個build.gradle(Project:PickerViewDemo),在buildscript里面的dependencies中添加兩個classpath:
// 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:1.0.0'
//請加入下面兩行
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
// 高版本的gradle改為
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
allprojects {
repositories {
jcenter()
}
}
建立真正的需要打包成aar的library庫
在當前PickerViewDemo的project中New一個Module,那么我的叫 PickerView,這個就是要打包成aar并發(fā)布的依賴庫了。
在PickerView下也有一個build.gradle(pickerview),我們需要配置這個build.gradle??截惛采w掉這個 build.gradle,然后請看注釋來修改成你的。
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
version = "1.0.1" // #修改# // 這里是aar的版本號
android {
compileSdkVersion 21
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
def siteUrl = 'https://github.com/saiwu-bigkoo/Android-PickerView' // #修改# // 項目的主頁地址,我這里是我的PickerView項目在github的鏈接地址
def gitUrl = 'https://github.com/saiwu-bigkoo/Android-PickerView.git' // #修改# // 項目 git 地址,我這里同樣是用Github上的git地址
group = "com.bigkoo" // #修改# // 組名稱,這個相當于依賴的時候 compile 'com.bigkoo:pickerview:1.0.1' “:”號前面的前綴
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'PickerView For Android' // #修改# // 標題
url siteUrl
// Set your license
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'sai' // #修改# // 你的userid,昵稱
name 'sai.wu' // #修改# // 用戶名
email 'sai.wu@bigkoo.com' // #修改# // 郵箱
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
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))
}
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")
// 上面兩個 user和key 需要留意一下,在local.properites 里面配置的
configurations = ['archives']
pkg {
repo = "maven"
name = "PickerView" // #修改# // 在 jcenter 上面的項目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
然后我們需要到 Bintray 上進行注冊登錄, 完成后到編輯界面左邊菜單,查看API Key,點擊show就能看到你的key了。接下來請拷貝這個key,然后回到項目中。
在local.properites 加入兩行配置:
bintray.user=這里填寫你在Bintray中的user名字
bintray.apikey=這里是你剛剛拷貝的key
local.properites 文件因為有你的隱私信息,所以如果上傳到git上面,記得排除掉不要上傳哦~~(即在.gitignore里面加入local.properties以忽略版本控制)
打包和提交
用終端進入到 PickerViewDemo的根目錄。然后執(zhí)行
./gradlew build // 第一次上傳可能需要 ./gradlew install
./gradlew bintrayUpload //上傳到Bintray
上面已經(jīng)傳到Bintray上面了,接下來回到 Bintray 網(wǎng)站,到個人主頁,右下角Latest Activity 中會出現(xiàn)你剛剛上傳的項目,如我的是PickerView,那么這就是你已經(jīng)傳到倉庫中了,然后到Jcenter 中點擊 Include My Package
選擇這個項目,然后提交等待審核。
依賴
審核通過后,在build.gradle(Module:app)的dependencies里面加入
compile 'com.bigkoo:pickerview:1.0.1'//這是我的例子,請?zhí)鎿Q成你自己的
sync gradle 一下,就會自動下載aar并依賴,嗯,這樣就完成了。