文章目录
0.代码开源流程
- 写开源库代码(如:Android Library 模块)。
- 上传开源库到远程版本控制仓库 Github。
- 上传开源库至远程文件服务器(如:JCenter)。
- Github 上添加说明文档(如:README. md)。
- Github 上添加开源协议(如:Apache 协议)。
1. 写开源库代码(如:Android Library 模块)
- 如:自定义一个圆 CircleView
- 具体步骤:
注意:主项目包名和库的包名最好不要一样,同步到 JCenter 可能失败。
2. 上传开源库到远程版本控制仓库 Github
git init // 初始化
git add . // 添加文件到本地仓库暂存区
git commit -m "first commit" // 把文件提交到本地仓库
git remote add origin git@github.com:strugglelin/CircleView.git // 本地仓库与远程仓库关联
git push -u origin master // 本地master分支与远程master分支建立关联,推送本地master分支内容到远程仓库
3. 上传开源库至远程文件服务器(如:JCenter)
1. 代码引用zip文件
- jar 文件:包含 classes.jar。
- aar 文件:classes.jar、res、R.txt 等。专门为 Android 设计的文件类型。
2. 代码引用方式
// zip 包放 lib 文件夹
// build.gradle
dependencies {
implementation files('libs/xxx.jar')
implementation 'com.strugglelin.circleview:CircleView:1.0.0'
}
3. JCenter 简介
- JCenter:一种远程的 Android Library 文件服务器。
- Maven 仓库:中央远程仓库。
- Android 应用:通过 bintray-release 插件上传 Android Library 到 JCenter。
4. 上传开源库至远程文件服务器 JCenter
- 注册 Bintray 账号
1、个人版注册地址:https://bintray.com/signup/oss ;企业版:https://bintray.com/
2、通过 Github 账号注册
注意:Github 绑定的邮箱为网易、QQ 邮箱,创建账号会不通过。可以 foxmail 邮箱。 - Bintray 上创建仓库
- 将要上传的代码放到 Module 文件中
- 同步代码到GitHub
- 配置 bintray-release 插件
1、module 中 gradle 文件配置
2、项目中 gradle 文件配置
注意:bintray-release 的 Github地址 https://github.com/novoda/bintray-release
// 配置1
apply plugin: 'com.novoda.bintray-release' // 添加 bintray 插件
// 配置2:项目发布信息
publish {
userOrg = 'strugglelin' // Bintray用户名
repoName = 'CircleView' // Bintray上创建的仓库名(Repository)
groupId = 'com.strugglelin.circleview' // 依赖名compile 'x:y:z' 中的 x-包名
artifactId = 'CircleView' // 依赖中的 y-项目名
publishVersion = '1.0.0' // 依赖中的 z-版本号
desc = 'a CircleView' // 对该开源库的描述
website = 'https://github.com/strugglelin/CircleView' // VCS地址,即填写项目上传的Github地址
}
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
// 配置1:bintray 插件
classpath 'com.novoda:bintray-release:0.9.1'
}
}
// 配置2:防止中文注释错误
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
allprojects {
tasks.withType(Javadoc) {
options{
encoding "UTF-8"
charSet 'UTF-8'
links "http://docs.oracle.com/javase/7/docs/api"
}
}
}
- 上传开源库到 JCenter 中
1、获取 Bintray 用户名和 Bintray 上 API Key。
2、Androidstudio 的 Terminal 输入以下命令执行。(第一次可能要科学上网)
// Windows 中
gradlew.bat clean build bintrayUpload -PbintrayUser=strugglelin -PbintrayKey=***** -PdryRun=false
// Mac 中
./gradlew clean build bintrayUpload -PbintrayUser=strugglelin -PbintrayKey=***** -PdryRun=false
注意:PbintrayUser=Bintray 用户名,PbintrayKey=Bintray 上 API Key。
注意:项目成功上传到 JCenter,查看相关信息。
- 添加到 JCenter
注意:提交添加到 JCenter 审核,需要等待(半小时-数小时不等),审核通过会 Bintray 私信和 发邮箱通知。通过了就可以通过 添加依赖 引用这个库。
5. 开源库版本更新
- 修改 module 中 gradle 文件配置的版本号。
- 重新执行上传代码命令。
publishVersion = '1.0.1' // 依赖中的 z-版本号
// Windows 中
gradlew.bat clean build bintrayUpload -PbintrayUser=strugglelin -PbintrayKey=***** -PdryRun=false
// Mac 中
./gradlew clean build bintrayUpload -PbintrayUser=strugglelin -PbintrayKey=***** -PdryRun=false
注意:PbintrayUser=Bintray 用户名,PbintrayKey=Bintray 上 API Key。
6. 开源库使用测试
- 查看依赖
- Gradle 添加依赖
dependencies {
implementation 'com.strugglelin.circleview:CircleView:1.0.1'
}
4. Github 添加说明文档
- 作用:介绍开源项目 和 使用方法。
- 具体步骤:
1、本地编写 README.md(中文文档)和 README-en.md(英文文档)。
2、放 项目根目录 推送到 GitHub。
5. Github 添加开源协议
- 作用:制定 开源代码贡献者对源码修改 的规则。
- 具体操作:
1、选择开源协议
2、添加开源协议:GitHub 添加开源协议帮助文档