關於android發佈倉庫到外部項目的文章,目前大概爲分兩類:java
一種是發佈到bintray、jcenter上,一種是將項目上傳到github,而後利用jitpack自動打包,下面簡單說說各自的操做方法。android
略過git
略過github
略過web
在項目的gradle文件的buildscript/dependencies節點添加如下兩行代碼:ajax
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
sdk.dir=你的sdk路徑 # 其實你只須要添加下面兩行,第一個填你的用戶名,好比個人是yolanda。 bintray.user=yolanda bintray.apikey=fa************************5a
apply plugin: 'com.android.library' // 這裏添加下面兩行代碼。 apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'com.jfrog.bintray' android { compileSdkVersion 23 buildToolsVersion "23.0.3" resourcePrefix "andserver_res_" defaultConfig { // applicationId "com.yanzhenjie.andserver.sample" // 這一行要刪除,由於library不容許有applicationId。 minSdkVersion 8 targetSdkVersion 23 versionCode 1 versionName '1.0.1' } } dependencies { // 若是你的library有依賴別的jar,這裏要把jar依賴進來。 compile fileTree(dir: 'libs', includes: ['*.jar']) } // 項目引用的版本號,好比compile 'com.yanzhenjie:andserver:1.0.1'中的1.0.1就是這裏配置的。 version = "1.0.1" // 定義兩個連接,下面會用到。 def siteUrl = 'https://github.com/yanzhenjie/AndServer' // 項目主頁。 def gitUrl = 'git@github.com:yanzhenjie/AndServer.git' // Git倉庫的url。 // 惟一包名,好比compile 'com.yanzhenjie:andserver:1.0.1'中的com.yanzhenjie就是這裏配置的。 group = "com.yanzhenjie" install { repositories.mavenInstaller { // 生成pom.xml和參數 pom { project { packaging 'aar' // 項目描述,複製個人話,這裏須要修改。 name 'AndServer For Android'// 可選,項目名稱。 description 'The Android build the framework of the Http server.'// 可選,項目描述。 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 'yanzhenjie' // 開發者的id。 name 'yanzhenjie' // 開發者名字。 email 'smallajax@foxmail.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) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) // destinationDir = file("../javadoc/") failOnError false // 忽略註釋語法錯誤,若是用jdk1.8你的註釋寫的不規範就編譯不過。 } // 生成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" // 上傳到maven庫。(這裏要特別注意,若是寫了maven報404錯誤,請在bintray建立一個倉庫,這裏填改爲你建立的倉庫的名字,如何建立請看下圖。) name = "andserver" // 發佈到Bintray上的項目名字,這裏的名字不是compile 'com.yanzhenjie:andserver:1.0.1'中的andserver。 userOrg = 'bintray_user' // Bintray的用戶名,2016年11月更新。 websiteUrl = siteUrl vcsUrl = gitUrl licenses = ["Apache-2.0"] publish = true // 是不是公開項目。 } }
準備工做都作完啦,最後一步就是上傳操做了,點擊AndroidStudio底部的Terminal,觀察下Terminal顯示的路徑是不是你當前項目的root。apache
gradle install
,若是沒有配置gradle用戶環境,輸入gradlew install
,若是沒有問題,最終你會看到BUILD SUCCESSFUL
。failOnError false
這句話,在剛纔編寫gradle時提示過了。若是加了這句而你的javadoc寫的不規範會有警告,你不用鳥它。gradle install
後看到BUILD SUCCESSFUL
後,再輸入上傳命令gradle bintrayUpload
,等一分鐘左右就執行完了,會提示SUCCESSFUL
。審覈經過後,就能夠正常引用了 api
在項目的gradle文件的buildscript/dependencies節點添加如下代碼:瀏覽器
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
apply plugin: 'com.github.dcendents.android-maven' group='com.github.dessmann'
上傳項目時,須要保證項目的gradle文件夾與gradlew、gradlew.bat也同步上傳,否則jitpack可能沒法打包app
在jitpack.io主頁上粘貼github版本庫的項目路徑,執行發佈,須要注意的是,若是是私有項目,須要在jitpack.io主頁上切換到private,而後在網頁底端找到受權的按鈕,生成受權碼,在下面引用的時候,若是引用的是私有項目,須要用到該受權碼
allprojects {
repositories {
maven {
url 'https://jitpack.io'
credentials { username authToken }//若是是私有項目,須要添加該行以供jitpack驗證
}
google()
jcenter()
}
}
其中authToken是以前受權碼