- 前言
開發(fā)中郭蕉,或多或少都會用到無私的程序猿分享的開源項目搁骑,Androidstudio中使用開源也很方便
例如家喻戶曉的Rxjava邓萨,只需要一句話compile 'io.reactivex:rxjava:1.1.6'
朋沮,就可以添加到項目中,時間久了囱淋,是不是自己也想試試開源一下自己的成果猪杭,給大家用用,獲得成(zhuang)就(bi)感妥衣?
開整吧皂吮,盡量詳細講解
- 注冊(第一坑)
注冊本來很簡單,但是在這就有大坑税手,在查找資料時間發(fā)現(xiàn)蜂筹,很多人直接扔的官網(wǎng)注冊地址,但是官網(wǎng)注冊默認是組織芦倒,有30天試用期艺挪,之后要付費。而且發(fā)布時候各種問題兵扬,折騰大半天麻裳,
所以我們需要的網(wǎng)址是:正確注冊地址口蝠、正確注冊地址、正確注冊地址
開始注冊
注冊成功之后
這時候有兩種發(fā)布方式津坑,一種是gradle-bintray-plugin
,一種bintray-release
- 本篇文章將介紹第一種
gradle-bintray-plugin
1.創(chuàng)建一個Maven倉庫妙蔗,點擊Add New Repository
2.進入創(chuàng)建頁面,可以看圖操作
3.設置倉庫為Public疆瑰,Name填寫為maven灭必,類型Type設置Maven,Default Licenses選擇Apache-2.0乃摹,Description就是庫的描述,自由填寫跟衅,然后點擊Create孵睬,稍等幾秒鐘,就完成創(chuàng)建伶跷,然后回到主頁掰读,在Add New Repository位置就可以看到創(chuàng)建的maven倉庫
這時候回到Androidstudio進行配置
4.在Project的build.gradle中,加入Maven和Jfrog Bintray的依賴插件
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:2.0.0-alpha3'
//添加下面兩句叭莫,進行配置
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://raw.github.com/bmob/bmob-android-sdk/master" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
5.然后在library的build.gradle中進行配置蹈集,例如我要開源的項目FloatingActionButton
完整的build.grale中,
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
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:25.3.1'
testCompile 'junit:junit:4.12'
}
//從這里開始配置
//添加這兩行
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//項目主頁--項目的GitHub地址
def siteUrl = 'https://github.com/dazhaoDai/FloatingActionButton'
//項目的git地址
def gitUrl = 'https://github.com/dazhaoDai/FloatingActionButton.git'
//發(fā)布到JCenter上的項目名字雇初,compile引用時的第2部分項目名
def libName = "FloatingActionButton"
//compile引用時的第1部分
group = "com.ddz.materialdesign"
// 版本號拢肆,//compile引用時的最后部分項目名,下次更新是只需要更改版本號即可
version = "1.0.1"
//上面配置后上傳至JCenter后的編譯路徑是這樣的: compile 'com.ddz.materialdesign:FloatingActionButton:1.0.1'
//生成源文件
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 'A customizable FloatingActionButton on Android'
url siteUrl
licenses {
license {
//開源協(xié)議
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
//開發(fā)者的個人信息,根據(jù)個人信息填寫
id 'ddz'
name 'daidazhao'
email 'daidazhao@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值必須要和你創(chuàng)建Maven倉庫的時候的名字一樣刊橘,我們前面創(chuàng)建的就是maven
repo = "maven"
//發(fā)布到JCenter上的項目名字
name = libName
//項目描述
desc = 'A customizable FloatingActionButton on Android'
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options {
//如果你的項目里面有中文注釋的話鄙才,必須將格式設置為UTF-8,不然會出現(xiàn)亂碼
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
6.這個時候促绵,還差關鍵一步攒庵,就是設置user和apikey
在local.properties中,添加
bintray.user=dazhao
bintray.apikey=*****************************************************
這里用到user败晴,是之前注冊的用戶名浓冒,看圖
而apikey,則需要我們去申請,具體步驟尖坤,點擊Edit Profile裆蒸,選擇API KEY,根據(jù)登錄密碼糖驴,申請API KEY僚祷,具體流程看圖
根據(jù)登錄密碼佛致,申請KEY
把user和apikey填到上面的local.properties文件中。配置工作就此結束
開始提交
7.第一步----打開Androidstudio的Terminal窗口辙谜,Windows系統(tǒng)輸入gradlew install
,Mac加上在前面加上./俺榆,回車,
如果成功的話會出現(xiàn)
BUILD SUCCESSFUL
8.然后進行第二步装哆,輸入gradlew bintrayUpload
罐脊,回車
看到BUILD SUCCESSFUL,說明提交成功了
9.這個時候打開maven倉庫蜕琴,可以看到萍桌,已經提交成功了
現(xiàn)在需要最后一步,進入項目凌简,右下角有個Add to Jcenter按鈕
10.點擊打開上炎,添加描述信息,直接點擊Send雏搂,等待JCenter審核
11.審核等待時間不太長藕施,甚至1個小時左右就成功了,審核通過之后凸郑,Add to Jcenter按鈕會消失裳食,同時會受到消息提示
至此,通過gradle-bintray-plugin方式芙沥,提交開源項目诲祸,就完成了。
我們就可以愉快的使用了自己造的輪子了
compile 'com.ddz.materialdesign:FloatingActionButton:1.0.1'
但是而昨,說好的挖坑和填坑烦绳,肯定不會就這樣:
填坑
- 坑1
What went wrong:
Execution failed for task ':FloatingActionButton:bintrayUpload'.
Could not create package 'daidazhao/maven/FloatingActionButton': HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
遇到這種錯,提示local.properties中user或者apikey設置錯誤配紫,先檢查user名字和apikey是否設置錯誤径密,如果檢查無誤,極有可能是之前注冊使用的官網(wǎng)地址躺孝,導致報錯
- 坑2
Could not upload to 'https://xxxxxxx.pom': HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'xxxxxxx.pom']
說明library中rope的值和倉庫名字不一致享扔,檢查兩個值是否一致
- 坑3
- What went wrong:
Execution failed for task ':FloatingActionButton:bintrayUpload'.
Could not create package 'dazhao/maven/FloatingActionButton': HTTP/1.1 404 Not Found
找不到用戶名,說明local.properties中user填寫植袍,檢查名字是否用的是注冊名字
總結
整理整個過程
1.注冊賬號
2.創(chuàng)建maven的Repository
3.在項目根目錄的build.gradle中添加bintray插件
4.修改Module中的build.gradle中的配置
5.在local.properties中添加user和apikey的認證
6.在Android Studio的Terminal使用 gradlew install / bintrayUpload上傳
7.最后在maven倉庫中同步到Jcenter中
8.通過審核惧眠,開始使用
參考