- 前言
开发中,或多或少都会用到无私的程序猿分享的开源项目,Androidstudio中使用开源也很方便
例如家喻户晓的Rxjava,只需要一句话compile 'io.reactivex:rxjava:1.1.6'
,就可以添加到项目中,时间久了,是不是自己也想试试开源一下自己的成果,给大家用用,获得成(zhuang)就(bi)感?
开整吧,尽量详细讲解
- 注册(第一坑)
注册本来很简单,但是在这就有大坑,在查找资料时间发现,很多人直接扔的官网注册地址,但是官网注册默认是组织,有30天试用期,之后要付费。而且发布时候各种问题,折腾大半天,
所以我们需要的网址是:正确注册地址、正确注册地址、正确注册地址
开始注册
注册成功之后
这时候有两种发布方式,一种是gradle-bintray-plugin
,一种bintray-release
- 本篇文章将介绍第一种
gradle-bintray-plugin
1.创建一个Maven仓库,点击Add New Repository
Maven仓库.png2.进入创建页面,可以看图操作
创建页面.png3.设置仓库为Public,Name填写为maven,类型Type设置Maven,Default Licenses选择Apache-2.0,Description就是库的描述,自由填写,然后点击Create,稍等几秒钟,就完成创建,然后回到主页,在Add New Repository位置就可以看到创建的maven仓库
创建成功这时候回到Androidstudio进行配置
4.在Project的build.gradle中,加入Maven和Jfrog Bintray的依赖插件
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:2.0.0-alpha3'
//添加下面两句,进行配置
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://raw.github.com/bmob/bmob-android-sdk/master" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
5.然后在library的build.gradle中进行配置,例如我要开源的项目FloatingActionButton
library的build.gradle完整的build.grale中,
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
//从这里开始配置
//添加这两行
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//项目主页--项目的GitHub地址
def siteUrl = 'https://github.com/dazhaoDai/FloatingActionButton'
//项目的git地址
def gitUrl = 'https://github.com/dazhaoDai/FloatingActionButton.git'
//发布到JCenter上的项目名字,compile引用时的第2部分项目名
def libName = "FloatingActionButton"
//compile引用时的第1部分
group = "com.ddz.materialdesign"
// 版本号,//compile引用时的最后部分项目名,下次更新是只需要更改版本号即可
version = "1.0.1"
//上面配置后上传至JCenter后的编译路径是这样的: compile 'com.ddz.materialdesign:FloatingActionButton:1.0.1'
//生成源文件
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
//生成Javadoc文档
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
//文档打包成jar
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
//拷贝javadoc文件
task copyDoc(type: Copy) {
from "${buildDir}/docs/"
into "docs"
}
//上传到JCenter所需要的源码文件
artifacts {
archives javadocJar
archives sourcesJar
}
// 配置maven库,生成POM.xml文件
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
//项目描述,自由填写
name 'A customizable FloatingActionButton on Android'
url siteUrl
licenses {
license {
//开源协议
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
//开发者的个人信息,根据个人信息填写
id 'ddz'
name 'daidazhao'
email 'daidazhao@gmail.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
//上传到JCenter
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user") //读取 local.properties 文件里面的 bintray.user
key = properties.getProperty("bintray.apikey") //读取 local.properties 文件里面的 bintray.apikey
configurations = ['archives']
pkg {
//这里的repo值必须要和你创建Maven仓库的时候的名字一样,我们前面创建的就是maven
repo = "maven"
//发布到JCenter上的项目名字
name = libName
//项目描述
desc = 'A customizable FloatingActionButton on Android'
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options {
//如果你的项目里面有中文注释的话,必须将格式设置为UTF-8,不然会出现乱码
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
6.这个时候,还差关键一步,就是设置user和apikey
Paste_Image.png在local.properties中,添加
bintray.user=dazhao
bintray.apikey=*****************************************************```
这里用到user,是之前注册的用户名,看图
![user.png](http:https://img.haomeiwen.com/i2789715/405cf6ea1d7be39d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
而apikey,则需要我们去申请,具体步骤,点击Edit Profile,选择API KEY,根据登录密码,申请API KEY,具体流程看图
![点击Edit Profile](http:https://img.haomeiwen.com/i2789715/eccab67c5094bfeb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
根据登录密码,申请KEY
![根据登录密码申请key](http:https://img.haomeiwen.com/i2789715/a10254cf001abebe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
把user和apikey填到上面的local.properties文件中。配置工作就此结束
**开始提交**
7.第一步----打开Androidstudio的Terminal窗口,Windows系统输入```gradlew install```,Mac加上在前面加上./,回车,
如果成功的话会出现
>BUILD SUCCESSFUL
![BUILD SUCCESSFUL.png](http:https://img.haomeiwen.com/i2789715/06d0d83cfa830eca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
8.然后进行第二步,输入```gradlew bintrayUpload``` ,回车
![Paste_Image.png](http:https://img.haomeiwen.com/i2789715/964536cda1b416a8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
看到BUILD SUCCESSFUL,说明提交成功了
![BUILD SUCCESSFUL ](http:https://img.haomeiwen.com/i2789715/c4facaf868d3ba75.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
9.这个时候打开maven仓库,可以看到,已经提交成功了
![提交仓库成功](http:https://img.haomeiwen.com/i2789715/3a409e0cdd544de1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
现在需要最后一步,进入项目,右下角有个Add to Jcenter按钮
![Paste_Image.png](http:https://img.haomeiwen.com/i2789715/3992a713b152c0fe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
10.点击打开,添加描述信息,直接点击Send,等待JCenter审核
![Paste_Image.png](http:https://img.haomeiwen.com/i2789715/b25b40c1efe1b01c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
11.审核等待时间不太长,甚至1个小时左右就成功了,审核通过之后,Add to Jcenter按钮会消失,同时会受到消息提示
![按钮消失](http:https://img.haomeiwen.com/i2789715/59386cb69a01d3ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![提示通过审核.png](http:https://img.haomeiwen.com/i2789715/bb6a0edd6f53f286.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
**至此,通过gradle-bintray-plugin方式,提交开源项目,就完成了。
我们就可以愉快的使用了自己造的轮子了**
compile 'com.ddz.materialdesign:FloatingActionButton:1.0.1'
但是,说好的挖坑和填坑,肯定不会就这样:
**填坑**
- 坑1
> What went wrong:
Execution failed for task ':FloatingActionButton:bintrayUpload'.
> Could not create package 'daidazhao/maven/FloatingActionButton': HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
遇到这种错,提示local.properties中user或者apikey设置错误,先检查user名字和apikey是否设置错误,如果检查无误,极有可能是之前注册使用的官网地址,导致报错
- 坑2
>Could not upload to '[https://xxxxxxx.pom](https://xxxxxxx.pom/)': HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'xxxxxxx.pom']
说明library中rope的值和仓库名字不一致,检查两个值是否一致
![Paste_Image.png](http:https://img.haomeiwen.com/i2789715/4991aa6e8c7a0b3a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![Paste_Image.png](http:https://img.haomeiwen.com/i2789715/c5ecabd880c78976.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 坑3
>* What went wrong:
Execution failed for task ':FloatingActionButton:bintrayUpload'.
> Could not create package 'dazhao/maven/FloatingActionButton': HTTP/1.1 404 Not Found
找不到用户名,说明local.properties中user填写,检查名字是否用的是注册名字
**总结**
整理整个过程
1.注册账号
2.创建maven的Repository
3.在项目根目录的build.gradle中添加bintray插件
4.修改Module中的build.gradle中的配置
5.在local.properties中添加user和apikey的认证
6.在Android Studio的Terminal使用 gradlew install / bintrayUpload上传
7.最后在maven仓库中同步到Jcenter中
8.通过审核,开始使用
参考
>[一步一步教你在JCenter发布开源库](http://www.jianshu.com/p/275fc9d54e4a#)
[android开源库发布到jcenter图文详解与填坑](http://www.jianshu.com/p/0acf9e05b27e)
网友评论
//项目主页--项目的GitHub地址
def siteUrl = 'https://github.com/dazhaoDai/FloatingActionButton'
//项目的git地址
def gitUrl = 'https://github.com/dazhaoDai/FloatingActionButton.git'