貌似大家一般都把开源库放在JCenter上,我曾经也是想把开源项目托管到这个上面的,但是看了各种教程,经历了各种折腾之后。。。失败了。。。于是乎转向了近期比较热门的JitPack,这个配置可以说是超级方便啊,唯一的缺点就是Android Studio默认没有支持JitPack,要引入项目需要的话需要额外添加一句配置,不过这都不是事。好了,废话就说这么多,接下来就是操作:
发布开源库
首先用Android Studio新建一个工程,然后添加需要发布的module。这里需要注意的是,项目的名字必须是你想发布的开源库的名字,module的名字可以随意。这个与JCenter刚好相反,JitPack上显示的是项目的名字,所以项目的名字一定要起好。
在项目的
build.gradle
的dependencies下添加如下代码:1
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
这里的插件版本一定要是最新的,不然可能会出现各种奇怪的问题,如果想知道最新的版本号是多少,可以去这里查看。添加完后,应该是这样的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}然后根据提示进行Sync或者点击
File-->Synchronize
进行同步。在需要发布的module里的build.gradle里的
apply plugin: 'com.android.library'
添加如下两句:1
2apply plugin: 'com.github.dcendents.android-maven'
group='com.github.yongming9011'把
com.github.yongming9011
里的yongming9011
改成自己的用户名,添加完后是这样的:1
2
3apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.yongming9011'然后将项目发布到github,Android Studio提供了一键发布:
VCS-->Import into Version Control-->Share Project on GitHub
,然后输入用户名跟密码就可以一键发布。给项目添加Tag,登录GitHub,打开刚才上传的项目,点击release:
如下:
新建一个tag,输入版本号跟标题:
然后点击
Publish
保存。现在开源库就发布成功了,就这么简单!!!
如何使用
打开JitPack,然后将开源库的github地址复制填进去:
然后点击
Look up
。然后就可以看到我们刚才添加的tag点击
Get it
,然后页面会移动到使用教程的地方关于如何使用,网站上已经说的很清楚了:
第一步:在项目的根目录的
build.gradle
下的allprojects下添加一句maven { url 'https://jitpack.io' }
1
2
3
4
5
6allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
}第二步:在项目的app下的build.gradle的dependencies下添加依赖
compile 'com.github.yongming9011:VerificationCodeView:v1.0'
1
2
3
4
5
6
7
8
9
10dependencies {
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:25.1.0'
compile 'com.github.yongming9011:VerificationCodeView:v1.0'
testCompile 'junit:junit:4.12'
compile project(':library')
}
以上两步,缺一不可。
参考文章:http://blog.csdn.net/qq_26971803/article/details/52181539