如何将自己的工程分享到jcenter
最近公司的项目开始使用Android Studio开发,发现使用maven的库构建项目确实很方便也不用下载太多的库了,版本控制也容易了许多。
于是决定把自己这两年来积累的库分享出来。首先在网上搜了一下教程。就找到了这篇文章http://www.jcodecraeer.com/a/anzhuokaifa/Android_Studio/2015/0227/2502.html
然后按照里面的步骤一步步来。
首先要在https://bintray.com/ 申请一个账号,之后点击自己的头像进入自己的主页
然后点开Edit
在点击API Key之后,点击箭头所示的位置,会自动复制api key
打开Android Studio 项目中最外层的local.properties 将以下内容填入
sdk.dir是默认的不用修改,user是你在网站上注册的名字 apikey就是刚刚复制的内容,粘贴即可
然后在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:1.1.2'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
然后在你要分享的module的build.gradle中添加以下两个插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
然后在项目的最下方 添加以下内容
def siteUrl = 'https://github.com/linwoain/lin_library' // 项目的主页
def gitUrl = 'https://github.com/linwoain/lin_library.git' // Git仓库的url
group = "com.linwoain.library"
// 项目的包名install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
name 'Android Utils' //项目描述
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 'linwoain' //填写的一些基本信息
name 'linwoain'
email 'lin@linwoain.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
options.encoding = 'UTF-8'
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 = "Lin_library" //发布到JCenter上的项目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
之后rebuild以下。网上的教程都是直接使用根目录下的gradlew.bat来执行接下来的命令以至于要重新下载一遍gradle工具。其实如果是使用Android Studio的话默认已经有了gradle工具。一般就在Android Studio的安装目录
其实就像java一样,将java的程序所在文件添加到环境变量即可直接使用gradle命令。我的gradle目录为 D:\android-studio\gradle\gradle-2.2.1
新建一个环境变量GRADLE_HOME 值为其目录,并在path的末尾添加一句 ;%GRADLE_HOME%\bin
打开cmd并进入工程目录或者在Android Studio的下直接打开terminal
首先执行gradle install命令,如果javadoc生成无误,就可以执行gradle bintrayupload提交。
提交完成后就可以在网站上看到你提交的内容。然后到
点击include my package 查询到自己提交的包。点击后提交comment。然后等待通过即可。若未通过会发信息提示你。
最后讲一讲我遇到的问题,刚开始的时候一直报错
失败原因Cannot call getBootClasspath() before setTargetInfo() is called
在网上找到的答案是gradle插件版本太低,请教了以为大神并在他的github
上看到了差别 classpath 'com.android.tools.build:gradle:1.1.2' 我当时的版本号为1.1.0所以出错。据说是1.1.0的bug。
然后在rebuild的时候一直报错peer not authenticated 试了各种办法无法解决。没想到第二天就可以正常使用了。可能是当时服务器问题。
最后遇到了最坑爹的问题 。因为要提交库的话必须包含javadoc与javasource。因为javadoc默认以gbk编码,而我的项目是以utf8编码,所以一直报gbk无法映射的问题。最后在https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/ 这里找到了解决办法。在 javadoc中添加一句
options.encoding = 'UTF-8' (上面以修改,若你是以gbk编码,请删除此句)
然后就是巨大的工程,写的注释不标准是通不过的,以前写的方法注释中有未写明参数含义或者返回值都会报错。还有自己写的自关闭的标签(如单独的<br />)。要不就不写注释要不就必须写标准。耗费了大白天的时间修改。最终全部修改完成。然后才提交成功。
最后附上我的可以提交成功的github上的项目,可以略作参考: