目的

这篇文章介绍通过Gradle把开源项目发布到公共仓库JCenter中,发布自己的android library(也就是aar)到公共的jcenter仓库。

为什么选择JCenter,因为JCenter现在是Android Studio中repositories的默认节点了,之前是Maven的,不过JCenter是兼容Maven的。

环境

Android Studio 2.2.3

参考资料

http://www.jianshu.com/p/c4f4894ad215

http://www.jianshu.com/p/6a6eca8c24c4

http://www.cnblogs.com/qianxudetianxia/p/4322331.html

申请Bintray账号

bintray上注册一个账号 建议注册个人账号

可以使用github账号直接授权登陆。

很重要,由于 Bintray网站 改版了,增加了Organization的概念。如果点击中间那个大大的绿色按钮,用那个点了注册就错了!! 变成了注册一个组织,注册地址是 https://bintray.com/signup 提交aar的时候就会出现Unauthorized这个错误 注意:个人的注册地址为https://bintray.com/signup/oss

天大的坑!一开始就进入了上面的坑到最后提交上传aar的时候一直上传不上去!!!,查阅相关资料后才爬出坑....得注册个人号 组织号如何提交待研究

记录API Key

注册后,在https://bintray.com/profile/edit 中查看,首次查看的时候提示需要输入登录密码确认。

创建maven仓库

配置build.gradle

配置插件上传到bintray需要的插件

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

插件版本过低可能遇到如下的异常

然后在你需要发布的那个module(即是library aar)的build.gradle里配置如下内容:

1、配置插件
// 根节点添加
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
2、配置版本
// 根节点添加
version = "0.0.1"
3、定义相关网站
// 根节点添加
def siteUrl = 'https://github.com/Javen205/JPay' // project homepage
def gitUrl = 'https://github.com/Javen205/JPay.git' // project git
4、定义Group

举个例子,当我们引用retrofit的时候是这样的:

compile 'com.squareup.retrofit2:retrofit:2.1.0

引号内字符串以冒号分割为三部分,第一部分就是group,第二部分是name, 第三部分是上面定义的version。

// 根节点添加
group = "com.javen205.jpay"

上传到jcenter至少需要四个文件,除了打包的aar之外,还需要pom和javadoc,source,否则是通不过jcenter的审核。这些我们都可以用脚本生成。

5、打包javadocjar和sourcejar

这也是上传到jcenter必须要的。

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
}
6、定义pom并打包aar
// 根节点添加
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'JPay 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 'javen205'
name 'javen.zhou'
email 'javen205@126.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
7、上传到Jcenter仓库

上传到jcenter的网站Bintray,需要用户验证:

bintrayUser=your_user_name 我这里为javendev
bintrayApiKey=your_apikey 就是在上面记录的API Key

这个属于个人隐私,一般不传,所以需要在记录到项目下的local.properties中(利用gitignore忽略这个文件到git),然后脚本再从local.properties中读取这两个值。

//配置bintray参数
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintrayUser")
key = properties.getProperty("bintrayApiKey")
configurations = ['archives']
pkg {
repo = "maven" //跟上面创建的Maven仓库名字保持一致
name = "JPay" //发布到JCenter上的项目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}

准备工作终于都做完了,我们开始使用插件自动上传aar到bintray。如果一切顺利的话就可以完成上传

谁便填写一个repoName

不填写repoName

重复提交

将库提交到JCenter,点击右侧的Add to JCenter

出现这个错误的原因是执行脚本时并没有默认生成pom-default.xml以及相关的文档和jar,添加到JCenter又必须需要,以下是解决方案

可以在Terminal中执行如下命令如果成功了就会在Lib工程build目录中自动生成docs libs

 ./gradlew clean build bintrayUpload

可能会遇到的错误

Fix the issues identified by lint, or add the following to your build script...

以上问题解决了,我们在maven中删除之前上传的,再次上传arr到bintray,成功之后再点击右侧的Add to JCenter

然后直接send就行(提交JCenter后groupID和在本地定义的一样,所以本地定义groupID要能标识个人,最好到 https://jcenter.bintray.com 看下有没有重复的包名

最后耐心等待Bintray审核通过。

其实我们的aar 已近上传到的Maven中我可以可以直接引用,上传到JCenter 就不再需要定义自己maven仓库地址,直接compile即可。

引用自己的库

1、Maven方式引用自己的库

只要在root下的build加上自己maven地址

maven{
url "https://dl.bintray.com/javendev/maven"
}

然后在app的build中加上引用即可

compile 'com.javen205.jpay:jpaylib:0.0.1'
2、compile方式引用自己的库

如果上面添加到JCenter审核通过,就可以直接在app的build中加上引用即可

compile 'com.javen205.jpay:jpaylib:0.0.1'

如果没有审核通过就添加引用当然就会存在引用异常,如下图

安利时间

JPay是对微信App支付和支付宝支付App支付的SDK进行二次封装,对外提供一个较为简单的接口和支付结果回调

[![License](https://img.shields.io/badge/license-Apache%202-green.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Download](https://api.bintray.com/packages/javendev/maven/JPay/images/download.svg)](https://dl.bintray.com/javendev/maven/JPay/_latestVersion)

推荐阅读

Android依赖管理与私服搭建

Android版-支付宝APP支付

Android版-微信APP支付

支付宝Wap支付你了解多少?

一张二维码集成微信、支付宝支付

如遇到问题欢迎留言交流

Android Studio 上传aar(Library)到JCenter的更多相关文章

  1. Android Studio 上传GitHub项目失败后的一些问题

    在Android Studio上传项目到GitHub时候多上传了了一些项目,想删除,但是报诸如 Remote project is already on GitHub 一些乱七八糟的问题,而且,提示p ...

  2. Android Studio 上传本地项目到 GitHub 上

    •准备工作 注册 GitHub 账号 [GitHub官网] [视频教程] 安装 Git [官方链接] [极速下载链接] 创建本地代码仓库 在桌面上,鼠标右击,选择  Git Bash Here : 接 ...

  3. 通过android studio上传项目到github

    第一步,下载git客户端,并且安装 下载地址:https://git-for-windows.github.io/ 第二步,在android studio中配置git(注意第一张图中的C:\Progr ...

  4. 在GitHub上删除项目后,在Android Studio上传项目依然提示project is already on github

    描述: 在GitHub上面上传项目,但是感觉有些问题,就想删除了重新上传. 但是在Android Studio重新上传项目时,遇到了问题,一直提示“project is already on gith ...

  5. Android Studio上传代码到Coding.net

    1.官方帮助文档:https://coding.net/help/doc/git/import-from-local.html 2.简单点: https://git.coding.net/javaka ...

  6. Android Studio上传项目到GitHub出错

    上传代码到Github出错: 一.github push文件过大(超过50M会有警告,超出100M就会被限制) error: GH001: Large files detected. this exc ...

  7. android studio上传项目到github报错Successfully created project 'Demo' on GitHub, but initial commit failed:

    今天博主正在愉快地学习在AndroidStudio中使用Git,结果报了下面这个错∑(っ°Д°;)っ: Can't finish GitHub sharing process Successfully ...

  8. Android 上传开源项目到 jcenter 实战踩坑之路

    本文微信公众号「AndroidTraveler」首发. 背景 其实 Android 上传开源项目到 jcenter 并不是一件新鲜事,网上也有很多文章. 包括我本人在将开源项目上传到 jcenter ...

  9. (转载) Android studio如何生成aar包

    Android studio如何生成aar包 标签: Android studio如何生成aaAndroid studio aarAndroid 如何生成aar包 2016-12-21 14:42 1 ...

随机推荐

  1. OpenOCD 0.9.0 release

    OpenOCD 0.9.0 release May 18th, 2015 I’m happy to announce the release of OpenOCD version 0.9.0, fin ...

  2. USB组合设备 Interface Association Descriptor (IAD)

    Communication Device Class,简称CDCUSB Compound Device,USB复合设备USB Composite Device,USB组合设备 摘要USB复合设备 Co ...

  3. ICO如此疯狂为哪般?

    编者语: 独角兽一词起源于硅谷,是投资行业,尤其是风险投资业的术语,指的是那些估值超过十亿美元的创业公司.独角兽凤毛麟角,占创业公司总数的0.1%都不到.鑫根资本认为,一个独角兽能达到如此估值,肯定是 ...

  4. JVM菜鸟进阶高手之路

    http://www.jianshu.com/u/3def157aab07?utm_medium=note-author-link&utm_source=mobile

  5. CF 427D Match & Catch 求最短唯一连续LCS

    题目来源:CF 427D Match & Catch 题意:给出2个字符串 求最短的连续的公共字符串 而且该字符串在原串中仅仅出现一次 思路:把2个字符串合并起来求height 后缀数组hei ...

  6. struct对象可能分配在托管堆上吗

    struct对象可能被分配在托管堆上吗? --会的. 比如当对struct装箱的时候,就会被分配在托管堆上. 比如,让一个struct实现一个接口. public interface IReport ...

  7. CentOS MongoDB 高可用实战

    原文:https://www.sunjianhua.cn/archives/centos-mongodb.html 一.MongoDB 单节点 1.1.Windows 版安装 1.1.1 获取社区版本 ...

  8. SQL 参考

    本主题将介绍 ArcGIS 中的选择表达式所用的常规查询的各个元素.ArcGIS 中的查询表达式使用常规 SQL 语法. 警告: SQL 语法不适用于使用字段计算器计算字段. 字段 在 SQL 表达式 ...

  9. win7 64位系统及开发环境重装后的总结

    前言 话说来这家公司之后就一直使用这个系统,现在感觉这系统跑的实在是有点慢了,运行,调试各种浪费时间呀,不过也用了将近20个月了,这也可以说是我用的最久的一个系统了.由于新项目即将拉开战幕,所以自己趁 ...

  10. 开源项目MultiChoiceAdapter详解(四)——MultiChoiceBaseAdapter的使用

    MultiChoiceBaseAdapter是一个可以多选的BaseAdapter,使用的方式相比来说扩展性更强! 使用方式: 1.布局文件 2.写一个类继承MultiChoiceBaseAdapte ...