1 前沿
在开发中我们都会用到无私的程序员分享的开源项目,Androidstudio中使用开源也很方便 。例如家喻户晓的Rxjava,只需要一句话compile ‘io.reactivex:rxjava:1.1.6’,就可以添加到项目中,时间久了,是不是自己也想试试开源一下自己的成果,给大家用用,或者成就感。
这时候有两种发布方式,一种是gradle-bintray-plugin,一种bintray-release
,本篇文章将介绍第一种gradle-bintray-plugin。第二种方法,参考:手把手教你将Android项目开源到JCenter两种方式以及挖坑和填坑(二);Android 快速发布开源项目到jcenter。先看一下上传过程图解:
2 开始注册 ![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9dbeffb583354e21feb603d8718e74cd.png)
提示:可通过网易邮箱大师注册google邮箱,例如:如何注册Gmail。
3 创建一个Maven仓库,点击Add New Repository,进入创建页面
4 设置仓库为Public,Name填写为maven,类型Type设置Maven
Default Licenses选择Apache-2.0,Description就是库的描述,自由填写,然后点击Create,稍等几秒钟,就完成创建,然后回到主页,在Add New Repository位置就可以看到创建的maven仓库。
这时候回到Androidstudio进行配置。
5 在project中的build.gradle进行配置
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// 1.添加下面两句,进行配置
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
}
allprojects {
repositories {
jcenter()
}
}
// 2.设置全局版本号
ext {
compileSdkVersion = 25
buildToolsVersion = "25.0.0"
minSdkVersion = 15
targetSdkVersion = 25
}
task clean(type: Delete) {
delete rootProject.buildDir
}
6 在仓库library的build.gradle中进行配置
(1)library中的的build.gradle
apply plugin: 'com.android.library'
android {
// 1.版本都是全局配置的(app/library都这样使用)
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
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.2.0'
testCompile 'junit:junit:4.12'
}
// 2.1 普通项目apply from
apply from: '../maven_push.gradle'
// 2.1 gralde插件apply from
// apply from: '../meven_plugin_push.gradle'
(2.1)project中新建maven_push.gradle
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
group = GROUP
version = VERSION_NAME
// 生成源文件
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 {
pom {
project {
packaging 'aar'
name DESC
url HOME_URL
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'xxxxxxxxxx'
name 'xxxxxxxxxx'
email 'xxxxxxxxxxxxxxxxxxxx'
}
}
scm {
connection GIT_URL
developerConnection GIT_URL
url HOME_URL
}
}
}
}
}
// local.properties忽略上传github
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("MAVEN_USER")
key = properties.getProperty("MAVEN_APIKEY")
configurations = ['archives']
pkg {
// repo值必须要和你创建Maven仓库的时候的名字一样
repo = "maven"
// 发布到JCenter上的项目名字
name = POM_ARTIFACT_NAME
desc = DESC
websiteUrl = HOME_URL
vcsUrl = GIT_URL
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options {
// 避免中文注释乱码
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
(2.2)如果开发的是Gradle插件,在project中新建meven_plugin_push.gradle
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'
group = GROUP
version = VERSION_NAME
// 生成源文件
task sourcesJar(type: Jar) {
from sourceSets.main.allGroovy
classifier = 'sources'
}
// 生成Javadoc文档
task javadocJar(type: Jar, dependsOn: groovydoc) {
from groovydoc.destinationDir
classifier = 'javadoc'
}
// 上传到JCenter所需要的源码文件
artifacts {
archives javadocJar
archives sourcesJar
}
// 配置maven库,生成POM.xml文件
install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
name POM_ARTIFACT_NAME
description DESC
url HOME_URL
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
// 开发者的个人信息
id 'xguan'
name 'chenliguan'
email 'chenliguan8@gmail.com'
}
}
scm {
connection GIT_URL
developerConnection GIT_URL
url HOME_URL
}
}
}
}
}
// local.properties忽略上传github
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("MAVEN_USER")
key = properties.getProperty("MAVEN_APIKEY")
configurations = ['archives']
pkg {
// repo值必须要和你创建Maven仓库的时候的名字一样
repo = "maven"
// 发布到JCenter上的项目名字
name = POM_ARTIFACT_NAME
desc = DESC
websiteUrl = HOME_URL
vcsUrl = GIT_URL
labels = ['gradle', 'plugin', 'time']
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options {
// 避免中文注释乱码
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
(3)project中的gradle.properties
HOME_URL = 'https://github.com/chenliguan/xxxxxxxxxx'
GIT_URL = 'https://github.com/chenliguan/xxxxxxxxxx.git'
(4)library中的gradle.properties
GROUP=com.test.testjcenter
POM_ARTIFACT_NAME=lesscode
VERSION_NAME=1.0.2
DESC='A customizable TestJcenter on Android'
7 project中的local.properties中,添加user和apikey
MAVEN_USER=****
MAVEN_APIKEY=***************************************
(1)这里用到user,是之前注册的用户名,看图:
(3)根据登录密码,申请KEY:
(4)把user和apikey填到上面的local.properties文件中。配置工作就此结束
8 开始提交
(1)打开Androidstudio的Terminal窗口,Windows系统输入gradlew install,Mac加上在前面加上./,回车, 如果成功的话如图:
(2)输入gradlew bintrayUpload,回车
9 这个时候打开maven仓库,已经提交成功了
(1)打开maven仓库
(2)现在需要最后一步,进入项目,右下角有个Add to Jcenter按钮:
(3)点击打开,添加描述信息,直接点击Send,等待JCenter审核
(4)审核快1小时,慢了是一天,审核通过之后,Add to Jcenter按钮会消失,同时会受到消息提示
(5)新版的JCenter改变了位置如下:
10 使用JCenter开源项目
10.1 普通的开源项目
compile 'com.test.testjcenter:lesscode:1.0.2'
10.2 插件
(1)在project的gradle中添加如右配置:
buildscript {
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/xguan/maven" }
}
dependencies {
classpath ("com.codelibs.systrace-plugin:systrace:1.0.1") { changing = true }
}
}
allprojects {
repositories {
google()
jcenter()
}
}
(2)在app的gradle中添加如右配置:
// com.codelibs.systrace-plugin为resources/META-INF/gradle-plugins下的properties文件名称
apply plugin: 'com.codelibs.systrace-plugin'
systrace {
enable = true
baseMethodMapFile = "${project.buildDir}/systrace_output/Debug.methodmap"
blackListFile = "${project.projectDir}/blacklist/blackMethodList.txt"
}
11 学习链接
(1)将开源项目上传到jcenter
一步一步教你将开源项目上传到jcenter(第一种方式)
(2)搭建maven私有仓库Nexus
使用Nexus搭建maven私有仓库
(3)另一种发布开源库方法
以上是 jCenter 和 MavenCentral方式,还有一种是JitPack.io + GitHub,Android 写自己的开源库,发布到 JitPack.io。