在android开发的过程中,我们经常使用一些大神或者前辈们打包好的开源库和功能组件,这些开源项目给我们的开发带来了极大的便利,我们通过简单的几行代码就可以为自己的应用添加上一个好用的功能模块,类似这样:

下面我们就亲自动手来发布自己的开源库。
在网上查阅了好多大神的教程,也照着做了,但是都没有成功,因为配置build.gradle那块比较复杂,老是出现各种问题,后来我通过观察其他有名的开源项目发现国外大神已经把大部分的gradle配置操作帮我们做好了,并打包了一个很好用的开源项目,点这里 我们只需要引用就可以了,在此表达下对大神的景仰!
一、首先你得有自己的开源项目
23333,其实暂时没有也没关系,目前主要是来学习怎么发布,随便找一个项目练练手就行。
假设现在你已经有了一个android studio项目。
1.在自己的项目上,右键,新建一个moudle:

注意选择moudle类型是Android Library:

完成了之后你的project下就会多出来一个类似这样的moudle:

名字你可以写你自己的library的名字。然后就可以在这个library中编写你的代码。这里我为了方便,删掉了所有的测试代码,如果你有编写测试的话,可以保留。
二、jFrog bintrary操作
1.开通JFrog
打开jFrog bintrary注册账号,可以用github账号或者google账号直接登录。
2.创建maven仓库
这一步和创建git仓库稍微有些类似。


这里需要注意的一点是仓库名字必须填maven,否则后面上传的时候会有错误,这个有点奇怪,后面有时间再看下。
3.添加package
仓库建好后开始进入你的仓库,添加一个package。


这里弄完了之后,jFrog的配置基本上就弄好了。
三、library发布前的配置
1.首先打开你的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.2.3'//你的gradle版本
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'//添加maven插件
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.novoda:bintray-release:0.4.0'//引入我们前面所说的插件(关键)
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这里有个问题就是gradle版本和maven插件版本的匹配问题,大家要找到和自己的gradle版本匹配的maven插件。
2.编辑library所在的moudle下的build.gradle。
同样打开文件,对应下面的标注修改。
这里为了方便,我删掉了所有的测试模块相关的内容,因为我没有写任何的测试代码,如果你有写测试代码的话,可以保留测试模块。
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.novoda.bintray-release'//添加插件
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 2
versionName "1.0.1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
allprojects {
tasks.withType(Javadoc) {//兼容中文字符
options{
encoding "UTF-8"
charSet 'UTF-8'
links "http://docs.oracle.com/javase/7/docs/api"
}
}
}
publish {
userOrg = 'misakamikoto'//bintray.com 用户名,在你登录jfrog后右上角可以看到,例如我的是misakamikoto
groupId = 'com.mewlxy.commontitlebar'//JCenter上显示的路径 path
artifactId = 'CommonTitleBar'//项目名称 之前填写过的library名称
publishVersion = '1.0.1'//版本号 version code
desc = 'this is a common titlebar for android'//项目描述 description
website = 'https://github.com/lxygithub/CommonTitleBar' //项目网址链接 link
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-compat:25.3.1'
}
3.编译、打包和上传
在com.novoda:bintray的主页,我们可以看到使用说明,非常简单,一句命令就搞定了。
先在你的android studio下方找到Terminal,打开它。
然后输入线面这条命令:
gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false
这里需要填写两个参数,BINTRAY_USERNAME就是我们上面填写的那个jFrog上的username。
BINTRAY_KEY在这里可以看到:


然后在显示的输入框中输入你的密码,就可以看到了。
把上面的命令中的两个参数替换了之后,回车执行就可以了。成功后你会在命令行中看到BUILDED SUCCESS的字样。
从上面的命令我们可以看出一共执行了三条命令,分别是clean、build和bintraryUpload,也就是这条命令把gradlew clean、gradlew build和gradlew bintraryUpload这三个命令合成了一条,如果你只是想执行其中一个操作的话,也可以单独执行命令,只有bintraryUpload命令是需要传username和key参数的。
四、提交到jcenter审核
命令执行成功之后,你的library下面的build目录下的outputs文件夹下会多出几个文件类似这样:

这些就是被上传到jcenter的库文件。
回到jfrog,可以看到我们刚才上传成功的package file:

点击它,进入:


提交完成之后,等待一段时间,审核通过之后(审核通过会有右键提醒),就可以在项目中愉快的使用开源库了。:)
当你的library更新时,就重新使用命令build并上传就可以了。切记要更改版本号!
我的gradle配置,大家可以参考下。
参考资料
http://blog.csdn.net/hejjunlin/article/details/52452220
http://blog.chengdazhi.com/index.php/217
网友评论