将自己写的库上传到Jcenter或者Maven提供给自己或者别人使用,在构建项目的时候只要写上一行如下类似的引用代码即可引用自己的库
compile 'com.sus.library:imagelib:1.0.0'
看到这篇文章的同学可能之前已经踩了不少坑,希望下面的介绍可以帮你解惑
如果有什么问题欢迎提出!趁热乎哈!
下面将逐步介绍如何将Library发布到JCenter
具体案例为:ImageLoaderUtil
所有下文涉及到的文件和配置都包含在其中,如果你觉得对你有用,麻烦STAR一下
- 这里选择“For an Open Source Account Sign Up Here”,而非“START YOUR FREE TRIAL”,如果选择 “START YOUR FREE TRIAL”,可能会碰到下面的问题 Bintray link to jcenter missing
- 问题结论就是:”Add To JCenter” is not enabled for Enterprise Trial users. You need to be OSS or Premium organization/user in order to link your packages to JCenter.
填写相关信息,邮箱尽量使用Gmail邮箱地址(国内邮箱有可能无法注册或者注册成功无法收到激活邮件),注册完成之后到你填写的邮箱里面去激活Bintray账号即可
举例:
- First Name : Shuai
- Last Name : Su
- Username : su2008shuai
- Password : xxxxxxxxxxxxxx
- Emai Address : [email protected]
- Select Country : China
点击上图所示的“Add NewRepository”按钮,添加代码仓库,点击后就会跳转到下图的界面
举例:
- Name : me
- Type : Maven
点击右上角个人头像进入到个人信息主界面,点击Edit按钮即可进入到下图所示的界面。点击“API KEY”,输入Bintray本账号的登陆密码,即可查看到本账号的API KEY
- Name : imagelib
- License : Apache-2.0
- Version control : https://github.com/soulrelay/ImageLoaderUtil
在Project的build.gradle添加如下信息:
//用于打包Maven所需文件
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
//用于上传Maven生成的文件到Bintray
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
我的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:2.3.0'
//用于打包Maven所需文件
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
//用于上传Maven生成的文件到Bintray
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
name 'glide-snapshot'
url 'http://oss.sonatype.org/content/repositories/snapshots'
}
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//加载属性文件
Properties properties = new Properties()
File localPropertiesFile = project.file("bintray.properties");
if (localPropertiesFile.exists()) {
properties.load(localPropertiesFile.newDataInputStream())
}
File projectPropertiesFile = project.file("project.properties");
if (projectPropertiesFile.exists()) {
properties.load(projectPropertiesFile.newDataInputStream())
}
//读取属性
def projectRepositoryName = properties.getProperty("project.repositoryName")
def projectName = properties.getProperty("project.name")
def projectGroupId = properties.getProperty("project.groupId")
def projectArtifactId = properties.getProperty("project.artifactId")
def projectVersionName = android.defaultConfig.versionName
def projectPackaging = properties.getProperty("project.packaging")
def projectSiteUrl = properties.getProperty("project.siteUrl")
def projectGitUrl = properties.getProperty("project.gitUrl")
def projectVersionDesc = properties.getProperty("project.versiondesc")
def projectVersionVcsTag = properties.getProperty("project.versionvcstag")
def developerId = properties.getProperty("developer.id")
def developerName = properties.getProperty("developer.name")
def developerEmail = properties.getProperty("developer.email")
def bintrayUser = properties.getProperty("bintray.user")
def bintrayApikey = properties.getProperty("bintray.apiKey")
def bintrayOrganizationId = properties.getProperty("bintray.organizationId");
def javadocName = properties.getProperty("javadoc.name")
group = projectGroupId
// 配置生成POM.xml文件的参数
install {
repositories.mavenInstaller {
pom {
project {
name projectName
groupId projectGroupId
artifactId projectArtifactId
version projectVersionName
packaging projectPackaging
url projectSiteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection projectGitUrl
developerConnection projectGitUrl
url projectSiteUrl
}
}
}
}
}
//生成sources.jar
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))
}
//生成javadoc.jar
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
//javadoc的配置
javadoc {
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version projectVersionName
links "http://docs.oracle.com/javase/7/docs/api"
title javadocName
}
}
bintray {
user = bintrayUser
key = bintrayApikey
configurations = ['archives']
pkg {
//userOrg = bintrayOrganizationId
repo = projectRepositoryName
name = projectName
websiteUrl = projectSiteUrl
vcsUrl = projectGitUrl
licenses = ["Apache-2.0"]
publish = true
version {
name = projectVersionName
desc = projectVersionDesc
vcsTag = projectVersionVcsTag
}
}
}
apply from: "bintrayUpload.gradle"
这里注意会遇到一个奇葩问题:
- Where:
Script ‘/Users/sus/share/ImageLoaderUtil/imagelib/bintrayUpload.gradle’ line: 85
*What went wrong:
A problem occurred evaluating script.
android.compileSdkVersion is missing!
其实就是「 android.compileSdkVersion is missing!」 这个问题很奇葩,需要把
apply from: “bintrayUpload.gradle”这句话放在最下面,如下完整文件信息所示,我碰到这个问题的时候是直接把这句话放在apply plugin: ‘com.android.library’的后面了
完整文件信息:
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion '25.0.0'
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.squareup.okhttp3:okhttp:3.4.2'
compile 'com.github.bumptech.glide:glide:3.8.0-SNAPSHOT'
compile 'com.github.bumptech.glide:okhttp-integration:1.5.0-SNAPSHOT'
}
apply from: "bintrayUpload.gradle"
创建bintray.properties用于配置bintray和开发者信息
#配置bintray账号相关信息
#bintray用户名,不是登陆邮箱,是个人中心右上角显示的名字
bintray.user=su2008shuai
#bintray的ApiKey
bintray.apiKey=xxxxxxxxxxxxx
#bintray的Organization Id
#bintray.organizationId=soulrelay
#配置开发者信息
#昵称
developer.id=sushuai
#姓名
developer.name=sushuai
#邮箱
[email protected]
创建project.properties用于配置项目信息
#project
#仓库名称,就是在bintray官网建立的仓库的名称
project.repositoryName=me
#项目名称
project.name=imagelib
#项目组id
project.groupId=com.sus.library
#项目id,一般同project.name
project.artifactId=imagelib
#打包类型
project.packaging=aar
#项目官方网站地址
project.siteUrl=https://github.com/soulrelay/ImageLoaderUtil
#项目git地址
project.gitUrl=https://github.com/soulrelay/ImageLoaderUtil
#生成的javadoc名称
javadoc.name=imagelib
project.versiondesc = 1.0.0 normal
project.versionvcstag = 1.0.0 tag
gradlew install
gradlew bintrayUpload
期间可能会碰到如下问题
- -bash: gradlew: command not found
解决方案:
gradlew is not in your global path. To execute the ‘clean’ task (or any task for that matter) using the gradle wrapper (gradlew) in your project directory in your terminal, specify the current directory with the ‘./’:
- ./gradlew clean
Running mac, you also have to do “chmod 755 gradlew” on the file before to make it executable.
归结起来的话:
- chmod 755 gradlew
- ./gradlew install
- ./gradlew bintrayUpload
若出现BUILD SUCCESSFUL则说明成功上传到了Bintray(有时候在执行./gradlew bintrayUpload的时候报错,但这时去bintray官网查看它也上传成功了,可能是缓存的问题,可以Invalidate Caches or clean restart一下试试)
进入到Bintray网站,找到刚才上传的项目,点击右下角的“Add To JCenter”按钮
然后填写项目描述点击“Send”提交审核即可(这里可以什么都不干,直接点击Send按钮),如果审核成功,它会给你发送一封站内信(同时你注册的邮箱优也会收到通知,1天之内肯定可以收到通知)通知你。
访问这个链接:https://jcenter.bintray.com/com/sus/library/imagelib/1.0.0/
Could not upload to ‘https://api.bintray.com/content/su2008shuai/me/ImageLoaderUtil/1.0/ImageLoaderUtil/imagelib/1.0/imagelib-1.0-javadoc.jar‘: HTTP/1.1 400 Bad Request [message:Failed to resolve package name]
Error:Could not find ImageLoaderUtil.jar (com.sus.library:ImageLoaderUtil:1.0.1).
Searched in the following locations:
https://jcenter.bintray.com/com/sus/library/ImageLoaderUtil/1.0.1/ImageLoaderUtil-1.0.1.jar
类似上面的问题的原因都是配置不对,bintray.properties和project.properties上的配置一定要和bintray线上的配置一致,否则会报各种找不到xx的问题
- 我的CSDN博客地址:http://blog.csdn.net/s003603u
- 我的GitHub地址:https://github.com/soulrelay
- 我的简书地址:http://www.jianshu.com/u/514ca03bbc17