Gradle打包并發(fā)布aar

以前使用開源庫比較常用做法是下載源碼包,通過import library引入。但通常我們都不需要修改源碼,而且升級(jí)的時(shí)候又要去下載一次源碼再替換,顯得比較麻煩。后來轉(zhuǎn)用Android Studio之后自帶gradle構(gòu)建項(xiàng)目,通過依賴管理輕松實(shí)現(xiàn)更新第三方庫。我也開始把我的開源項(xiàng)目轉(zhuǎn)為Android Studio,并提供aar包給開發(fā)者進(jìn)行依賴。現(xiàn)在就開始介紹下怎么打包并發(fā)布aar。

環(huán)境

Android Studio 1.0+
OSX 10.9+

建立項(xiàng)目

在Android Studio中新建一個(gè)Project。以我的PickerView控件為例,New 一個(gè)項(xiàng)目名叫 PickerViewDemo 的項(xiàng)目。
然后之后在 PickerViewDemo 下會(huì)有一個(gè)build.gradle(Project:PickerViewDemo),在buildscript里面的dependencies中添加兩個(gè)classpath:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        //請(qǐng)加入下面兩行
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
        // 高版本的gradle改為
        // classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

建立真正的需要打包成aar的library庫

在當(dāng)前PickerViewDemo的project中New一個(gè)Module,那么我的叫 PickerView,這個(gè)就是要打包成aar并發(fā)布的依賴庫了。
PickerView下也有一個(gè)build.gradle(pickerview),我們需要配置這個(gè)build.gradle。拷貝覆蓋掉這個(gè) build.gradle,然后請(qǐng)看注釋來修改成你的。

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

version = "1.0.1"   // #修改# // 這里是aar的版本號(hào)

android {
    compileSdkVersion 21
    buildToolsVersion "20.0.0"

    defaultConfig {
        minSdkVersion 9
        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'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

def siteUrl = 'https://github.com/saiwu-bigkoo/Android-PickerView'                        // #修改# // 項(xiàng)目的主頁地址,我這里是我的PickerView項(xiàng)目在github的鏈接地址
def gitUrl = 'https://github.com/saiwu-bigkoo/Android-PickerView.git'                     // #修改# // 項(xiàng)目 git 地址,我這里同樣是用Github上的git地址
group = "com.bigkoo"             // #修改# // 組名稱,這個(gè)相當(dāng)于依賴的時(shí)候 compile 'com.bigkoo:pickerview:1.0.1' “:”號(hào)前面的前綴

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name 'PickerView For Android'                                   // #修改# // 標(biāo)題
                url siteUrl
                // Set your license
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'sai'                                           // #修改# // 你的userid,昵稱
                        name 'sai.wu'                                       // #修改# // 用戶名
                        email 'sai.wu@bigkoo.com'                               // #修改# // 郵箱
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

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
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")
    // 上面兩個(gè) user和key 需要留意一下,在local.properites 里面配置的
    configurations = ['archives']
    pkg {
        repo = "maven"
        name = "PickerView"                                                 // #修改# //  在 jcenter 上面的項(xiàng)目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

然后我們需要到 Bintray 上進(jìn)行注冊登錄, 完成后到編輯界面左邊菜單,查看API Key,點(diǎn)擊show就能看到你的key了。接下來請(qǐng)拷貝這個(gè)key,然后回到項(xiàng)目中。
local.properites 加入兩行配置:

bintray.user=這里填寫你在Bintray中的user名字
bintray.apikey=這里是你剛剛拷貝的key

local.properites 文件因?yàn)橛心愕碾[私信息,所以如果上傳到git上面,記得排除掉不要上傳哦~~(即在.gitignore里面加入local.properties以忽略版本控制)

打包和提交

用終端進(jìn)入到 PickerViewDemo的根目錄。然后執(zhí)行

./gradlew build  // 第一次上傳可能需要 ./gradlew install
./gradlew bintrayUpload //上傳到Bintray

上面已經(jīng)傳到Bintray上面了,接下來回到 Bintray 網(wǎng)站,到個(gè)人主頁,右下角Latest Activity 中會(huì)出現(xiàn)你剛剛上傳的項(xiàng)目,如我的是PickerView,那么這就是你已經(jīng)傳到倉庫中了,然后到Jcenter 中點(diǎn)擊 Include My Package
選擇這個(gè)項(xiàng)目,然后提交等待審核。

依賴

審核通過后,在build.gradle(Module:app)dependencies里面加入

compile 'com.bigkoo:pickerview:1.0.1'//這是我的例子,請(qǐng)?zhí)鎿Q成你自己的

sync gradle 一下,就會(huì)自動(dòng)下載aar并依賴,嗯,這樣就完成了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容