使用Gradle發布aar項目到JCenter倉庫

使用Gradle發布aar項目到JCenter倉庫

目的

發布自己的android library(也就是aar)到公共的jcenter倉庫,所有的人都能用gradle最簡單的方式引用。
為什么選擇jcenter,它兼容maven,而且支持更多形式倉庫,android studio最新版本已經默認jcenter了。

一、注冊賬號

https://bintray.com/上注冊一個賬號。
注意: 注冊時請選擇 “For an Open Source Account Sign Up Here”

二、創建一個maven倉庫

  1. 點擊“Add New Organization”
  2. Name輸入“maven”,Type選擇“Maven”
  3. Create

三、記錄API Key

注冊后,在https://bintray.com/profile/edit頁面的左邊菜單的最下面有API Key,點擊后:

image

復制明文API key,后面會用到。

四、新建項目(以MatrixView為例)

在Android Studio中New一個Project。
完成之后在MatrixView下會有一個build.gradle,在buildscript/dependencies中添加兩個classpath:

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

這是兩個幫助發布項目的插件,完整的project build.gradle請查看這里。

五、新建模塊

在當前Project中New一個Module,比如我這里叫matrixview,這個默認matrixview其實已經是一個完整的Android Library,就可以以aar的形式發布了。
完成之后在matrixview下也會有一個build.gradle,這個build.gradle是今天的重中之重。

六、構建build.gradle

默認的build.gradle大概是這樣的:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 11
        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'])
}

下面我們一步步修改補充這個build.gradle。

1. 添加插件

// 根節點添加
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

2. 定義版本

// 根節點添加
version = "1.0"

3. 定義相關網站

有單獨的官方網站那是最好的,沒用的話就用git網址就可以了。

// 根節點添加
def siteUrl = 'https://github.com/aystshen/MatrixView'    // project homepage
def gitUrl = 'https://github.com/aystshen/MatrixView.git' // project git  

4. 定義Group

舉個例子,當我們引用recylerview的時候是這樣的:

compile 'com.android.support:recyclerview-v7:21.0.2'

引號內字符串以冒號分割為三部分,第一部分就是group,第二部分是name, 第三部分是上面定義的version。

// 根節點添加
group = "com.ayst.matrixview"

5. 定義pom并打包aar

上傳到jcenter至少需要四個文件,除了打包的aar之外,還需要pom和javadoc,source,否則是通不過jcenter審核的。不過不用緊張,這些我們都可以用腳本生成。

// 根節點添加
install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name 'Less Code For Android'
                url siteUrl
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'hbshen'
                        name 'haibo.shen'
                        email 'ayst_shen@me.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

6. 打包javadocjar和sourcejar

這兩個也是上傳到jcenter必須要的。

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
}

7. 上傳到Jcenter倉庫

上傳到jcenter的網站BinTray,需要用戶驗證,需要2個值:

// 示例值, 僅供參考
// your bintray user name
// your bintray api key
bintray.user=hbshen
bintray.apikey=c5434272d522d35d1a0123459981225564155753

第二個API Key就是你在上面記錄的API Key。
因為這個屬于個人隱私,一般不能傳到網上去,所以需要在記錄到matrixview下的local.properties中(利用gitignore忽略這個文件到git),然后腳本再從local.properties中讀取這兩個值。

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")
    configurations = ['archives']
    pkg {
        repo = "maven"
        name = "MatirxView"                // project name in jcenter
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

local.properties如下:

ndk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk\\ndk-bundle
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk
bintray.user=hbshen
bintray.apikey=c5434272d522d35d1a0123459981225564155753

完整的build.gradle請參考這里。

8. 執行

根據上面的build.gradle,會產生幾個特別的gradle任務:

javadocJar
sourcesJar
install
bintrayUpload

通過gradew執行這些任務即可。

gradew javadocJar
gradew sourcesJar
gradew install
gradew bintrayUpload

或者通過Android Studio的最右邊欄的Gradle窗口分別可視化操作,點擊執行:


image

9. 請求審核

當bintrayUpload成功之后,

  1. https://bintray.com/hbshen主頁“Latest Activity”塊,會看到你提交了一個項目
  2. 從這個Latest Activity列表中點擊你的項目
  3. 進入詳情頁https://bintray.com/hbshen/maven/MatrixView
  4. 點擊“Add to JCenter ”,填寫提交信息,就提交審核了

只要上面沒問題,一般審核很快,這次審核通過了,后面升級就不用再審核了。

七、外部項目引用此庫

dependencies {
    compile 'com.ayst.matrixview:matrixview:1.0'
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容