Android Studio 快速发布开源项目到jCenter仓库之一

Android Studio 快速发布开源项目到jCenter仓库之一

在进行Android开发的时候,需要在build.gradle中依赖第三方项目,只需要在dependencies中加入依赖版本就可以将远程仓库中的依赖包下载到本地,相比eclipse先下载再引入来说方便了很多,也显得很高(能)大(装)上(B)。那我们自己怎么上传开源项目到jcenter呢,网上有很多写的很好的博文,我也参考了很多大佬的博客,谨以此文进行记录。

一、创建Android library

这个大家应该都会就不多说了。

二、注册bintray账号

为什么要注册这个账号呢,因为jcenter()属于bintray旗下的一个仓库。网址:https://bintray.com/signup/oss。如果从官网直接进去的记得选择注册开源账户而不是免费账户,前面网址我已经选择了开源账户。至于为什么呢,一些大神说是会有些坑,我没有测试过。
注册最好用谷歌邮箱,国内邮箱不支持(反正我用腾讯和网易不支持),需要科学上网。登陆后,你可以点击昵称旁的箭头->Edit Profile->API Key验证密码后可以看到给你分配的Key。

三、引入bintray-release

在你的项目的根build.gradle添加bintray-release的classpath,注意不是module的。如果版本不匹配Android studio会对代码进行标黄提示,按提示更改即可。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath 'com.novoda:bintray-release:0.8.0'//添加
    }
}
配置待上传moudle的build.gralde,添加以下代码:
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'//添加

//添加,其他不变
publish {
    userOrg = 'lxguo'//bintray.com用户名
    groupId = 'com.lxguo'//jcenter上的路径
    artifactId = 'mylibrary'//项目名称
    publishVersion = '1.0.0'//版本号
    desc = 'this is for test'//描述,不重要,可不填
    website = 'https://github.com/mdk119/mylibrary'//网站,不填老报错
}

按照上面编写即可,细节有标识注释了,假设按照上述的编写,最终引入的方式为:

implementation 'com.lxguo:mylibrary:1.0.0

四、上传

点击Android studio的terminal输入

gradlew clean build bintrayUpload -PbintrayUser=xxx -PbintrayKey=xxxxxxxxxxxxxxxxxxxxxx -PdryRun=false


注:user就是用户名(user name),key就是我们刚才的Api key,dryRun是一个配置参数,当为true的时候,会运行所有的环节,但是不会上传。
当看到BUILD SUCCESSFUL就上传成功了。

BUILD SUCCESSFUL in 1m 54s

你可以访问https://bintray.com/你的用户名/maven,即可看到你上传的项目。上传的过程中会碰到一些错误,大家可以百度一下,这里我要说一点就是404的错:

* What went wrong:
Execution failed for task ':dashview:bintrayUpload'.
> Could not create package 'smalllee/maven/dashview': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]

根据其他大佬的说法,有的说是没有注册开源账户,有的说是仓库名称必须指定为maven,但是我都改了之后还是报错。后来再module中配置了website网址后终于上传成功了。

看到我们上传的项目了,你可以点击进去看该库的一些信息,但是注意此时还不能够直接被引用。按照下图,点击Add To jcenter,发送项目描述,等待人工审核即可。

如果审核通过则可以直接通过implementation引用,否则就要在项目的根目录下配置你创建的maven地址进行引用,如下图:

allprojects {
    repositories {
        google()
        jcenter()
        maven{url'https://dl.bintray.com/lxguo/maven'}//审核不通过添加
    }
}

module下的build.gradle引入,即可以实现上传项目的引用:

dependencies {
   ......
    implementation 'com.lxguo:mylibrary:1.0.0'
}

参考大神链接:

https://blog.csdn.net/lmj623565791/article/details/51148825
https://blog.csdn.net/u013231041/article/details/70174354

猜你喜欢

转载自blog.csdn.net/qq_34500783/article/details/80654620