Android如何在Jcenter中托管工具库
前言:
当程序接触到一定程度的时候,我们会发现经常用到一些很通用的代码,但是却又在不同的项目、Demo中不停的在重复封装,我们就会觉得做这些事情是一种浪费时间和精力的无用劳动力,那么第三方库的作用就油然而生了,即实现 一库封装,多处公用。
那么我这边暂时只记录关于Jcenter仓库的依赖使用。
平台:Bintray
仓库使用:jcenter()
封装库:Android Library
IDE版本:Android Studio 3.5.3
JDK版本:1.8
Gradle版本:3.4.2
1、在Android项目中,我们New 一个Module用来封装通用的程序,如下图

2、编写发布到Jcenter
在app中的gradle中加入bintray版本:
classpath 'com.novoda:bintray-release:0.9.1'

在Module中的gradle中加入如下内容:
apply plugin: 'com.novoda.bintray-release' publish { // gradlew clean build bintrayUpload -PbintrayUser=用户名(如itsdf07) -PbintrayKey=用户名对应的Key(Bintray平台获取) -PdryRun=false userOrg = 'itsdf07'//bintray.com用户名 groupId = '****'//jcenter上的路径 artifactId = 'lib-net'//项目名称 publishVersion = '1.0.2'//版本号 desc = '版本更新的描述'//描述,不重要 website = '个人网站'//网站,最好有,不重要 }

最后在IDE中的Terminal中执行提交指令:
gradlew clean build bintrayUpload -PbintrayUser=用户名(如itsdf07) -PbintrayKey=用户名对应的Key(Bintray平台获取) -PdryRun=false
备注:当Build时出现如下Sync错误时,这是由于插件兼容问题导致的错误,即为使用的构建工具Gradle与 com.novoda:bintray-release 版本不对应
ERROR: Unable to load class 'org.gradle.api.internal.component.Usage'. Possible causes for this unexpected error include: Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network) The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build processes (requires restart) Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project. In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

如下为Android Studio 、gradle、bintray-release的参考对应关系
AndroidStudio 2.3.3 classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.novoda:bintray-release:0.3.4' AndroidStudio 3.5.3 classpath 'com.android.tools.build:gradle:3.4.2' com.novoda:bintray-release:0.9.1

浙公网安备 33010602011771号