一行代码引用library——如何将库上传至jcenter
2016-05-15 本文已影响262人
a57ecf3aaaf2
注册Bintray
- 打开Bintray官方网站,注册帐号。网站可能较慢或无法打开,建议.翻.墙.。
![](https://img.haomeiwen.com/i1234624/03231fb963e938f1.png)
-
有github账户的用户,建议直接使用github授权并登录。
使用github登录
-
登录成功后,进入个人中心,点击
Edit
按钮。
点击`Edit`按钮
-
获取API KEY,并记录获取到的API KEY。
![](http://upload-images.jianshu.io/upload_images/1234624-3c19dcd1a756a33e.png)
配置Android Studio
- 在Project的最顶层的build.gradle文件内添加如下依赖:
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
- 在库 Module中的build.gradle中继续添加如下内容:
ext {
bintrayRepo = 'maven' //仓库名
bintrayName = 'switch' //项目名
publishedGroupId = 'com.fynn.switcher' //发布id,一般为包名
artifact = 'switch' //jcenter的ArtifactId:一般是项目名
libraryVersion = '1.0.0' //发布的版本号:每次上传必须不一样
siteUrl = 'https://github.com/ifynn/Switch' //网站地址
gitUrl = 'https://github.com/ifynn/Switch.git' //git地址
libraryName = 'switch' //项目名
libraryDescription = 'A switch for Android' //项目描述
//开发者信息
developerId = 'Fynn'
developerName = 'Fynn'
developerEmail = 'ifynn@outlook.com'
//版权信息
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
apply from: 'https://raw.githubusercontent.com/llxdaxia/GradleScript/master/install_v1.gradle'
apply from: 'https://raw.githubusercontent.com/llxdaxia/GradleScript/master/bintray_v1.gradle'
- 在
local.properties
文件中添加注册的Bintray信息:
bintray.apikey=之前记录的api key
bintray.user=bintray用户名
![](http://upload-images.jianshu.io/upload_images/1234624-0b2e8497cef3a898.png)
执行上传操作
- 在Android Studio中的terminal中输入命令:
./gradlew bintrayUpload
- 等待上传完毕后,进入Bintray并登录即可看到已成功上传:
![](http://upload-images.jianshu.io/upload_images/1234624-4050b03629ed1e9e.png)
- 上传成功后,还需要提交
add to JCenter
的请求,待审核成功后即可使用如下方式引用。
compile 'com.fynn.switcher:switch:1.0.0'
![](http://upload-images.jianshu.io/upload_images/1234624-0f6f1031fd3786c7.png)
可能遇到的问题:
1. Execution failed for task ':appu:bintrayUpload'.
> Could not create package '**': HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
原因:bintray用户名或apikey错误
解决:进入bintray官网查看用户名和apikey,并修改
2.Execution failed for task ':pkg:javadoc'.
> Javadoc generation failed. Generated Javadoc options file (useful for troubleshooting): '.../build/tmp/javadoc/javadoc.options'
原因:若为GBK编码,java源文件中注释不能存在中文
解决:修改编码格式,或者注释中删除中文,或者在项目中添加如下配置,取消javadoc检查:
allprojects {
repositories {
jcenter()
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
3.java.lang.UnsupportedClassVersionError: PR/Sort : Unsupported major.minor version 52.0
原因:jdk运行环境和编译环境不匹配
解决:设置JAVA_HOME环境变量、配置Android Studio的jdk路径为与环境变量配置一致的路径