AndroidStudio怎么将开源项目发布到jcenter

时间:2022-04-30 09:38:45

AndroidStudio是从哪里下载类库的?

我们在用AndroidStudioAndroid程序的时候经常会看到在build.gradle文件里面添加引用的类库,只需要一行代码。 
例如:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">dependencies {
compile <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.david.gradientuilib:gradientuilibrary:1.0.1'</span>
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

有的时候我会感到好奇,为什么AndroidStudio里面引用类库就只需要一行代码,不像Eclipse里面引用类库需要实际的jar文件才可以,其实AndroidStudio的类库也是需要实际的jar,aar这样的文件的,之所以配置一行代码就能实现引用类库,就在于Gradle可以帮助我们通过相应的配置下载对应的类库文件 
那么问题来了,下载下来的文件去哪里了呢? 
答案就在用户目录目录下面的.gradle文件夹可以找到,Mac和Linux平台也能找到相应的路径 
AndroidStudio怎么将开源项目发布到jcenter

AndroidStudio是从Maven Repository 服务器下载类库的。基本上有jcenterMaven Central这两个服务器用于下载Android的类库。 
在代码里配置如下: 
jcenter仓库

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">allprojects {
repositories {
jcenter()
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

Maven Central仓库

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">allprojects {
repositories {
mavenCentral()
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

一开始AndroidStudio是使用MavenCentral作为默认的远程仓库下载类库的。 
但是MavenCentral有一些问题没有解决。首先是对于开发者不友好,开发者想上传自己的类库到MavenCentral非常困难,所以后来AndroidStudio团队决定把默认远程仓库换成jcenter

jcenter上传下载速度快,而且是最大的Java类库仓库,最最重要的一点是,如果你上传类库到jcenter成功了,那么如果你还想上传类库到MavenCentral,就会变得非常容易,只需要用鼠标bintray网站上点几下就可以了。

怎么从远程仓库pull一个AndroidLibrary下来?

在讲述如何上传类库到jcenter,我们需要先了解我们写的那一行gradle代码的意义。

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">compile <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.david.gradientuilib:gradientuilibrary:1.0.1'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

基本上我们可以从这一行代码可以了解到,引用一个library的代码需要3个部分,即:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">GROUP_ID:ARTIFACT_ID:VERSION</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

在这句代码里面的GROUP_IDcom.david.gradientuilibARTIFACT_IDgradientuilibraryVERSION1.0.1。按照我的理解,GROUP_ID就像是我们写一个项目使用的公司包名一样,表示自己所在的团队或个人,ARTIFACT_ID我认为就是这个类库的名称,用于区分同一个GROUP下不同的类库,最后的VERSION顾名思义就是版本号。

怎么上传你的类库到jcenter仓库?

基本上大致的步骤可以慨括为,在AndroidStudio上准备好要上传的库项目,配置相关gradle代码,然后上传到bintray网站上,最后同步到jcenter仓库里。

AndroidStudio怎么将开源项目发布到jcenter

第一步:在Bintray上创建一个package

步骤如下:

1. 在bintray.com上面注册一个账号,然后登录进去。

bintray可以通过github账号来登录,所以建议最好是有一个github账号,也方便后面的工作。

2.注册登录完成之后,点击Maven

AndroidStudio怎么将开源项目发布到jcenter

3.点击Add New Package

AndroidStudio怎么将开源项目发布到jcenter

4.填写必要信息,然后点击Create Package

AndroidStudio怎么将开源项目发布到jcenter

注意这里的Version control是必填的,所以在上传类库到bintray之前最好是把项目push到github上,这样就会很方便了。

5.Create Package成功之后,就可以看到自己创建的Package了

AndroidStudio怎么将开源项目发布到jcenter

第二步:创建好要上传的AndroidStudio项目

在这里我们将要上传的是Android Library module

AndroidStudio怎么将开源项目发布到jcenter

然后我们需要设置bintray的username和API Key来进行bintray的加密认证,这些信息将写在local.properties文件里。写在local.properties的原因在于,bintray的username和APIKey是敏感的私人信息,自己应该保管好,而不是把它上传到github上,正好把local.properties文件写在.gitignore里面过滤掉不会影响上传的github项目。

代码如下:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">bintray.user=YOUR_BINTRAY_USERNAME
bintray.apikey=YOUR_BINTRAY_API_KEY</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

这里的username就是你bintray账号的用户名,APIKey可以在bintray页面的Edit Profile找到

AndroidStudio怎么将开源项目发布到jcenter

在要上传的库module的build.gradle文件里面添加如下代码:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">apply plugin: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.android.library'</span>

ext {
bintrayRepo = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'maven'</span>
bintrayName = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'GradientUI'</span>

publishedGroupId = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.david.gradientuilib'</span>
libraryName = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'GradientUI'</span>
artifact = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'gradientuilibrary'</span>

libraryDescription = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'The gradient ui that View can change from one background to another background. (Such as the background of ImageView, the color of TextView)'</span>

siteUrl = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'https://github.com/wangdong20/AndroidGradientUI'</span>
gitUrl = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'https://github.com/wangdong20/AndroidGradientUI.git'</span>

libraryVersion = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'1.0.1'</span>

developerId = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'david'</span>
developerName = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'David Wong'</span>
developerEmail = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'wangd1991@gmail.com'</span>

licenseName = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'The Apache Software License, Version 2.0'</span>
licenseUrl = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://www.apache.org/licenses/LICENSE-2.0.txt'</span>
allLicenses = [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Apache-2.0"</span>]
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>

在这里需要把bintrayName写成之前Create Pacakge所填写的package name,publishedGroupId就是我们之前提到的GROUP_IDartifact就是之前提到的ARTIFACT_IDlibraryVersion就是VERSION

如果上传成功的话,那么我们引用该类库的代码就是:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">compile <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.david.gradientuilib:gradientuilibrary:1.0.1'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

最后我们还需要配置bintray upload的相关代码

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">apply <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'</span>
apply <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

这两行代码添加到前面修改过的library module的build.gradle的最后面。

然后是在整个project的build.gradle里面添加最后三行代码。

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">dependencies {
classpath <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.android.tools.build:gradle:1.3.0'</span>

// NOTE: Do <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> place your application dependencies here; they belong
// <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> the individual module build.gradle files
classpath <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'</span>
classpath <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"</span>
classpath <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.github.dcendents:android-maven-gradle-plugin:1.3'</span>
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

最后在AndroidStudio提供的Terminal窗口执行如下命令:

AndroidStudio怎么将开源项目发布到jcenter

<code class="hljs cmake has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">gradlew <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">install</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

如果顺利的话,过几分钟就会出现

<code class="hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">BUILD SUCCESSFUL</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

接下来需要把build成功的文件upload到bintray上

<code class="hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">gradlew bintrayUpload</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

顺利的话,也会出现

<code class="hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">BUILD SUCCESSFUL</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

检查你在bintray创建的package,你会看到对应的版本号 
AndroidStudio怎么将开源项目发布到jcenter

在这里我觉得我有必要提一下,在我执行gradlew bintrayUpload命令的时候遇到的一些问题。

我遇到的问题是在build过程中,出现javadoc生成失败的问题,百度了一下,原因是编码问题,中文的注释对应的编码是GBK编码,上传的时候会出问题,没有找到太好的解决方案。最后我采用了一个简单粗暴的方法,就是把所有有中文注释的地方都换成了英文,改完后顺利地上传成功。

到目前为止,你已经成功地把类库文件上传到bintray上,接下来就是同步到jcenter上了。

第三步:同步library文件到创建的bintray仓库

AndroidStudio怎么将开源项目发布到jcenter
这一步需要做的是最简单的,只需要在如上页面点击右下角的Add to JCenter按钮,可能是我已经上传过了,所以现在没有显示Add to JCenter按钮。

接下来会跳到一个Request to include package GradientUI in jcenter的页面,什么都不用做,直接点击Send按钮就可以了。

大概过个2,3个小时,通过jcenter那边的审核就会在bintray上收到jcenter那边的同意消息提醒。 
至此就要恭喜你,你的类库上传到jcenter成功了!

到这里大家就可以用我写的类库GradientUI,仅仅只需要添加一行代码:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">dependencies {
compile <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.david.gradientuilib:gradientuilibrary:1.0.1'</span>
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

写在最后

GradientUI的介绍GradientUI即我的上一篇博客 
开源地址AndroidGradientUI 
参考文章:How to distribute your own Android library through jCenter from AndroidStudio