如果想在AndroidStudio中直接使用别人的项目或者是库,一般的做法是在dependencies中进行compile,而compile的东西就是存放在JCenter或者Maven仓库中的。

在这里我上传到JCenter,因为现在默认是支持JCenter,同时JCenter也兼容Maven

注册Bintray账号

简单注册就好,Bintray传送门, 也可以选择github或者其他方式登陆

生成项目的JavaDoc和source JARs并上传

存放在JCenter仓库中的东西就是JavaDoc和sourceJARs.

1. 在根build.gradle中:
添加后两行classpath就可以了,第一行不用变

1
2
3
4
5
6
7
8
9
10
11
12
13
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
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
}
}

2. 在需要上传的Module下的build.gradle中:
第一行,如果是library就改为com.android.library。
后面两行必须的。

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
apply plugin: 'com.android.application'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.0.0"

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
resourcePrefix "" //随便填

defaultConfig {
applicationId "com.customwidget.lzqwidget"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}

// 下面的可以全部复制,然后更改为自己的就好了
def siteUrl = 'https://github.com/LiZHongquan2013/CustomWidget' // 项目主页
def gitUrl = 'https://github.com/LiZHongquan2013/CustomWidget.git' // 项目git的clone地址
group = "com.customwidget.lzqwidget" // Maven Group ID for the artifact 一般为包名
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
// Add your description here
name 'Android CustomWidget' //项目描述
url siteUrl
// Set your license, 不用修改
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'LiZHongquan2013'
name 'LiZHongquan2013'
email 'JonsTank2013@gmail.com'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
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))
}
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 = "CustomWidget"
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}

3. 在local.properties中:

API Key可以点击自己的头像,选择your profile->点击edit选项->选择API Key

1
2
bintray.user=填写你的Bintray用户名
bintray.apikey=填写你的Bintray API Key

这个文件一般是需要是.gitignore中写入的,避免信息泄露
默认.gitignore中是包含了它,如果没有,就需要手动加入

4. 生成
Rebuild一下,然后 ./gradlew install

5. 上传到Bintray
./gradlew bintrayUpload

上传完成即可在Bintray网站上找到你的Repo,我们需要完成最后一步工作,申请你的Repo添加到JCenter。可以进入这个页面,点击include my package, 输入你的项目名字点击匹配到的项目,然后写一写Comments再send即可,然后就等管理员批准了,我是大概等了40分钟,然后网站上会给你一条通过信息,然后就OK了,大功告成。