快速发布 Android/Java Libs 到 Bintray
本文内容为:如何使用 BintrayRelease 插件快速发布一个 Android/Java Lib 到 Bintray Mavne,并引入 Project 使用的流程。
创建 Bintray 账户及 Maven 仓库
1、打开 Bintray 首页,点击 For an Open Source Account Sign Up Here,快速注册或者用第三方的账户登陆即可

2、个人页面点击 Add New Repository 创建一个 Mavne 仓库

填写仓库信息,仓库名称记住,后面有用

3、获取 Bintray API Key

点击箭头位置复制 API Key 后面有用

新建 lib 项目
1、正常新建一个 Android Project
2、创建一个 Android Lib Module
点击 File > New > New Module…,选择 Android Library > Next

填入你的 Libraray name

配置 bintrayrelease 插件
1、在 Project 的 build.gradle 文件中添加快速发布插件地址
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
// 快速发布插件
// https://github.com/StefMa/bintray-release
classpath "guru.stefma.bintrayrelease:bintrayrelease:1.1.2"
}
}
2、在 Lib Moudle (非 app Moudle 注意区分)的 build.gradle 文件中尾部追加如下内容
// 快速发布插件
apply plugin: "guru.stefma.bintrayrelease"
// lib version
version = "1.0.2"
// 组名,一般同包名
group = "cc.skyrin"
// 组件信息配置
androidArtifact {
// lib 组件 id
artifactId = "crashcatch"
}
// 如上配置之后,将来引用的 path = group:artifactId:version
// 即 'cc.skyrin:crashcatch:1.0.2'
// 发布参数配置
publish {
// bintray 用户名
userOrg = 'skyrin'
// bintray repository 名称,填入在 bintray 创建的仓库名
repoName = 'maven'
// 不执行 --dry-run
dryRun = false
// lib 描述
desc = 'a usefull crash catcher'
// lib 开源地址,不重要
website= 'https://github.com'
}
发布 Lib 到 Bintray Maven
1、打开 Terminal 输入如下命令,参数 PbintrayUser 的值为你的 Bintray 用户名,PbintrayKey 为之前获取的 Bintray API Key
./gradlew clean build bintrayUpload -PbintrayUser=skyrin -PbintrayKey=45c86666666666666666666666666666666666
2、如果顺利的话控制台如下所示,表示提交成功,如果出现异常可参照文章附录异常解决

在项目中引用
1、获取你的 Maven URL 并配置在需要使用的 Project 的 build.gradle 中


allprojects {
repositories {
google()
jcenter()
// set up your bintray maven url
maven{url 'https://dl.bintray.com/skyrin/maven'}
}
}
2、 现在,可以在 Moudle 的 build.gradle 文件中添加依赖引用了
在 Bintray lib overview 中可以找到 maven、gradle、ivy 等配置引用方式

添加引用到 build.gradle
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
...
implementation 'cc.skyrin:crashcatch:1.0.2'
}
发布过程常见错误
1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':crashcatch:bintrayUpload'.
> Could not upload to 'https://api.bintray.com/content/skyrin/maven/crashcatch/1.0.2/cc/skyrin/crashcatch/1.0.2/crashcatch-1.0.2.aar': HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
检查 Bintray API Key 是否正确填入,=
后面不要带空格
1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':crashcatch:bintrayUpload'.
> Could not create package 'skyrin/mavens/crashcatch': HTTP/1.1 404 Not Found [message:Repo 'mavens' was not found]
检查 repoName 值是否和 Bintray Maven 仓库名称一致
1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':crashcatch:bintrayUpload'.
> Could not create package 'skyrincc/maven/crashcatch': HTTP/1.1 404 Not Found [message:Subject 'skyrincc' was not found]
检查 userOrg 值是否和 Bintray 用户名一致
如果上面三项都没问题的话,还是报 404 的错误,那么你可能需要科学上网发布。