关于android发布仓库到外部项目的文章,目前大概为分两类:
一种是发布到bintray、jcenter上,一种是将项目上传到github,然后利用jitpack自动打包,下面简单说说各自的操作方法。
一.发布到bintray、jcenter
1.注册bintray账号
略过
2.获取自己的apikey
略过
3.在bintray上建立maven仓库
略过
4.配置项目的gradle文件
在项目的gradle文件的buildscript/dependencies节点添加以下两行代码:
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
5.配置项目的local.properties文件
sdk.dir=你的sdk路径
# 其实你只需要添加下面两行,第一个填你的用户名,比如我的是yolanda。
bintray.user=yolanda
bintray.apikey=fa************************5a
6.配置要上传的library/module的gradle文件
apply plugin: 'com.android.library'
// 这里添加下面两行代码。
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
resourcePrefix "andserver_res_"
defaultConfig {
// applicationId "com.yanzhenjie.andserver.sample" // 这一行要删除,因为library不允许有applicationId。
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName '1.0.1'
}
}
dependencies {
// 如果你的library有依赖别的jar,这里要把jar依赖进来。
compile fileTree(dir: 'libs', includes: ['*.jar'])
}
// 项目引用的版本号,比如compile 'com.yanzhenjie:andserver:1.0.1'中的1.0.1就是这里配置的。
version = "1.0.1"
// 定义两个链接,下面会用到。
def siteUrl = 'https://github.com/yanzhenjie/AndServer' // 项目主页。
def gitUrl = 'git@github.com:yanzhenjie/AndServer.git' // Git仓库的url。
// 唯一包名,比如compile 'com.yanzhenjie:andserver:1.0.1'中的com.yanzhenjie就是这里配置的。
group = "com.yanzhenjie"
install {
repositories.mavenInstaller {
// 生成pom.xml和参数
pom {
project {
packaging 'aar'
// 项目描述,复制我的话,这里需要修改。
name 'AndServer For Android'// 可选,项目名称。
description 'The Android build the framework of the Http server.'// 可选,项目描述。
url siteUrl // 项目主页,这里是引用上面定义好。
// 软件开源协议,现在一般都是Apache License2.0吧,复制我的,这里不需要修改。
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
//填写开发者基本信息,复制我的,这里需要修改。
developers {
developer {
id 'yanzhenjie' // 开发者的id。
name 'yanzhenjie' // 开发者名字。
email 'smallajax@foxmail.com' // 开发者邮箱。
}
}
// SCM,复制我的,这里不需要修改。
scm {
connection gitUrl // Git仓库地址。
developerConnection gitUrl // Git仓库地址。
url siteUrl // 项目主页。
}
}
}
}
}
// 生成jar包的task,不需要修改。
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
// 生成jarDoc的task,不需要修改。
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
// destinationDir = file("../javadoc/")
failOnError false // 忽略注释语法错误,如果用jdk1.8你的注释写的不规范就编译不过。
}
// 生成javaDoc的jar,不需要修改。
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
// 这里是读取Bintray相关的信息,我们上传项目到github上的时候会把gradle文件传上去,所以不要把帐号密码的信息直接写在这里,写在local.properties中,这里动态读取。
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user") // Bintray的用户名。
key = properties.getProperty("bintray.apikey") // Bintray刚才保存的ApiKey。
configurations = ['archives']
pkg {
repo = "maven" // 上传到maven库。(这里要特别注意,如果写了maven报404错误,请在bintray创建一个仓库,这里填改成你创建的仓库的名字,如何创建请看下图。)
name = "andserver" // 发布到Bintray上的项目名字,这里的名字不是compile 'com.yanzhenjie:andserver:1.0.1'中的andserver。
userOrg = 'bintray_user' // Bintray的用户名,2016年11月更新。
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true // 是否是公开项目。
}
}
7.上传项目到Jcenter
准备工作都做完啦,最后一步就是上传操作了,点击AndroidStudio底部的Terminal,观察下Terminal显示的路径是否是你当前项目的root。
- 这里如果你系统配置了gradle的用户环境,输入
gradle install
,如果没有配置gradle用户环境,输入gradlew install
,如果没有问题,最终你会看到BUILD SUCCESSFUL
。 - 如果你看到了生成javadoc时编译不过,那么要看下在gradle中task javadoc下有没有
failOnError false
这句话,在刚才编写gradle时提示过了。如果加了这句而你的javadoc写的不规范会有警告,你不用鸟它。 - 最后一步,运行
gradle install
后看到BUILD SUCCESSFUL
后,再输入上传命令gradle bintrayUpload
,等一分钟左右就执行完了,会提示SUCCESSFUL
。 - 浏览器https://bintray.com/后会看到你的项目。
8.提交审核
审核通过后,就可以正常引用了
二.利用jitpack.io自动打包
1.配置项目的gradle文件
在项目的gradle文件的buildscript/dependencies节点添加以下代码:
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
2.配置要上传的library/module的gradle文件
apply plugin: 'com.github.dcendents.android-maven' group='com.github.dessmann'
3.将项目上传到github
上传项目时,需要保证项目的gradle文件夹与gradlew、gradlew.bat也同步上传,不然jitpack可能无法打包
4.利用jitpack自动打包
在jitpack.io主页上粘贴github版本库的项目路径,执行发布,需要注意的是,如果是私有项目,需要在jitpack.io主页上切换到private,然后在网页底端找到授权的按钮,生成授权码,在下面引用的时候,如果引用的是私有项目,需要用到该授权码
5.在其他项目上引用
allprojects {
repositories {
maven {
url 'https://jitpack.io'
credentials { username authToken }//如果是私有项目,需要添加该行以供jitpack验证
}
google()
jcenter()
}
}
其中authToken是之前授权码