如何快速将Android库发布到JCenter
將Android庫發布到jcenter是為了更方便的引用
一般有以下幾個步驟:
1. 新建工程和module
新建工程,并將你要發布的獨立成一個module,便于發布
example: 
 
這里面新建了lib-db module,我們的目標就是將它達成aar格式并發布到jcenter中
2. 修改build.gradle
project 的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {repositories {google()jcenter()}dependencies {classpath 'com.android.tools.build:gradle:3.1.3'//關鍵的二行代碼必須加上classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files} }allprojects {repositories {google()jcenter()} }task clean(type: Delete) {delete rootProject.buildDir }module的build.gradle
apply plugin: 'com.android.library' //1、引用插件 apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'com.jfrog.bintray'android {compileSdkVersion 28defaultConfig {minSdkVersion 14targetSdkVersion 28versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}}dependencies {implementation fileTree(dir: 'libs', include: ['*.jar']) } //2、自定義一些屬性,這些屬性在upload.gradle都會使用到 ext { //發布到bintray上倉庫名,執行上傳前必須手動創建同名到倉庫,否則上傳會失敗bintrayRepo = 'framework'bintrayName = 'db' //群組idpublishedGroupId = 'com.fzm.db' //發布成庫的名稱libraryName = 'db' //最好和libraryName保持一致artifact = 'db' //庫描述libraryDescription = 'a db library for fzm'siteUrl = 'github project index page'gitUrl = 'github project url'libraryVersion = '0.0.3'developerId = 'developer id'developerName = 'your name'developerEmail = 'xxxx@163.com'licenseName = 'The Apache Software License, Version 2.0'licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'allLicenses = ["Apache-2.0"] } //此處是為了將發布的代碼邏輯封裝到upload.gradle腳步中去了,這樣build.gradle更簡潔,代碼維護性更高 apply from : 'upload.gradle'module name建議和bintrayName、libraryName保持一致
upload.gradle
此腳步文件格式比較固定,可以直接copy即可
group = publishedGroupId version = libraryVersioninstall {repositories.mavenInstaller {pom.project {packaging 'aar'groupId publishedGroupIdartifactId artifactname libraryNamedescription libraryDescriptionurl siteUrllicenses {license {name licenseNameurl licenseUrl}}developers {developer {id developerIdname developerNameemail developerEmail}}scm {connection gitUrldeveloperConnection gitUrlurl siteUrl}}} }task sourcesJar(type: Jar) {classifier = 'sources'from android.sourceSets.main.java.srcDirs }task javadoc(type: Javadoc) {source = android.sourceSets.main.java.srcDirsclasspath += project.files(android.getBootClasspath().join(File.pathSeparator)) }task javadocJar(type: Jar, dependsOn: javadoc) {classifier = 'javadoc'from javadoc.destinationDir }artifacts {archives javadocJararchives sourcesJar }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 = bintrayReponame = bintrayNamedesc = libraryDescriptionwebsiteUrl = siteUrlvcsUrl = gitUrllicenses = allLicensesdryRun = falsepublish = trueoverride = falsepublicDownloadNumbers = trueversion {desc = libraryDescription}} } javadoc{options{encoding "UTF-8"charSet "UTF-8"} }3. 新建倉庫
訪問https://bintray.com/ -> login -> view Profile -> add new Repository
注意: 
 這里的倉庫名稱必須和bintrayRepo字段保持一致,此處示例為framework;
因為我們通過腳步實現上傳發布,所以需要用到api key; 
 edit Profile -> api key -> 輸入密碼 
 
因為api key 屬于敏感信息,所以我們存放到本地(local.properties),不要上傳到git上
bintray.user=userName bintray.apikey=your api key4. library的構建、上傳
//clean and install ./gradlew clean install upload ./gradlew bintrayUpload成功后我們登錄官網可以在framework倉庫中看到發布的library了
注意此時該library并沒有發布到jcenter中,還差最后一步
5. 發布到jcenter
進入自己的library(示例中為db) -> Add to JCenter
提交申請后只需要耐心等待審核了,審核通過會顯示如下,這樣你就可以直接引用了
三種引用方式 
 
總結
以上是生活随笔為你收集整理的如何快速将Android库发布到JCenter的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Android如何自定义Gradle插件
- 下一篇: Android-实现一个简单的自动翻译插
