AS上傳Library到JCenter 教程+踩坑記錄

本文使用'com.novoda:bintray-release做爲上傳插件,下面以我我的的utils library上傳爲例進行說明:java

<!--more-->android

註冊bintray帳號

網址:https://bintray.com/signup/ossgit

注:可經過GithubGoogleTwitter受權bintray實現註冊github

建立bintray倉庫

網址:https://bintray.com/profile/editweb

操做步驟:apache

  1. 點擊左側「Repositories」選項,
  2. 點擊該選項右側「New Repository」按鈕,
  3. 填寫Name選項框的值爲「maven」,
  4. 選擇Type選項框的值爲「Maven」,
  5. 點擊「Create」按鈕。

注意:Name選項框的值maven必須是小寫。bash

配置gradle參數

配置gradle-wrapper.properties

#Sun May 21 20:41:27 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip //非此版本號可能會存在`Could not generate a proxy class for class com.android.build.gradle.tasks.BuildArtifactReportTask.`此類錯誤

配置Project的build.gradle

// 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:3.0.0' //非此版本號可能會存在`Could not generate a proxy class for class com.android.build.gradle.tasks.BuildArtifactReportTask.`此類錯誤

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        
        classpath 'com.novoda:bintray-release:+' // 新增
    }
}

allprojects {
    repositories {
        jcenter()
    }
    tasks.withType(Javadoc) { // 新增
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addStringOption('encoding', 'UTF-8')
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

配置Library項目的build.gradle

apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release' // 新增

android {
    ...
    lintOptions { // 新增
        abortOnError false
    }
}

dependencies {
    ...
}

publish { // 新增
    userOrg = 'fqxyi' // 註冊bintray時的username
    groupId = 'com.fqxyi' // 項目包名
    artifactId = 'utils' // 項目名
    publishVersion = '1.0.0' // 發佈版本號
    desc = 'Summarize the tools or methods commonly used in routine development' // 項目描述,可選項
    website = 'https://github.com/fengqingxiuyi/AndroidUtils' // 項目站點,可選項
}

配置其它Module的build.gradle

...

android {
    ...
    lintOptions { // 新增
        abortOnError false
    }
}

...

執行上傳命令

命令:網絡

gradlew clean build bintrayUpload -PbintrayUser=username -PbintrayKey=API Key -PdryRun=false

命令解釋:app

  1. PbintrayUser的值爲註冊bintray時的username,
  2. PbintrayKey的值爲bintray的Api Key,可從https://bintray.com/profile/edit網址左側的API Key選項中獲得,
  3. PdryRun是一個配置參數,當爲true時,表示會運行全部環節,但不會上傳。

申請添加到JCenter

網址:https://bintray.com/username/...maven

注意:

  1. 網址中的username須要更換爲註冊bintray時的username,
  2. 網址中的artifactId須要更換爲項目名。

操做步驟:

  1. 點擊右下角的「Add to jcenter」按鈕添加library package到jcenter,會跳轉到https://bintray.com/message/a...網址,
  2. 點擊「Send」按鈕發送請求。

檢測是否申請經過

訪問https://jcenter.bintray.com/g...網址,若是能看到如下四個文件即表示申請經過:

artifactId-publishVersion-javadoc.jar
artifactId-publishVersion-sources.jar
artifactId-publishVersion.aar
artifactId-publishVersion.pom

注意:網址中的groupId,默認是項目包名,在其成爲網址的一部分後,須要將.符號改成/符號,例如:https://jcenter.bintray.com/com/fqxyi/utils/utils/1.0.0/

FAQ

Repo 'maven' was not found

問題:

* What went wrong:
Execution failed for task ':androidutilslibrary:bintrayUpload'.
        > Could not create package 'fqxyi/maven/androidutilslibrary': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]

解決:未建立倉庫 或 倉庫名寫錯,注意maven是小寫

Lint檢查報錯

問題:Lint檢查報錯,致使Build & Upload失敗

解決:

方式一:須要自行根據錯誤信息修正Error級別的問題,

方式二:爲Library項目的build.gradle配置如下Lint選項實現,--該方式摘自網絡,未驗證

android {

    ...

    lintOptions {
        abortOnError false // 即便有報錯也不會中止打包
        checkReleaseBuilds false // 打包Release版本的時候也不進行Lint檢測
    }
    
    ...
    
}

網絡問題致使的上傳失敗

問題:

* What went wrong:
Execution failed for task ':utils:bintrayUpload'.
> javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
* What went wrong:
Execution failed for task ':utils:bintrayUpload'.
> org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1:8888 refused
* What went wrong:
Execution failed for task ':utils:bintrayUpload'.
> org.apache.http.NoHttpResponseException: The target server failed to respond

解決:

步驟1:關閉代理軟件,重啓網絡;

步驟2:關閉由上傳操做自動開啓的Java客戶端(Mac上會出現,其餘設備不清楚)

上傳成功後jcenter項目首頁不顯示pom

問題:

上傳成功後jcenter項目首頁不顯示pom,點擊add to jcenter彈出如下錯誤提示:

Please fix the following before submitting a JCenter inclusion request: - Add a POM file to the latest version of your package.

是因爲編碼問題致使javadoc生成失敗致使。

解決:

步驟1:在Project的build.gradle中配置如下語句:

...

allprojects {
    ...
    tasks.withType(Javadoc) { // 新增
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addStringOption('encoding', 'UTF-8')
    }
}

...

步驟2:拆分上傳命令:

一、在Terminal執行gradlew clean build命令。

二、先右擊執行generatePomFileForReleasePublication task,再右擊執行publishReleasePublicationToMavenLocal task,具體操做看圖:

Gradle projects

四、在Terminal中執行gradlew bintrayUpload -PbintrayUser=username -PbintrayKey=API Key -PdryRun=false命令

相關文章
相關標籤/搜索