/BintrayUploadGradle

Used for bintrayUpload repo gradle scripts. Then yon can add your repo in JCenter.

Apache License 2.0Apache-2.0

BintrayUploadGradle

Used for bintrayUpload repo gradle scripts. Then yon can add your repo in JCenter.

Details view my blog: 如何把Android Studio项目发布到**仓库JCenter/第三方仓库JitPack中

Android仓库

简单的普及下关于Android的依赖仓库,有两种分别是JCenterMaven Central其实不管是JCenter还是Maven Central都是Maven库。

JCenter

JCenter是由bintray.com维护,在Android Studio的项目根目录的build.gradle中我们会看到自动帮我们实现的JCenter

buildscript {
    repositories {
        JCenter()
    }
}

Maven Central

当然也可以在build.gradle中定义Maven Central

buildscript {
    repositories {
        mavenCentral()
    }
}

至于在Android Studio中为什么默认使用JCenter原因还是有的,感兴趣的可以自己去查,我要说的一点就是,这里你可以认为JCenterMaven Central的超集,这样就能更好的理解为什么要使用JCenter了。

gradle获取library

这里要了解一下我们看到的依赖所定义的方式,其实是有格式的并不是随便乱写的。其实你只要平常够仔细就能发现他们的格式是一样的。 由GroupIdArtifactIdVersionId组成。例如com.jakewharton:butterknife:6.1.0它的GroupId是com.jakewharton,ArtifactIdbutterknifeVersionId6.1.0。现在看这些依赖是不是更清晰了呢。当我们添加了依赖之后gradle会先去Maven中查找是否有该library如果有就会使用上面定义的形式下载http://JCenter.bintray.com/GroupId/ArtifactId/VersionId

http://JCenter.bintray.com/com/jakewharton/butterknife/6.1.0 通过该链接下载到本地再与我们的项目结合。

下面正式进行实现依赖的实现

使用gradle发布项目到JCenter**仓库

注册bintray

首先要在https://bintray.com 中注册账号,注册都是很简单的就不所说了。 然后回到主页在你的Owned Repositories中看下你是否已经添加了maven。 图 第一次注册的应该没有,所以我们要先New Repository创建maven

创建之后会自动跳转到maven中,你会发现没有package,我们可以Add New Package这种相信都会,我这里要说的是另外一种,我们直接在Android Studio中进行配置然后上传到Bintray

代码中配置

分离成多个Module

为了使别人能更好的使用,我们一般在实现自己的依赖的时候会把实现的该依赖的源码作为一个Module,再把实例代码作为Application Module。所以我们要事先处理好Module,下面是我弄好的示例

添加bintray插件

在分了Module之后,首先在项目的根目录的build.gradledependencies中添加bintray插件

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3

添加bintray认证

找到local.properties文件在其中添加

bintray.user=xxxx
bintray.apikey=xxx

bintray.user是注册的user,至于bintray.apikey在JFrog Bintray中的Your ProfileEdit页面的API Key中能找到。

修改Module中的build.gradle

在其中添加如下示例代码:

ext {
    bintrayRepo = 'maven'
    bintrayName = 'LazyFragment'
    publishedGroupId = 'com.isanwenyu.lazyfragment'
    libraryName = 'LazyFragment'
    artifact = 'lazyfragment'
    libraryDescription = 'Android imitation WeChat lazy loading fragments'
    siteUrl = 'https://github.com/isanwenyu/LazyFragment'
    gitUrl = 'https://github.com/isanwenyu/LazyFragment.git'
    libraryVersion = '1.0.0'
    developerId = 'isanwenyu'
    developerName = 'isanwenyu'
    developerEmail = 'isanwenyu@163.com'
    licenseName = 'The Apache Software License, Version 2.0'
    licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
    allLicenses = ["Apache-2.0"]
}

同时在最后添加两个脚本

apply from: 'https://raw.githubusercontent.com/isanwenyu/BintrayUploadGradle/master/bintray_upload.gradle'

这是我这个依赖的示例。其中bintrayRepo是默认的使用mavenlazyfragment是建立的package namesiteUrl是你的项目地址,我的项目在github上,所以是github项目的地址形式。gitUrlVCS。其他应该没什么问题,相应的改成自己的信息。

这样就构建好了依赖com.isanwenyu.lazyfragment:lazyfragment:1.0.0

上传到bintray

打开Android Studio的终端

  • 编译library文件

在终端输入

./gradlew install

出现BUILD SUCCESSFUL就没问题

  • 上传

在终端输入

./gradlew bintrayUpload

或直接通过gradle面板 依次双击执行build/assemble publishing/bintrayUpload

注意:

在工程根目录下的build.gradle中

allprojects {
 repositories {
     jcenter()
 }
 //解决android support包找不到问题
 tasks.withType(Javadoc).all { enabled = false }
}

一样的出现BUILD SUCCESSFUL就没问题 然后你进入JFrog Bintray进入maven你就会发现其中多了一个package,如果有的话那就表示完美成功。

同步到JCenter

完成了上面的步骤并不代表别人可以直接使用

dependencies {
    compile 'com.isanwenyu.lazyfragment:lazyfragment:1.0.0'
}

还要在工程根目录下的build.gradle中添加

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://dl.bintray.com/isanwenyu/maven/'
        }
    }
}

所以我们要同步到JCenter中,怎么同步呢?别急,只要在你刚刚生成的package点击Add to JCenter即可。

进去之后直接发送就可以,不需要填写什么,直接send就可以了。

最后就是等待了。几个小时之后你会收到考核通过的消息,再返回package就会发现Linked to发生了变化。

现在你也可以通过http://JCenter.bintray.com/com/isanwenyu/LazyFragment/lazyfragment/1.0.0 查看。

同时别人也能使用你的依赖,通过如下简单的配置

dependencies {
    compile 'com.isanwenyu.lazyfragment.lazyfragment:1.0.0'
}

版本更新

对于版本更新,只要更改上面配置的版本号libraryVersion = '1.1.0'改成你要更新的版本,其它不变。再重新上传到bintray

总结

整理上传流程如下:

  • 既然要上传到JCenter上,自然要去https://bintray.com 中注册账号
  • 根据自己的需求创建mavenRepository
  • 把项目分离成Library Module
  • local.properties中添加bintray.userbintray.apikey
  • 修改Module中的build.gradle中的配置
  • Android Studio终端使用./gradlew xxx上传
  • 最后在JFrog Bintray中同步到JCenter

发布到JitPack

登录JitPack,可以直接通过GitHub授权登陆

在编辑框中输入GitHub Reop的网址,完成后点击LookUp

img

### 添加`JitPack`仓库到根目录的`build.gradle`中
添加到`repositories`的尾部:

```
allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}
```

添加引用

```
dependencies {
        compile 'com.github.isanwenyu:LazyFragment:1.0.0'
}
```

注意:

  1. 上传到github的版本需要有一个Release,设置Tag
  2. 也可以直接通过commitid获取

img

引用

  1. 如何使Android Studio项目发布到JCenter中

  2. 发布自己项目让别人可以在dependencies中compile的更简单