在安卓开发中,咱们常常会在 build.gradle
中引入各类各样的依赖辅助咱们进行开发,如经常使用的 okhttp 请求 Library:android
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
}
复制代码
若是咱们开发了本身的通用 Library,怎么让别人能够这样使用呢,下面介绍一下怎么将 Android Library 发布到 Jcenter。git
Jcenter是一个 Bintray 维护的 Maven 仓库,咱们能够在 Android 项目根目录的 build.gradle
中设置如下内容使用:github
buildscript {
repositories {
Jcenter()
}
}
allprojects {
repositories {
Jcenter()
}
}
复制代码
在发布以前,先要注册 bintray 帐号。web
注册免费的开源帐户请点击右边白色的文字,不要点击绿色按钮。bash
填写好用户信息、邮箱后激活帐户便可使用。app
若是是我的发布 Library,直接新建仓库便可maven
若是是团队,能够先创建一个组织,在组织下操做。gradle
在建立 Library 以前,须要先建立仓库(Repository),Library 是在仓库之下的。按照以下示例,建立 maven 类型的仓库ui
建立好 maven 仓库后,点击进入 maven 仓库,点击 Add a Package
便可建立 Library,正常填写信息便可,在建立 Library 以前,能够先在 GitHub 上建立源代码仓库,在提交审核时须要。spa
在 Edit Profile 中能够查看到 API Key,这里须要记录 API Key,在后面上传 Library 时会用到。
这里咱们要使用 bintray-release: 上传咱们的 Library。
首要要添加插件,在项目根目录的 build.gradle
中添加 classpath 信息
buildscript {
repositories {
Jcenter()
}
dependencies {
classpath 'com.novoda:bintray-release:<latest-version>'
}
}
复制代码
而后在源代码 build.gradle
中添加配置
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release' // android.library 后
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
...
}
// 设置 publish 信息
publish {
userOrg = 'github' // 组织名称,我的同用户名
groupId = 'com.github.xxx'
artifactId = 'xxx-lib'
publishVersion = '1.0.0'
desc = 'lib desc'
website = 'https://xxx.com'
}
复制代码
在 sync 完成后,在项目根目录运行下面的命令上传 Library 文件
./gradlew clean build bintrayUpload -PbintrayUser=你的用户名 -PbintrayKey=这里填写刚刚保存的API Key -PdryRun=false
复制代码
若是提示 BUILD SUCCESSFUL
即表示上传成功,就能够在你的仓库下看见上传的 Library。
上传完 Library 后,咱们须要把 Library 同步到 Jcenter,方便他人使用。进入 Library 页面,能够在 Actions 选项中找到 Add to Jcenter 选项,点击后,不用填写任何信息,提交,等待管理员审批便可。
最后咱们就能够像文章首部提到的那样使用咱们的 Library 了:
// 添加 Jcenter 源
buildscript {
repositories {
Jcenter()
}
}
allprojects {
repositories {
Jcenter()
}
}
复制代码
// 添加依赖
dependencies {
// 格式:implementation 'groupId:lib:version'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
}
复制代码