Android Studio Gradle上傳項目到JCenter

本文在簡書同步更新:https://www.jianshu.com/p/3b5f2358de1b
轉載請註明出處

發佈項目到jcenter可以通過兩個插件,一種是gradle-bintray-plugin,另外一種是bintray-release,各有優劣,大家按照個人喜好使用, 本文主要介紹gradle-bintray-plugin。

一、註冊個人賬戶

1、首先登陸https://bintray.com/,請注意,還沒有開始,第一個大坑就已經來襲,請不要選擇首頁的綠色按鈕,這是面向企業版,試用30後要收費的,而且也會有一些未知的問題,我們選擇紅色橢圓圈裏面的source account進入。

2、可以使用github google等網站賬號授權登錄,本文使用的直接註冊,注意不要使用國內的一些郵箱(qq,新浪,163等都不好使),以免到時候驗證郵箱或者密碼比較麻煩。

3、註冊登錄後的界面如下,點擊右上角的Edit Profile進入個人頁面。

4、獲取APIKey,保存,到時候上傳要用到。

二、用Android Studio建立一個簡單的插件

1、新建一個項目TestPlugin(名字隨意),再新建一個module(library),類型選擇Android Library

2、將src/main下的目錄全部刪除,創建一個新的目錄groovy,接着根據module的包名創建目錄,比如我的module包名是com.plugin.gradle.library,那麼就按照這個目錄創建。之後在該目錄下創建一個以groovy後綴名的文件TestPlugin.groovy,作用是打印出一行字,目的是檢驗插件是否能運行。大家直接copy就好,因爲代碼沒有自動提示,需要手寫,項目結構和代碼如下

3、在main目錄下創建resources目錄,在這個目錄下創建META-INF/gradle-plugins, 在目錄裏面我們再創建名爲library.properties的文件,文件名library就是以後給別人引用的插件名。在該文件添加代碼:
implementation-class = com.plugin.gradle.library.TestPlugin

三、配置gradle(重要)

1、先配置好主項目的build.gradle,這裏需要引入兩個依賴

爲了讓gradle更明顯的區分,這裏將項目的結構切換成Android的結構,可以在build.gradle後面灰色字體明顯看到該項目有三個gradle,分別是Project:TestPlugin、module:app、module:library。我們現在用的是Project級別的gradle,配置好gradle順便同步一下項目,AS自動幫我們下載依賴。

2、配置module:library裏面的gradle,因爲我們最終上傳的是這個插件,所以重要的信息都包含在裏面。

apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

version = "0.0.1" //版本

def siteUrl = 'https://github.com/'    // 項目主頁
def gitUrl = 'https://github.com/' //  git

group = "com.plugin.gradle"  

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
}


install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name 'Android Library'
                url siteUrl

                //用apache2.0
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                //開發者基本信息
                developers {
                    developer {
                        id 'pao fan'
                        name 'pao fan'
                        email '[email protected]'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}


Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
    /**
     * 用戶名和key,這兩個信息一般會保存在local.properties裏面,只保留在本地,不上傳到倉庫。
     */
    user = properties.getProperty("bintrayUser")
    key = properties.getProperty("bintrayApiKey")
    println user
    println key
    configurations = ['archives']
    pkg {
        repo = "Maven"               //跟上面創建的Maven倉庫名字保持一致
        name = "library"                //發佈到JCenter上的項目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

用戶名和key存放如下,只是示例,大家請使用自己的賬號信息 - -,這個文件記得.gitignore忽略不要上傳。

四、上傳插件

我們到底怎麼上傳這個插件呢,其實有兩種方法,但本質上是一樣的,可以使用工具,或者使用命令行。
1、使用gradle快捷工具

第一步,在右上角側邊欄找到Gradle,點擊打開,在目錄中展開library-other,雙擊install運行

第二步如圖展開publishing,雙擊bintrayUpload運行。

打開Messages,當你看到BUILD SUCCESSFUL,就代表已經上傳成功啦。

Information:Gradle tasks [bintrayUpload]
Information:BUILD SUCCESSFUL
Information:Total time: 20.01 secs
Information:0 errors
Information:0 warnings
Information:See complete output in console

2、使用命令行
也可以在AS自帶的Terminal使用命令行,先要配置gradle的環境變量才能使用,像JDK一樣。

gradew javadocJar
gradew sourcesJar
gradew install
gradew bintrayUpload

五、Add to JCenter

1、上傳成功後,在個人首頁可以看到如下信息,Maven就是已經上傳的倉庫。

2、點擊Maven進入,這裏有兩個包,library就是剛剛上傳的插件。

3、但現在還不能使用,需要點擊library進入,選擇紅框Add to JCenter,等待後臺審覈便能使用。


簡單粗暴,直接send就可以,接下來等待半天或者一天就可以審覈通過。

4、使用方式

compile 'com.plugin.gradle:library:0.0.1'

六、總結

主要的步驟就是:
1.創建賬號
2.編寫插件(或者隨意寫一個module)
3.編寫module的build.gradle
4.上傳,等待審覈

總的來說不難,需要大家去實踐,那麼,本文到此結束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章