0%

Android Library发布到jcenter

Android Library发布到jcenter

Android开发的时候我们经常会写一些公共的library给其他项目调用,将library上传到jcenter就可以在gradle里很方便的引用.也方便其他人使用.

jcenter是一个托管在bintray.com的资源库.我们要把library放到jcenter上就需要在bintray.com上注册账号然后创建package.这一步我就不详说了网上有很多介绍也很简单,我主要介绍一下在Android studio里library的配置,旨在记录一下怕自己以后忘了.

1.添加项目配置信息

在module下建立project.properties文件,内容如下:

1
2
3
4
5
6
7
8
9
10
#project
project.name=rxbus
project.groupId=com.cm
project.artifactId=rxbus
project.packaging=aar
project.siteUrl=https://github.com/ch331917692/RxBus
project.gitUrl=https://github.com/ch331917692/RxBus.git

#javadoc
javadoc.name=rxbus

详解:

  • project.name:项目名称
  • project.groupId:项目组ID,通常情况下如果你的包名为com.example.test,那么项目组ID就是com.example
  • project.artifactId:项目ID,通常情况下如果你的包名为com.example.test,那么项目ID就是test
  • project.packaging:包类型,Android库是aar
  • project.siteUrl:项目官方网站的地址,没有的话就用Github上的地址,例如:https://github.com/ch331917692/RxBus
  • project.gitUrl:项目的Git地址,例如:https://github.com/ch331917692/RxBus.git
  • javadoc.name:生成的javadoc打开后主页显示的名称,通常跟项目名称一样即可
2.配置Bintray账号信息及开发者信息

在module下建立local.properties文件,内容如下:

1
2
3
4
5
6
7
8
#bintray
bintray.user=xxx
bintray.apikey=xxx

#developer
developer.id=CM
developer.name=CM
developer.email=xxx@xxx.com
详解:
  • bintray.user:你的Bintray的用户名
  • bintray.apikey:你的的Bintray的API Key
  • developer.id:通常是你在开源社区的昵称
  • developer.name:你的姓名
  • developer.email:你的邮箱
注意要将local.proerties文件加入忽略列表,以免被提交到Github或其他网站泄露个人信息
3.配置bintrayUpload.gradle

修改你的library module的build.gradle文件,在最后加上apply from: “https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle“,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apply plugin: 'com.android.library'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
minSdkVersion 7
targetSdkVersion 22
versionCode 100
versionName "1.0.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"

也可以将https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle文件下载下来放在module目录下,bintrayUpload.gradle内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

// load properties
Properties properties = new Properties()
File localPropertiesFile = project.file("local.properties");
if(localPropertiesFile.exists()){
properties.load(localPropertiesFile.newDataInputStream())
}
File projectPropertiesFile = project.file("project.properties");
if(projectPropertiesFile.exists()){
properties.load(projectPropertiesFile.newDataInputStream())
}

// read properties
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 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 javadocName = properties.getProperty("javadoc.name")

group = projectGroupId

// This generates POM.xml with proper parameters
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
}
}
}
}
}

// This generates 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))
}

// This generates javadoc.jar
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives javadocJar
archives sourcesJar
}

// javadoc configuration
javadoc {
options{
encoding "UTF-8"
charSet 'UTF-8'
author true
version projectVersionName
links "http://docs.oracle.com/javase/7/docs/api"
title javadocName
}
}

// bintray configuration
bintray {
user = bintrayUser
key = bintrayApikey
configurations = ['archives']
pkg {
repo = "maven"
name = projectName
websiteUrl = projectSiteUrl
vcsUrl = projectGitUrl
licenses = ["Apache-2.0"]
publish = true
}
}

module下build.gradle配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apply plugin: 'com.android.library'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
minSdkVersion 7
targetSdkVersion 22
versionCode 100
versionName "1.0.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

apply from: "bintrayUpload.gradle"
4.执行命令打包并上传到Bintray

打开终端进入项目目录下,执行gradlew bintrayUpload命令即可

看到BUILD SUCCESSFUL表示已经成功了,library已经提交到bintray上了.

5.请求提交你的项目到jcenter

上面第四步是将编译后的文件提交到bintray上,但是还没有发布到jcenter

登入Bintray网站进入maven的package页面点击右下角的Add to JCenter然后send等待审核就OK了,审核通过了我们就可以直接在gradle里使用compile 'om.cm:rxbus:1.0'这种方式使用library了.

参考:http://www.cnblogs.com/shiwei-bai/p/4991636.html

补充:

在第四步的时候遇到两个错误这里贴出来希望对遇到相同问题的童鞋有所帮助

1.Observed package id ‘build-tools;20.0.0’ in inconsistent location ‘E:SDK_JDKandroid-sdkbuild-toolsandroid-4.4W’ (Expected ‘E:SDK_JDKandroid-sdkbuild-tools20.0.0’)

解决办法:

将sdk下build-tools目录里的android-4.4W的名称改为20.0.0即可

2.Observed package id 'system-images;android-19;default;armeabi-v7a' in inconsistent location 'C:Androidsdksystem-imagesandroid-19armeabi-v7a' (Expected 'C:Androidsdksystem-imagesandroid-19defaultarmeabi-v7a')

解决办法:

在sdk下system-imagesandroid-19目录下新建一个default目录然后将armeabi-v7a文件夹移动到default目录即可

3.Could not upload to 'https://*****.pom': HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path '****.pom']

解决办法:

这个问题一般都是你的module的名字和你在project.properties,配置的artifactId不一致导致的,改成一样的即可

 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!