为什么发布自己项目(aar)到JCenter呢,这个答案显而易见,把自己开发功能库或者插件库制作成aar并且发布上去,这样开发者想使用项目的功能或者插件,就可以通过Android Studio自带的gradle方式来添加aar下载到开发者自己的项目里面,简单地说,简洁,方便调用。还有一方面JCenter兼容maven,Android Studio可以默认使用JCenter了。下面我通过自己发布项目Android 自定义通讯录(仿Ios反弹效果+模糊搜索+查看手机通讯录+拉伸导航条)为例子来讲解下发布项目到JCenter库所需要的步骤。
Bintray账号
需要一个bintray网站的账号,需要有github帐号可以直接登,如果没有就只能注册,bintray传送门!新建项目,在项目中添加所需插件
这边就是我自己项目为例子,在根项目的build.gradle的添加上传到公共的JCenter仓库所需俩个插件
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
,如图:
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'构建module模块build.gradle信息
apply plugin: 'com.android.library'
//添加申请生成插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
def siteUrl = 'https://github.com/WX-JIN/JContact' //项目在github主页地址
def gitUrl = 'https://github.com/WX-JIN/JContact.git' //Git仓库的地址
group = "com.soubw"//发布aar前缀根节点
version = "0.0.1"//发布aar的库版本
//最后生成是compile 'com.soubw:jcontactlib:0.0.1' 就是group + :+module名字 + :+version
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'JContact'//添加项目描述
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 'wx_jin'//设置自己ID
name 'WangXiaojin'//设置自己名字
email '85268837@qq.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
}
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 = "jcontact" //项目在JCenter的名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
}
按配置,修改有中文字既可以了!
配置自己网站的信息
我们上传到jcenter的网站Bintray,需要用户验证,一个网站用户名一个是配置清单的API KEY 这个API KEY是从网站清单获取的,在https://bintray.com/profile/edit 可以看到API KEY一栏,这边需要你再次网站密码,才能看到你API KEY
这俩个配置信息即我们上面配置bintray.user和bintray.apikey,在步骤3的时候,我们获取这个信息是从local.properties这个文件读取的,我们需要在自己项目的local.properties添加
bintray.user=你bintray的用户名
bintray.apikey=你的网站的API KEY
这里为什么要写在local.properties呢,因为一般我们都是使用github上传项目到github,这时github会自动忽略local.properties这个文件上传。或者使用git(可以利用gitignore忽略这个文件到git)执行
在stuido的terminal执行
gradlew install
如果提示成功接着进行上传代码
gradlew bintrayUpload
这边如果出错,一般编码问题,自己修改下就可以了!在网站上提交申请到Jcenter
当bintrayUpload成功之后,到自己网站项目提交到申请
最后差不多几小时,一般都通过申请!
参考文章:
https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/