Mac Android Studio将项目(library)上传到jcenter

我们经常在gradle引用,却不知道是什么原理,今天跟着代码君,教你开源自己的项目供别人使用,像这样

compile 'com.jzp:DemoJCenterLibrary:1.0.0'

参考文献:鸿洋大神的博客
鸿洋大神写的博客比较简洁干练,可能不适合初级入门者,所以在此进行补充,把我踩过的坑,都写出来,供大家借鉴参考

说明
  1. 上传项目用的插件有两种,gradle-bintray-plugin 和 bintray-release网上有很多资料是介绍gradle-bintray-plugin的,但是小江亲测了一下,gradle-bintray-plugin步骤非常繁琐,踩了很多坑,等把文章写完,在末尾把踩过的坑填一下
  2. 本文主要介绍的是 bintray-release ,这个步骤比较简单,和gradle-bintray-plugin比较,已经可以算是傻瓜式上传啦。
  3. 流程大致是这样的,首先创建自己android studio 的项目,然后上传到bintray仓库,最后上传同步到jCenter仓库,供别人使用自己的库文件,好啦,下面开始划重点啦!
一、新建一个项目,就是要开源的项目
  1. 新建工程(new project)UploadjcenterTest
  2. 新建自己要共享的库(new model)mylibrary
  3. 在库文件里新建一个mToast类,此类的作用是开源自己写的toast提示框
  4. 如果有自己的库要上传,可以省略1,2,3步骤,用自己的库

    这里写图片描述
二、注册bintray.com账号,搭建自己的仓库

为什么要注册这个账号呢,因为jcenter()属于bintray旗下的一个仓库。所以我们要上传jCenter就必须要有bintray账号

  1. 进入https://bintray.com/,注册账号。
  2. 注册完成后,需要邮箱激活;也可以选择第三方登录。

注册成功后进入准备工作,这些后期在配置android studio中的gradle要用到

  1. 获取API key

    这里写图片描述

  2. 记住自己的用户名和密码,后面上传要用

  3. 创建一个组织(organization)和maven项目,这个是必须的,缺少此步骤,后面会报错,先创建出来,后面我再解释

  4. 创建organization——–jcenterupload (随意命名)

    这里写图片描述

  5. 创建maven库—–maven(必须叫maven而且是小写)

    这里写图片描述
    这里写图片描述

  6. 创建成功效果图

    这里写图片描述

三、Android studio gradle的配置,及上传
  1. 在你的项目的build.gradle添加bintray-release的classpath,注意是项目的build.gradle,不是module的,即UploadJcenterTest/build.gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'com.novoda:bintray-release:0.3.4' //添加的
    }
}

这里写图片描述



2. 在Module:mylibrary(你要上传的库)的build.gradle中配置参数

apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'//添加
android { 
//保持不变 
} 
dependencies { 
//保持不变 
} 
//添加
publish {
    userOrg = 'jzplibrary'//bintray.com自己创建的组织名称
    groupId = 'com.jzp'//项目路径
    artifactId = 'toastlibrary'//项目名称
    publishVersion = '1.0.0'//版本号
    desc = 'Oh hi, this is a nice description for a project, right?'//描述,不重要,随便写
    website = 'https://github.com/jiangzepeng/MyModel'//随便写;当然有github地址最好了
}

这里写图片描述

3. 按照上面填写的,最后如果成功我们引入的方式为

compile ‘groupId : artifactId : publishVersion’ //引入原理
compile ‘com.jzp:toastlibrary:1.0.0


4. 以上步骤做完就可以开始上传了,上传的代码,执行在android studio的终端上terminal,

 PbintrayUser--填写bintray.com用户名
 PbintrayKey--填写bintray.com中的API Key

Mac版

./gradlew clean build bintrayUpload -PbintrayUser=jzp -PbintrayKey=xxxxxxxxxxxxx -PdryRun=false

Window版

gradlew clean build bintrayUpload -PbintrayUser=jzp -PbintrayKey=xxxxxxxxxxxxx -PdryRun=false

这里写图片描述
6. 上传成功会提示BUILD SUCCESSFUL

四、bintray.com 加入到jcenter中
  1. 登录到bintray.com,成功的效果图

    这里写图片描述

  2. 把项目添加到jCenter中,点击add to jCenter

    这里写图片描述

  3. add to jcenter

    这里写图片描述

  4. 好了,到此基本算成功了,只需等待bintray的工作人员审核,我试过,大概半天差不多就有结果了,审核后会发送站内消息和邮箱通知你

  5. 审核通过后,可以根据你上传的groupId,访问该网站https://jcenter.bintray.com/加上你的groupId;例如访问https://jcenter.bintray.com/com/jzp/就可以找到自己的库了
  6. 如果只想内部引用的话在项目的gradle中配置自己仓库的url,Maven的url为https://dl.bintray.com/jcenterupload/maven/,然后compile到自己项目中

    compile ‘com.jzp:toastlibrary:1.0.0’

    这里写图片描述
    这里写图片描述

  7. 引入成功的效果图

    这里写图片描述

番外篇

一、bintray-release方式踩过的坑
  1. HTTP/1.1 404 Not Found [message:Repo ‘maven’ was not found]
    原因:注册完bintray.com,未创建jcenteruploadTest组织和maven

    解决方案:执行上面的第二点的第三步骤

    参考文献:http://blog.csdn.net/tmac2000/article/details/53261141

二、gradle-bintray-plugin方式踩过的坑
  1. Error:No service of type Factory available in ProjectScopeServices.

    原因:classpath com.github.dcendents:android-maven-gradle-plugin:1.3版本太低

    解决:更新到1.4.1以上就可以解决问题
    参考文献:http://www.jianshu.com/p/c4f4894ad215

  2. 执行终端命令是,提示command not found,可能没编译进来,重新rebuild一下就好
三、参考文献
  1. 英文文献(很多基本都是参考着两篇)
    1. https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en
    2. https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/
  2. 中文文献
    1. http://www.jianshu.com/p/e01f2005893e(bintray-release手动上传)
    2. http://www.jianshu.com/p/d75759435833(gradle-bintray-plugin方式上传)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农掘金

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值