想不想把自己的库也上传到jcenter,然后只需要一名话 compile com.zzb.library:android-common:0.1.0 //(compile group_id:artifact_id:version)
就可以引用你的库? 按着下面的教程一步一步来就可以了
1.到https://bintray.com/注册一个账号
2.AndroidStudio创建一个项目,然后再在这个项目里创建一个library(File->New->New Module->Android Library)
3.打开项目根目录的gradle.properties文件,复制下面的内容,替换相应字段的值
VERSION_NAME=0.1.0 GROUP=com.zzb.library POM_NAME=jcenter test POM_ARTIFACT_ID=common POM_PACKAGING=aar #比如我们引用appcompat-v7,那么会这么写 compile 'com.android.support:appcompat-v7:22.2.0' #实际对应的字段就是 compile 'GROUP:POM_ARTIFACT_ID:VERSION_NAME' POM_DESCRIPTION=项目描述 #项目地址等,我这使用的是github的项目地址 POM_URL=https://github.com/BaronZ/android-common-library POM_SCM_URL=https://github.com/BaronZ/android-common-library POM_SCM_CONNECTION=scm:git:git://github.com/BaronZ/android-common-library.git POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/BaronZ/android-common-library.git #开源协议 POM_LICENCE_NAME=The Apache Software License, Version 2.0 POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt POM_LICENCE_DIST=repo POM_DEVELOPER_ID=zzb POM_DEVELOPER_NAME=ZZB POM_DEVELOPER_EMAIL=realbo89@outlook.com POM_DEVELOPER_URL=http://baronz.github.io GIT_URL=https://github.com/BaronZ/android-common-library.git ISSUE_URL=https://github.com/BaronZ/android-common-library/issues
4.项目目录下build.gradle,dependencies添加引用
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
}
注,如果不自己写gradle脚本,可跳过步骤5和6
5.项目目录下gradle目录添加文件gradle-jcenter-push.gradle,内容如下
apply plugin: 'maven-publish' apply plugin: 'com.jfrog.bintray' publishing { publications { mavenJava(MavenPublication) { groupId GROUP version VERSION_NAME artifactId POM_ARTIFACT_ID artifact "${project.buildDir}/outputs/aar/${project.name}-release.aar" artifact androidJavadocsJar artifact androidSourcesJar pom.withXml { Node root = asNode() root.appendNode('name', POM_ARTIFACT_ID) root.appendNode('description', POM_DESCRIPTION) root.appendNode('url', POM_URL) def issues = root.appendNode('issueManagement') issues.appendNode('system', 'github') issues.appendNode('url', ISSUE_URL) def scm = root.appendNode('scm') scm.appendNode('url', POM_SCM_URL) scm.appendNode('connection', POM_SCM_CONNECTION) scm.appendNode('developerConnection', POM_SCM_DEV_CONNECTION) def license = root.appendNode('licenses').appendNode('license') license.appendNode('name', POM_LICENCE_NAME) license.appendNode('url', POM_LICENCE_URL) license.appendNode('distribution', POM_LICENCE_DIST) } } } } def getBintrayUserProperty() { return hasProperty('BINTRAY_USER') ? BINTRAY_USER : "" } def getBintrayApiKeyProperty() { return hasProperty('BINTRAY_APIKEY') ? BINTRAY_APIKEY : "" } // gradle bintrayUpload bintray { user = getBintrayUserProperty() key = getBintrayApiKeyProperty() publications = ['mavenJava'] dryRun = false publish = true pkg { repo = 'maven' name = POM_ARTIFACT_ID desc = POM_NAME websiteUrl = POM_URL issueTrackerUrl = ISSUE_URL vcsUrl = GIT_URL licenses = ['Apache-2.0'] labels = ['android', 'aar'] publicDownloadNumbers = true version { name = VERSION_NAME vcsTag = VERSION_NAME } } }
6.项目目录下gradle目录添加文件android-artifacts.gradle,内容如下
task androidJavadocs(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) } task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { classifier = 'javadoc' from androidJavadocs.destinationDir } task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.sourceFiles }
7.在library目录下的build.gradle底部添加下面两行脚本
如果是执行了5和6,用下面脚本
apply from: rootProject.file('gradle/android-artifacts.gradle')
apply from: rootProject.file('gradle/gradle-jcenter-push.gradle')
如果没有5和6,用下面脚本
apply from: 'https://raw.githubusercontent.com/BaronZ/android-common/master/gradle/android-artifacts.gradle'
apply from: 'https://raw.githubusercontent.com/BaronZ/android-common/master/gradle/gradle-jcenter-push.gradle'
8.登录bintray.com创建maven库
- 点击maven
- 点击Add New Package
- 填入相应的信息,再点Create Package即可
- 回到bintray主页,点击edit
- 点击API Key
- 点击show,记下你的API Key,等下要用
9.找到你home目录下的gradle.properties(windows下目录是C:Users你的电脑名称.gradle),添加下面两行脚本
BINTRAY_USER=你的bintray.com的用户名
BINTRAY_APIKEY=上面一步让你记下的API Key
10.好了,敲下面命令,坐等上传成功,cmd->进入你的项目目录(注,执行下面的命令,请确保设置好环境变量,path里面有gradle/bin,有JAVA_HOME环境变量)
gradle clean build bintrayUpload
如果看到执行结果如下,那么恭喜你,还差一步
如果在生成Javadoc的时候遇到'编码GBK的不 可映射字符'错误,打开gradle/android-artifacts.gradle
添加下面代码到androidJavadocs这个任务里面
allprojects { tasks.withType(Javadoc) { options.encoding = "UTF-8" } }
即完整脚本如下
task androidJavadocs(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) allprojects { tasks.withType(Javadoc) { options.encoding = "UTF-8" } } }
11.还需要点add to jcenter,坐等审核通过
12.几个小时后,当你看到下面的截图,恭喜你,可以引用的你库了
13.下面一句话,引用你的库,大功告成
compile com.zzb.library:android-common:0.1.0
14.升级
确保global/gradle.properties里的BINTRAY_USER是正确的
修改library/gradle.properties的VERSION_NAME即可
命令:gradle clean build bintrayUpload