今天突然心血來潮,想折騰一下,讓自己的庫能夠被遠程依賴。在網上找了很多教程,以及很多朋友的幫忙(尤其是 小別墅),終于折騰出來了。話不多說。開始教程:
一.注冊bintray賬號
bintray的官網:https://bintray.com
個人注冊地址:https://bintray.com/signup/oss
也可以直接用github賬戶登錄。我就是這樣的。但是這樣有一個要求:你的github綁定了郵箱,而且是在國外可用的(Gmail,outlook等)。
二.新建maven倉庫
不多說。看圖說話
創建完成之后就會多一個maven
三.項目配置。
1.根目錄配置
把上傳好的,在GitHub上公開的項目導入下來(本地已有就不用再次導入了)
在項目的根目錄下的build.gradle如下配置
dependencies{
//注意:此處版本如果不是2.3.3,下方的版本有可能無效或者報錯。更換此處版本或者下方的版本即可。報錯及解決方案在下方
classpath'com.android.tools.build:gradle:2.3.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
}
報錯:
No service of type Factory available in ProjectScopeServices.錯誤解決:
https://code.google.com/p/android/issues/detail?id=219692
上面這個鏈接需要翻墻才可查看。但是終究是版本號不匹配,多嘗試更換幾次版本即可。
如:classpath 'com.android.tools.build:gradle:2.1.3' ,這時就要使用
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1',而不是
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
修改完成之后如圖:
2.依賴配置
在library的module下(你想要讓別人遠程依賴的module)build.gradle中進行如下配置
// 添加下面兩行代碼即可。
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
defaultConfig {
// applicationId "xxx.xxxx.xxx" // 如果有這一行要刪除,library沒有applicationId
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName '1.0.1'
}
然后在最下方
dependencies {
compile fileTree(include: ['*.jar'],dir:'libs')
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:design:26.0.0-alpha1'
}
沒錯,就是在上面這些代碼的更下方添加如下代碼:
// 項目引用的版本號,比如compile 'com.yanzhenjie:andserver:1.0.1'中的1.0.1就是這里配置的。
version="0.1"
// 定義兩個鏈接,下面會用到。
// 項目主頁。
def siteUrl ='https://github.com/xxxxx/項目地址'
// Git倉庫的url。(此處可以是ssh鏈接,也可以是http鏈接)
def gitUrl ='git@github.com:xxxx/項目地址.git'
// 唯一包名,比如compile 'com.yanzhenjie:andserver:1.0.1'中的com.yanzhenjie就是這里配置的。唯一
group="com.xxxx"?
install {
repositories.mavenInstaller{
// 生成pom.xml和參數
pom {
project{
packaging'aar'
// 項目描述,這里需要修改。
name 'BaseLibrary For Android'// 可選,項目名稱。
description 'The Android build the framework of the base library.'// 可選,項目描述。
url siteUrl// 項目主頁,這里是引用上面定義好。
// 軟件開源協議,現在一般都是Apache License2.0吧,復制我的,這里不需要修改。
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
//填寫開發者基本信息,這里需要修改。
developers {
developer {
id 'xxxx'// 開發者的id。
name 'xxxx'// 開發者名字。
email 'xxxxx@gmail.com'// 開發者郵箱。
}
}
// SCM,復制我的,這里不需要修改。
scm {
connection gitUrl// Git倉庫地址。
developerConnection gitUrl// Git倉庫地址。
url siteUrl// 項目主頁。
}
}
}
}
}
// 生成jar包的task,不需要修改。
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier='sources'
}
// 生成jarDoc的task,不需要修改。
task javadoc(type: Javadoc) {
options { //如果項目包含中文,最好配置上options,如果沒有中文,options這一整項可不要
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
source= android.sourceSets.main.java.srcDirs
classpath+=project.files(android.getBootClasspath().join(File.pathSeparator))
// destinationDir = file("../javadoc/")
// 忽略注釋語法錯誤,如果用jdk1.8你的注釋寫的不規范就編譯不過。(如果電腦上有JDK1.9,配置之后還報錯,一定要卸載JDK1.9,切記)
failOnError false
}
// 生成javaDoc的jar,不需要修改。
task javadocJar(type: Jar,dependsOn: javadoc) {
classifier='javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
// 這里是讀取Bintray相關的信息,我們上傳項目到github上的時候會把gradle文件傳上去,所以不要把帳號密碼的信息直接寫在這里,寫在local.properties中,這里動態讀取。
Properties properties =new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")// Bintray的用戶名。
key = properties.getProperty("bintray.apikey")// Bintray剛才保存的ApiKey。
configurations = ['archives']
pkg {
repo ="maven"http:// 上傳到maven庫。
// 發布到Bintray上的項目名字,這里的名字不是compile 'com.yanzhenjie:andserver:1.0.1'中的andserver。而是你在bintray網站上,你這個項目的名字。
name ="項目名字"
userOrg ='xxxx' // Bintray的用戶名。
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish =true// 是否是公開項目。
}
}
下面是修改之后的圖:
可以明確看到,配置中引用了一個文件(local.properties)。最后還有還有一步:打開local.properties文件(項目根目錄下)
復制這兩行進去:
bintray.user=YOUR_BINTRAY_USERNAME
bintray.apikey=YOUR_BINTRAY_API_KEY
把上方的YOUR_BINTRAY_USERNAME和YOUR_BINTRAY_API_KEY替換成你自己的。
獲取方式如下圖:
三.上傳項目,用以共享(遠程依賴)
打開android studio的命令行:
如圖:
輸入命令:gradlew install
等待編譯完成。BUILD SUCCESSFUL
再輸入命令:gradlew bintrayUpload
等待編譯完成。BUILD SUCCESSFUL
或者不執行上面這兩個命令,直接合成執行下面這個命令就可以了:
gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false
記住要把user和key換成自己的,同樣要顯示BUILD SUCCESSFUL才可以。
當成功執行完上述命令之后,登陸https://bintray.com/,你會發現maven中多了一個項目了。
進入maven。可以看到已經成功了。
進入項目(不翻墻進入會很慢,多等等就好)并將項目分享到jcenter。到這里本教程就基本完成了。
最后別忘記提交
提交之后等待審核之后就能依賴使用了。我等了一天才能用。
本次教程到此結束。第一次寫文章,有不完善的地方請提出您寶貴的意見
追加:
我以后想增加、迭代怎么辦?
這個非常簡單,當你的Library代碼更改后,先上傳同步,提交到github之后,只需要更改一下上面的配置里面的version
輸入命令:gradlew install
等待編譯完成。BUILD SUCCESSFUL
再運行
gradlew clean build bintrayUpload -PdryRun=false,就可以更新版本號了。
如果代碼運行失敗,就像之前上傳一樣,將代碼分成兩次執行。
輸入命令:gradlew install
等待編譯完成。BUILD SUCCESSFUL
再輸入命令:gradlew bintrayUpload
等待編譯完成。BUILD SUCCESSFUL
這樣,整個過程就結束了。更新的代碼可以立即使用(已經通過本人測試)
附:上方的baselibrary中包含的庫及其常見使用?http://www.lxweimin.com/p/a97d8c564d19