把 Android library 上传到 jCenter 和 Maven Central

3,055 阅读6分钟
原文链接: www.zybuluo.com

前言

我们在Android Studio中使用一些第三方库时(例如:gson),只需要在moduel的build.gradle中添加一条依赖就可以了:

dependencies {
    compile 'com.google.code.gson:gson:2.3.1'
    }

然后Android Studio就会去jCenter仓库或者Maven Central仓库,自动去寻找,并且下载这个第三方库。
而'com.google.code.gson:gson:2.3.1'就是第三方库在仓库中的地址。它的组成部分如下:

com.google.code.gson gson 2.3.1
groupId artifactId version

所以,如果自己也有一些轮子想分享给大家使用,就需要把它传到jCenter或Maven Central中。下面就是上传的具体方法。

申请账号

jCenter和Maven Central是两个标准的Maven仓库,其中jCenter是Google官方默认的中央仓库。它们分别由bintray和sonatype维护。

仓库名称 维护机构 Android Studio中使用
jCenter bintray jcenter()
maven sonatype mavenCentral()

所以,如果想把library传到jCenter和Maven Central,必须要有bintraysonatype的账号。

为library配置插件

1. 配置Gradle Android Maven plugin

  • 在project的build.gradle中添加依赖

    dependencies {
                classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
                }
  • 在library的build.gradle中应用插件

    apply plugin: 'com.github.dcendents.android-maven'

2. 配置Gradle Bintray Plugin

  • 在project的build.gradle中添加依赖

    dependencies {
                classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
                }
  • 在library的build.gradle中的应用插件

    apply plugin: 'com.jfrog.bintray'

3. 配置完以后,project和library的build.gradle应该分别是下面这样

project:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        //Gradle Android Maven plugin
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
        //Gradle Bintray Plugin
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.1'
    }
}

library:

apply plugin: 'com.android.library'
//Gradle Android Maven plugin
apply plugin: 'com.github.dcendents.android-maven'
//Gradle Bintray Plugin
apply plugin: 'com.jfrog.bintray'

配置Gradle Bintray Plugin插件

在第二步中,已经为library配置了Gradle Bintray Plugin插件,但是为了能够使用它,还需要对Gradle Bintray Plugin进行进一步的配置。
对Gradle Bintray Plugin的配置,在library的build.gradle中进行。

1. 为bintray账号设置 API Key

登陆 bintray
Edit Your Profile -> API Key

2. 在project的local.properties中添加bintray的账号信息

bintray.user = bintray_username
bintray.apikey = bintray_api_key

local.properties在工程创建时,就已经被默认加到.gitignore文件中了,不会被误传到仓库,所以通常都用它来保存账号的配置信息。

3. 在library的build.gradle文件中添加bintray的配置信息

  • 添加bintray账号
    从project的local.properties文件读取

    bintray {
                user = properties.getProperty("bintray.user")
                key = properties.getProperty("bintray.apikey")
                ...
                }
  • 配置要上传到Bintray中的package的信息
    以下四个是package的必需项:

    • repo,bintray中的目的仓库名字
      • bintray中默认有九个仓库,且仓库的名字都是固定的,如:maven,npm等
    • name,package的名字,也就是artifactId
    • licenses,package的licenses
    • vcsUrl,package的仓库地址

    下面我的一个library的bintray配置信息:

    bintray {
                ...
                pkg {
                    repo = 'maven'
                    name = 'magicreddot'
                    vcsUrl = 'https://github.com/kanglongba/MagicRedDot.git'
                    licenses = ['Apache-2.0']
                    userOrg = 'bintray_user' //可选项,如果作者属于一个组织,填写组织的名字;否则默认为‘BINTRAY_USER’
                }
                }
  • 配置package的版本信息

    • 必须项
      • name,版本名字
    • 可选项
      • desc,版本描述
      • released,此版本发布的日期。有格式要求:
        • 'yyyy-MM-dd'T'HH:mm:ss.SSSZZ'
        • java.util.Date instance
      • vcsTag,版本控制的tag名字
      • attributes,版本附加的属性信息

    上实例:

    bintray {
                ...
                pkg {
                    ...
                    version {
                        name = '1.0.0-Final‘
                        desc = 'a powerful red dot widget for android'
                        released  = new Date()
                        vcsTag = 'v1.0.0'
                        attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
                    }
                }
                }

4. 定义要上传到Bintray的文件集合

Gradle Bintray Plugin插件提供三种方式,分别是:

三种我都不懂,但是大家都用 Configurations,我也跟风这么写:

bintray {
    ...
    configurations = ['archives']
    }

至此,对Gradle Bintray Plugin插件的配置就完成了。最后附上我的项目-MagicRedDot的bintray配置信息。
在project的local.properties中:

bintray.user = bintray_username
bintray.apikey = bintray_api_kay
bintray.gpg.password = gpg_passphase

在library的build.gradle中:

def siteUrl = 'https://github.com/kanglongba/MagicRedDot'   // 项目的主页
def gitUrl = 'https://github.com/kanglongba/MagicRedDot.git'   // Git仓库的url
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 = "maven"
        name = "magicreddot"    //发布到JCenter上的项目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
        version {
            desc = 'a powerful red dot widget for android'
            gpg {
                sign = true //Determines whether to GPG sign the files. The default is false
                passphrase = properties.getProperty("bintray.gpg.password")
                //Optional. The passphrase for GPG signing'
            }
        }
    }
    }

配置Gradle Android Maven plugin插件

Gradle Bintray Plugin插件用来使Android Studio可以自动接入你在bintray中的指定仓库,Gradle Android Maven plugin的插件用来执行具体的打包、上传工作。它们合力将library发布到bintray仓库中。

对Gradle Android Maven plugin插件的配置全部都在library的build.gradle文件中进行,具体可以参考它的文档
我参考了其他人的配置,觉得主要可分为两个:

  • 对package的配置
    配置groupId和version

    group = 'com.bupt.edison.magicreddot'
            version = '1.0.0'

    命名groupId时,一般由host和library name组成。com.bupt.edison就是我的host,magicreddot就是我的library name。通常情况下,artifactId也以library name命名。

    artifactId也可以在这配置,在project的settings.gradle中添加:

    rootProject.name = 'magicreddot'

    但是,更常见的是在Gradle Bintray Plugin插件中配置。

  • 对任务的配置
    较长,直接贴代码

    install {
                repositories.mavenInstaller {
                    // This generates POM.xml with proper parameters
                    pom {
                        project {
                            packaging 'aar'
                            // Add your description here
                            name 'Android Magic Dot'
                            description 'a powerful red dot widget for android'
                            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 'kanglongba'        //填写bintray或者github的用户名
                                    name 'edison'         //姓名,可以是中文
                                    email 'kanglongba@gmail.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
                }

上传到bintray中的仓库

上面已经把相关的配置都做好了,接下来就可以将library上传到bintray中的仓库了。
两种方式:


  • 在project目录下执行

    ./gradlew install
    ./gradlew bintrayUpload
  • 在project的Android Studio的Gradle面板中,找到名为 bintrayUpload 的task,然后执行。

同步到jCenter中

如果只是上传到自己的bintray仓库中,那么别人想使用的话,必须在project的build.gradle中添加你的仓库地址:

repositories {
    ...
    maven {
        url 'https://dl.bintray.com/kanglongba/maven'
    }
    }

因此,为了更加开源,还需要把library同步到jCener中。jCenter是bintray维护的一个公共仓库。
操作很简单,在library的bintray主页直接点击Add to JCenter按钮,比如MagicRedDot:

MagicRedDot的bintray主页 -> Linked To -> Add to JCenter

接下来会弹出一个页面,什么也不用填,直接 send。
然后等待管理员审核,大概四个小时左右,library就被添加到了jCenter仓库中。这时可以访问jCenter的仓库地址,根据library的groupId和artifactId看看是否添加成功。

至此,就可以在项目中通过添加依赖的方式使用我们的库了:

dependencies {
    ...
    compile 'com.bupt.edison.magicreddot:magicreddot:1.0.0'
    }

从jCenter同步到Maven Central

虽然jCenter是Android默认的中央仓库,但是Maven Central仍有很多使用者,因次最好也将library传到Maven Central仓库中。
由于已经将library上传到了jCenter中,所以可以直接从jCenter同步到Maven Central。幸运的是,同步操作非常简单,比直接上传到Maven Central简单很多。

同步操作可以分为以下四步,其中第二步和第三步只需要操作一次:

1. 获取上传资格

2. 生成签名

3. 关联bintray账号

4. 同步到Maven Central

由于这部分步骤较多,请参考这篇文章的图文部分

Reference

  1. Gradle Android Maven plugin
  2. Gradle Bintray Plugin
  3. 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
  4. 发布library到Maven仓库