[关闭]
@kanglongba 2016-07-27T13:46:22.000000Z 字数 6507 阅读 2255

把Android library上传到jCenter和Maven Central

android library jCenter MavenCentral


前言

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

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

然后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

2. 配置Gradle Bintray Plugin

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的配置信息

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

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

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

  1. bintray {
  2. ...
  3. configurations = ['archives']
  4. }

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

  1. bintray.user = bintray_username
  2. bintray.apikey = bintray_api_kay
  3. bintray.gpg.password = gpg_passphase

在library的build.gradle中:

  1. def siteUrl = 'https://github.com/kanglongba/MagicRedDot' // 项目的主页
  2. def gitUrl = 'https://github.com/kanglongba/MagicRedDot.git' // Git仓库的url
  3. Properties properties = new Properties()
  4. properties.load(project.rootProject.file('local.properties').newDataInputStream())
  5. bintray {
  6. user = properties.getProperty("bintray.user")
  7. key = properties.getProperty("bintray.apikey")
  8. configurations = ['archives']
  9. pkg {
  10. repo = "maven"
  11. name = "magicreddot" //发布到JCenter上的项目名字
  12. websiteUrl = siteUrl
  13. vcsUrl = gitUrl
  14. licenses = ["Apache-2.0"]
  15. publish = true
  16. version {
  17. desc = 'a powerful red dot widget for android'
  18. gpg {
  19. sign = true //Determines whether to GPG sign the files. The default is false
  20. passphrase = properties.getProperty("bintray.gpg.password")
  21. //Optional. The passphrase for GPG signing'
  22. }
  23. }
  24. }
  25. }

配置Gradle Android Maven plugin插件

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

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

上传到bintray中的仓库

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

同步到jCenter中

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

  1. repositories {
  2. ...
  3. maven {
  4. url 'https://dl.bintray.com/kanglongba/maven'
  5. }
  6. }

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

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

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

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

  1. dependencies {
  2. ...
  3. compile 'com.bupt.edison.magicreddot:magicreddot:1.0.0'
  4. }

从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仓库
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注