在安卓開發中我們經常可以看到在gradle中引入compile來引入第三方庫,那麼這到底是怎麼回事?這個是Android項目所在jcenter的一個遠程倉庫。我們只需要將你的libray上傳到jcenter即可,那麼如何將一個項目上傳到jcenter呢?
1.去jcenter官網註冊個賬號:https://bintray.com
註冊界面如下圖:
2.在項目根目錄的build.gradle(Project)添加配置
在如下圖中的位置加入如下配置
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.0.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
3.在你要上傳的module的build.gradle中添加如下配置
在如上圖中的位置加入如下配置:配置中都有詳細的註釋是什麼作用。
/** 以下開始是將Android Library上傳到jcenter的相關配置**/
apply plugin:'com.github.dcendents.android-maven'
apply plugin:'com.jfrog.bintray'
//項目主頁
def siteUrl ='https://github.com/czl0325/ZLSqlDatabase-Android'
//項目的版本控制地址
def gitUrl ='https://github.com/czl0325/ZLSqlDatabase-Android.git'
//發佈到組織名稱名字,必須填寫
group ="com.github.czl0325"
//發佈到JCenter上的項目名字,必須填寫
def libName ="ZLSqlDatabase"
// 版本號,下次更新是只需要更改版本號即可
version ="1.0.0"
/** 上面配置後上傳至jcenter後的編譯路徑是這樣的:compile 'com.github.czl0325:ZLSqlDatabase:1.0.0' **/
//生成源文件
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))
options.encoding 'UTF-8'
options.charSet 'UTF-8'
options.author true
options.version true
options.links "https://github.com/linglongxin24/FastDev/tree/master/mylibrary/docs/javadoc"
failOnError false
}
//文檔打包成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 'sqlite framework'
url siteUrl
licenses {
license {
name 'sqlite framework'
url 'https://github.com/czl0325/ZLSqlDatabase-Android'
}
}
developers {
developer {
id'czl'
name'czl0325'
email'[email protected]'
}
}
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"
name ="ZLSqlDatabase" //發佈到JCenter上的項目名字,必須填寫
desc ='sqlite framework' //項目描述
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
4.在你項目根目錄的local.properties中添加用戶名和API Key
在如下圖中的位置先找到自己的APIkey:登錄到Jcenter官網->Edit Profile->API Key
在如下圖中的位置加入如下配置
5.執行命令
(1)生成maven庫所需要的POM文件:
gradlew install
如果查看到BUILD SUCCESSFUL說明POM文件已經生成成功,並且在你的Android studio的Project視圖中可以看到javadoc和javasource的jar包文件
請注意,mac電腦下是使用
gradle install
沒有了w,在mac電腦下要從終端啓動,進入項目目錄,先查看adb devices 和 gradle -v版本,如果沒有安裝請用brew update gradle安裝。
(2)上傳你的Library庫到jcenter的maven庫:graedlew bintrayUpload
gradlew bintrayUpload
如果查看到BUILD SUCCESSFUL說明你的Library已經成功上傳到了jcenter的maven庫
6.查看上傳的maven庫,並提交審覈
登錄jcenter首頁->點擊下方maven即可進入到你上傳的maven庫列表界面
在maven庫列表界面找你你剛纔上傳的maven庫點進去即可看到相關的項目詳情
在你上傳的maven庫詳情界面最下面可以看到:Add to JCenter 按鈕
點擊Add to JCenter 按鈕填寫項目描述即可提交。這個網站在國外維護,由於時差的原因一般他們上班時間在我們的晚上,等到第二天即可通過審覈
第二天查看通過審覈的界面
7.遇到的各種坑以及解決辦法
(1)坑一:錯誤: 編碼GBK的不可映射字符->請正確配置javadoc編碼
java//生成文檔
task javadoc(type:Javadoc){
options.encoding"UTF-8"
options.charSet'UTF-8'
}
(2)坑二:錯誤: 不允許使用自關閉元素->請刪除javadoc註釋裏面所有的含有html標籤,
(3)坑三: 錯誤: 程序包android.support.v7.widget不存在;錯誤: 找不到符號 ->在javadoc中加入忽略錯誤配置
(4)坑四:Could not create version ‘0.1’: HTTP/1.1 401 Unauthorized [message:This resource requires authentication],->沒有配置正確的API Key
(5)坑五:沒有有效的POM文件->一定要按步驟執行並沒有配置正確的API Key:
(6)坑六:沒有Add to JCenter按鈕:
注意:在這個地址註冊:https://bintray.com/signup/oss;不是https://bintray.com/signup;這兩個地址不一樣的!
如果後面還有什麼問題,可以一起交流。
最後如果還有其他問題推薦查看這篇文章,寫的很詳細
Android快速發佈項目到jcenter :http://blog.csdn.net/zhcswlp0625/article/details/54895584