把Android library上传到jCenter和Maven Central

标签: android library jCenter MavenCentral


前言

我们在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仓库
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 175,490评论 5 419
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 74,060评论 2 335
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 124,407评论 0 291
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 47,741评论 0 248
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 56,543评论 3 329
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 43,040评论 1 246
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 34,107评论 3 358
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 32,646评论 0 229
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 36,694评论 1 271
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 32,398评论 2 279
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 33,987评论 1 288
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 30,097评论 3 285
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 35,298评论 3 282
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 27,278评论 0 14
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 28,413评论 1 232
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 38,397评论 2 309
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 38,099评论 2 314

推荐阅读更多精彩内容