手把手图文并茂教你发布Android开源库

来源:互联网 发布:网络大电影宣传发行 编辑:程序博客网 时间:2024/05/17 22:22

转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼,文章链接:
http://blog.csdn.net/hejjunlin/article/details/52452220

经常逛github,总看到别人的readme中写着compile ‘com.xxx:1.0.xxx’,这个已经越来越普及,个人,团人,公司都在用,虽然做android这么长时间了,但是发现自己好像不会,走了好多弯路,几经折腾,这没什么技术含量的玩意,还挺费时的,所以,下定决心,发布一个开源库出来,也就是前面文章中介绍的SuperIndicator类库之实现《仿爱奇艺视频,腾讯视频,搜狐视频首页推荐位轮播图介绍》。Github: https://github.com/hejunlin2013/SuperIndicator
今天的Agenda如下:

  • 主工程下的的build.gradle配置Maven,jcenter服务
  • 创建一个类module
  • 上传类库到jcenter仓库
    • 注册/bintray账号
    • 添加package
    • 添加bintray插件
    • 编译类库
    • 配置local.properties添加Bintray认证
  • Android Studio终端使用命令上传
  • Bintray中同步到Jcenter,发送打包请求
  • 升级类库,再打包过程

背景

AndroidStudio是从Maven Repository 下载类库的。基本上有jcenter和Maven Central这两个服务器用于下载Android的类库。
在工程(注意是主工程下的)中的build.gradle有一段脚本如下:

buildscript {    repositories {        jcenter() //jcenter仓库        mavenCentral() //Maven Central仓库    }    dependencies {        classpath 'com.android.tools.build:gradle:2.1.0'        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()    }}

了解到Google studio团队最先使用MavenCentral作为远程仓库下载我们用的编译类库的。
MavenCentral有一些问题。首先是对于开发者体验不好,种种原因Google studio团队远程仓库换成jcenter。
jcenter有一个打包工厂,每天特别多的类库从此诞生,用于github上各种项目中。
像我们平时在app目录下build.gradle中的dependencies中经常看到的:

compile 'com.hejunlin.superindicatorlibray:superindicatorlibray:1.0.3'

这是什么意思呢?它实际上相当于

GROUP_ID:ARTIFACT_ID:VERSION

在这句代码里面的GROUP_ID是com.hejunlin.superindicatorlibray,ARTIFACT_ID是superindicatorlibray(类库名),VERSION是1.0.3。

创建一个类module

首先在Android Studio中点击File菜单,选择New->New Module,然后在弹出视图中选择Android Library,点击Next后填写库的名称。

这里写图片描述

创建完新的library后就可以写公用部分的类代码了,我这里是写superindicatorlibray,写好后,肯定还得有一个sample之类的测试这个类库,我用的是默认app工程,里面有Activity之类的,接下来就在app工程引用这个类库,在app下的build.gradle中添加依赖:

这里写图片描述

compile project(‘:superindicatorlibray’) //冒号后是你的类库的名字
本地充分测试好后,接着你就上要开始上传jcenter仓库了。

本文出自逆流的鱼,文章链接:http://blog.csdn.net/hejjunlin/article/details/52452220

上传类库到jcenter仓库

那么如何上传你的类库到jcenter仓库?

这里写图片描述

  • 注册Bintray用户,Bintray是一个可以托管Android库的平台,JCenter则由Bintray进行维护的。打开https://bintray.com/,进行注册,然后登录。

这里写图片描述

点击add,然后如图填写:

这里写图片描述

  • 填完后,如出现前面图中那样,有一个maven的仓库,接下来要在这里添加package,

这里写图片描述

点击Add New Package,如图:

这里写图片描述

现在一个空仓库和一个package已经创建好了,接下来要在studio,写脚本配置,上传类库

  • 添加bintray插件

在module中类库下build.gradle中添加如下脚本

apply plugin: 'com.jfrog.bintray'//添加bintray插件apply plugin: 'com.github.dcendents.android-maven'

Bintray在上传库时需要一个pom文件,而这个文件可以让Maven插件自动生成,但你还是需要给出groupId和version的值,将下面这两行代码添加到库module的build.gradle中。

group = 'com.hejunlin.superindicatorlibray'version = '1.0.3'
  • 编译类库
    写过ant脚本的话,知道,ant中都是一个个target,而gadle中是task,所以接下来写几个task,对应分别分别生成Jar,Javadoc和JavadocsJar,将下面三个task代码添加到库module的build.gradle文件即可。
task generateSourcesJar(type: Jar) {    from android.sourceSets.main.java.srcDirs     classifier 'sources'}task generateJavadocs(type: Javadoc) {    source = android.sourceSets.main.java.srcDirs //source指定了源码位置    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))}//这里dependsOn意为仅当generateJavadocs完成后才开始本task,和ant脚本也是对应的。task generateJavadocsJar(type: Jar, dependsOn: generateJavadocs) {             from generateJavadocs.destinationDir    classifier 'javadoc'}

将本地编译过的类库及源码及doc文件上传到Maven仓库中package中,需要将task添加到一个archive中,只需要向库module下的build.gradle中添加如下代码:

artifacts {    archives generateJavaDocsJar    archives generateSourcesJar}

本文出自逆流的鱼,文章链接:http://blog.csdn.net/hejjunlin/article/details/52452220

配置local.properties添加Bintray认证

接下来还要配置local.properties添加Bintray认证,git提交时,也会让你填用户名和密码,那jcenter,如何配置呢?
我们须要本地local.properties文件下配置,如果没有没有就创建一个,放在主工程目录下:

这里写图片描述

内容如下:

bintray.user=你的Bintray注册时的名字bintray.apikey=xxxxxxxxxxxxxxxxxxxxx

接下来说下这个apikey在哪?

这里写图片描述

这里写图片描述

输入密码,就可以得到这个key。接下来回到库module下的build.gradle中添加如下脚本:

Properties properties = new Properties()properties.load(project.rootProject.file('local.properties').newDataInputStream())bintray {    user = properties.getProperty("bintray.user")    key = properties.getProperty("bintray.apikey")    pkg {        repo = 'maven'        name = 'com.hejunlin.superindicatorlibray'        version {            name = '1.0.3-release'            desc = "a superindicatorlibray for viewpager, banner"            vcsTag = '1.0.3'        }        licenses = ['Apache-2.0']        vcsUrl = 'https://github.com/hejunlin2013/SuperIndicator.git'        websiteUrl = 'https://github.com/hejunlin2013/SuperIndicator'    }    configurations = ['archives']}

Android Studio终端使用命令上传

最后我们在Android studio中Terminal命令行中,执行:

这里写图片描述

gradlew install

如果顺利的话,过几分钟就会出现

这里写图片描述

恭喜你,编译成功了,去类module看,多了个build的目录,这里东西一会要上传jcenter:

这里写图片描述

接下来需要把build成功的文件upload到bintray上,同样在Terminal命令行,执行如下命令:

gradlew bintrayUpload

顺利的话,也会出现,和上面的图一样的BUILD SUCCESSFUL

Bintray中同步到Jcenter,发送打包请求

这时检查你在bintray创建的package,你会看到对应的版本号

这里写图片描述

点击进去,如图,首次没有打包时,这里是一个add to jcenter的字样,我这里是打了包的。就变成了下图:

这里写图片描述

点击那个箭头的“add to jcenter”,接下来会跳到一个Request to include package GradientUI in jcenter的页面

这里写图片描述
填下你的groupId,直接send,就会发起一个打包版本的请求,
过几个小时,通过jcenter那边的审核就会在bintray上收到jcenter那边的同意消息提醒。
恭喜你,你的类库上传到jcenter成功了!大家都可以用你的类库了。

升级类库,再打包过程

我们上传完成后,如发现类库中的一个bug,这就涉及到更新问题,那么怎么更新呢?
一句话,修改代码后,改本地build.gradle的版本号,按上面的操作,执行gradlew install,gradlew bintrayUpload,到bintray上点击“
Stage snapshots on oss.jfrog.org”同样发送一个请求,不用几分钟,就可以用了,升级,不像首次提交那样,非常快。马上就你可以更新github上的引用版本号,瞬间心情爽爽哒。

这里写图片描述

其实写这篇文章,是没有一点技术分析的过程,但是,如果当初有人写这么一个图文并茂的教程。我可能不会走那么多弯路,可以更节省我的时间,做更多有意义的事情。如果脚本什么的,没写全,可以参考我的github:SuperIndicator类库:https://github.com/hejunlin2013/SuperIndicator,喜欢可以点star。

第一时间获得博客更新提醒,以及更多android干货,源码分析,欢迎关注我的微信公众号,扫一扫下方二维码或者长按识别二维码,即可关注。

这里写图片描述

如果你觉得好,随手点赞,也是对笔者的肯定,也可以分享此公众号给你更多的人,原创不易

3 0