主要参考自:http://www.2cto.com/kf/201608/532583.html,感谢博主的奉献。
发布到maven中央仓库的话,有两种方式,一种是使用sonatype(https://issues.sonatype.org/),一种是使用bintray(com.bintray.com),都要注册后再进行操作。
两种方式都看了下,通过比较发现,使用sonatype过于复杂了,需要配置的文件有好几个,所以选择使用bintray来上传到中央仓库。
JCenter仓库是由bintray提供并维护,这个仓库是类似Maven中央仓库。只要你能连上Internet,你就可以通过Gradle或者Maven去下载仓库中的依赖包到你自己的项目中。
当然,我们也可以通过Android Studio把aar包发布到JCenter当中,这样就可以让更多的开发者使用我们提供的aar包。
把项目发布到JCenter中需要3步:
1. 注册bintray账户
2. 编写Gradle脚本,把项目发布到你bintray账户下的Maven仓库
3. 同步到JCenter仓库
1、在bintray.com上面注册一个账号,然后登录进去。
2、注册登录完成之后,创建仓库
3、创建仓库
4、Add New Package
5、选择要上传的AndroidStudio项目
6、设置bintray的username和API Key来进行bintray的加密认证
将这些信息写在local.properties文件里。写在local.properties的原因在于,bintray的username和APIKey是敏感的私人信息,应该保管好而不是把它上传到github上,正好把local.properties文件写在.gitignore里面过滤掉,不会影响上传的github项目。
如下:
bintray.user=YOUR_BINTRAY_USERNAME
bintray.apikey=YOUR_BINTRAY_API_KEY
username就是你bintray账号的用户名
apikey在这里查找:
7、配置库module的build.gradle文件
ext {
bintrayRepo = 'maven'
bintrayName = 'circleloadinglibrary'
publishedGroupId = 'com.mrzk.loadingview'
libraryName = 'circleloadinglibrary'
artifact = 'circleloadinglibrary'
libraryDescription = 'a circle loading for android'
siteUrl = 'https://github.com/zhangke3016/CircleLoading'
gitUrl = 'https://github.com/zhangke3016/CircleLoading.git'
libraryVersion = '1.1.0'
developerId = 'mrzk'
developerName = 'zhangke'
developerEmail = '[email protected]'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
我们引用该类库的代码就是:
compile
'com.mrzk.loadingview:circleloadinglibrary:1.1.0'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
dependencies {
classpath
'com.android.tools.build:gradle:2.0.0'
classpath
'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath
"org.jfrog.buildinfo:build-info-extractor-gradle:4.0.0"
classpath
'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
gradlew install
接下来需要把build成功的文件upload到bintray上:
gradlew bintrayUpload
一切顺利之后也会出现:BUILD SUCCESSFUL
注意,这里有坑。我在build过程中,出现javadoc生成失败的问题,在生成javadoc时有检查文档的正确性,但是这里的检查有点过于严格了,像
这种的注释都会导致生成失败,而且由于使用的是Windows系统,默认编码是GBK,所以在上传编译时报了编码的错误问题。
解决方法:
在库项目的build.gradle里添加:
allprojects {
tasks.withType(Javadoc) {
options.addStringOption(
'Xdoclint:none'
,
'-quiet'
)
options.addStringOption(
'encoding'
,
'UTF-8'
)
}
}
11、同步library文件到创建的bintray仓库
点击右下角的Add to JCenter按钮
跳到一个Request to include the package ‘test’ in ‘jcenter’的页面,什么都不用做,直接点击Send按钮就可以了。
大概1个小时左右,jcenter审核通过就会在bintray上收到同意消息提醒。 恭喜你,你的类库上传到jcenter成功了!
至此大家用我写的类库circleloading,仅仅只需要添加一行代码:
compile
'com.mrzk.loadingview:circleloadinglibrary:1.1.0'
===============================================================================================
1.错误:
使用中在项目中的build.gradle中添加这几行代码时,
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.0.0"
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
是会报错的:
那么说明你的classpath'com.github.dcendents:android-maven-gradle-plugin:1.3'版本错了,最新的android studio版本支持1.4.1了!
2.错误:在module中的build.gradle中引入的“apply from:
'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
”
有问题
我看下了这个地址的文件内容,里面有这么一块代码:
这段代码表示,我们还需要进行gpg签名,但是操作方法中并没有提到这个,而在local.properties文件中也没有说到有bintray.gpg.password这个参数。
其实我还是去看来下GPG签名的,但是,哎怎么感觉这么复杂呢!又是要下工具的,又还要另外爬网页生成密钥公钥的,算了,直接把文件内容拷贝到项目中。
在module中新建一个文件uploadv1.gradle,然后在module的build.gradle中进行引入。内容就是
https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle中去掉version{}这块的内容即可。其他的都按自己的参数配置来就好。
3.注意
在配置bintray的帐号和key时,记得不要在用户名和密钥上加''号,我手贱的加上了,又浪费了我一段时间。
如果出现:
Could not create package ‘路径’: HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
的情况,就是你的用户名或者密钥错了!