Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

把 Android 開源專案發佈到 JCenter 的完整流程與踩坑筆記

編輯:關於 Android 程式設計

我們每天都在 build.gradle 裡寫 compile 'com.android.support:design:23.4.0',一行字就把別人的成果拉進自己的專案。那反過來——要把自己寫的 library 也變成這一行字,需要經歷哪些步驟?本文把整個流程與常見錯誤整理成一份可照做的清單。

名詞先對齊

這條鏈路有三個角色:Bintray 是 artifact 的托管平台,JCenter 是掛在 Bintray 上、預設被 Gradle 當成第一個查詢來源的倉庫,Gradle plugin 則負責把本地 build 出的 aar 與產生的 POM 描述檔推上去。

值得先知道的一點背景:JFrog 已於 2021 年宣佈 Bintray 與 JCenter 停止服務,JCenter 轉為唯讀後逐步走入歷史,如今主流做法已改為發佈到 Maven Central(搭配 Sonatype 的 maven-publish 與 GPG 簽章)。本文的流程仍有參考價值——groupId、POM 描述、sources/javadoc jar、簽章這些觀念是共通的——但若你今天要真的上線,建議直接把目標換成 Maven Central。

步驟一:建立專案與帳號

先準備好要發佈的 library module(獨立於 app module 之外),再前往 Bintray 網站完成註冊。註冊後即可進入後續設定。

步驟二:根專案的 build.gradle

在 buildscript 的 dependencies 裡加入兩個 classpath 外掛:一個負責跟 Bintray 溝通,一個負責產生 Maven 格式所需的 POM。

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

步驟三:library module 的 build.gradle

這是最關鍵的一段。除了原有的 android 設定,還要補上 group 與 version、描述 POM 的 install 區塊、產生 sources 與 javadoc jar 的 task,以及最後負責上傳的 bintray 區塊。把缺的補上、原有的不要動即可。

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

// 提交到倉庫的版本號
version = "1.0.0"

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"
    resourcePrefix "superbro"

    defaultConfig {
        applicationId "com.superbro.myapplication"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0-beta1'
    compile 'com.android.support:design:24.0.0-beta1'
    compile 'com.android.support:support-v4:24.0.0-beta1'
}

def siteUrl = 'https://github.com/choubaguai/PhotoSelecter'   // 專案首頁
def gitUrl  = 'https://github.com/choubaguai/PhotoSelecter'   // Git 倉庫網址
group = "com.superbro"                                        // groupId,必填,建議用唯一包名

install {
    repositories.mavenInstaller {
        pom {
            project {
                packaging 'aar'
                name 'Android Photo Selecter'
                url siteUrl
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'https://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'superbro'
                        name 'superbro'
                        email 'superbro@163.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")     // 讀取 local.properties 的 bintray.user
    key  = properties.getProperty("bintray.apikey")   // 讀取 local.properties 的 bintray.apikey
    configurations = ['archives']
    pkg {
        repo = "maven"
        name = "photoselecter"      // 發佈到 JCenter 上的專案名稱,必填
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

// 這段是為了避免中文註解造成 Javadoc 編碼錯誤
javadoc {
    options {
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version true
        links "https://docs.oracle.com/javase/7/docs/api"
    }
}

步驟四:把憑證放進 local.properties

帳號與 API Key 不要寫進 build.gradle 進版本控制,放在 local.properties 由程式讀取才是安全做法:

// username 是註冊時使用的使用者名稱(不是 Email)
// apikey 由網站產生,位置如下圖
bintray.user=username
bintray.apikey=paikey

進入該頁面的方式是點開個人頭像後選擇 Edit Profile。進去之後點擊 1,輸入密碼驗證,再點擊 2 就能看到 API Key,複製貼上即可。

步驟五:上傳

準備工作完成後先 Sync 專案,讓新的 plugin 生效。

接著開啟 Android Studio 下方的 Terminal 面板:

先在本機產生並安裝 artifact:

gradlew install

沒有問題的話會看到:

BUILD SUCCESSFUL

接著把編譯結果推到 Bintray:

gradlew bintrayUpload

同樣會看到:

BUILD SUCCESSFUL

步驟六:申請加入 JCenter

上傳成功後回到網站,就能看到你剛推上去的專案。但這時它只在自己的倉庫裡,別人還抓不到。打開專案頁面,右下角有一個 linked to JCenter 的入口:

點進去之後什麼都不用填,直接按下方的 Send 送出申請。管理員審核通過後,這行 compile 就能在全世界任何人的專案裡生效了。


問題總結

1. 出現 GBK 相關的亂碼或編碼錯誤。這幾乎都是中文註解惹的禍,Javadoc 在預設編碼下無法處理 UTF-8 中文。解法是在 module 的 build.gradle 補上 javadoc 的編碼設定:

javadoc {
    options {
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version true
        links "https://docs.oracle.com/javase/7/docs/api"
    }
}

2. 提示找不到 class、interface 或 enum。把原始碼檔案的編碼格式改成 UTF-8。建議用 Notepad++ 或 EditPad 這類編輯器轉存,Windows 記事本存檔時常會加上 BOM 反而造成新的問題。

3. 提示找不到 username 或 APIKEY。檢查 local.properties 裡的 key 名稱大小寫是否與 build.gradle 中 getProperty() 的字串完全一致,這是大小寫敏感的比對。

4. HTTP/1.1 401 Unauthorized [message:This resource requires authentication]。代表 apikey 或 username 的值寫錯了,回到步驟四重新複製一次即可。

大致上就是這幾類問題。整個流程走順之後大約十分鐘,真正耗時的通常是最後那道人工審核。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved