|
| 1 | +# Android发布sdk到jcenter |
| 2 | + |
| 3 | +> 当我们写好一个开源项目或者写好一个商用的sdk的时候,我们可能需要将它上传到jcenter这样可以更好的提供给别人或者用户使用。当我们们上传之后,用户便可以在gradle里面通过compile来引用我们的项目了。 |
| 4 | +
|
| 5 | +本文介绍的是通过`bintray-release`这个插件来上传我们的项目。 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### 1. 注册bintray账号 |
| 10 | + |
| 11 | +[注册链接](https://bintray.com/signup/oss),一定要选择这个注册链接,因为这个注册链接是面向个人的,是免费的,如果选择了公司版是需要收费的。注册的过程就很简单了,我们也可以通过github进行第三方登录。 |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +### 2. 在bintray新建一个项目 |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +按照上述两幅图,填好项目描述等内容之后,点击Create就可以了。 |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +### 3. 查看自己的key |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +### 4. 在项目中配置上传所需的内容 |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +首先修改项目的gradle(Project) |
| 42 | + |
| 43 | +```java |
| 44 | +buildscript { |
| 45 | + |
| 46 | + repositories { |
| 47 | + mavenCentral() |
| 48 | + jcenter() |
| 49 | + |
| 50 | + } |
| 51 | + dependencies { |
| 52 | + classpath 'com.android.tools.build:gradle:2.3.0' |
| 53 | + classpath 'com.github.dcendents:android-maven-plugin:1.2' |
| 54 | + // NOTE: Do not place your application dependencies here; they belong |
| 55 | + // in the individual module build.gradle files |
| 56 | + |
| 57 | + //新增内容 |
| 58 | + classpath 'com.novoda:bintray-release:0.3.4' |
| 59 | + //==========新增结束========== |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +allprojects { |
| 64 | + repositories { |
| 65 | + jcenter() |
| 66 | + } |
| 67 | + //新增内容,防止一些注释在编译过程报错 |
| 68 | + tasks.withType(Javadoc) { |
| 69 | + options{ |
| 70 | + encoding "UTF-8" |
| 71 | + charSet 'UTF-8' |
| 72 | + links "http://docs.oracle.com/javase/7/docs/api" |
| 73 | + } |
| 74 | + } |
| 75 | + //==========新增结束========== |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +修改想要上传的module的gradle(Moudle) |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +```java |
| 86 | +apply plugin: 'com.android.library' |
| 87 | +//新增内容 |
| 88 | +apply plugin: 'com.novoda.bintray-release' |
| 89 | +//==========新增结束========== |
| 90 | + |
| 91 | +android { |
| 92 | + compileSdkVersion 25 |
| 93 | + buildToolsVersion "25.0.2" |
| 94 | + |
| 95 | + sourceSets.main { |
| 96 | + jniLibs.srcDir 'jni' |
| 97 | + jni.srcDirs = [] //disable automatic ndk-build call |
| 98 | + } |
| 99 | + |
| 100 | + defaultConfig { |
| 101 | + minSdkVersion 14 |
| 102 | + targetSdkVersion 25 |
| 103 | + versionCode 1 |
| 104 | + versionName "1.0.0" |
| 105 | + multiDexEnabled true |
| 106 | + |
| 107 | + } |
| 108 | + buildTypes { |
| 109 | + release { |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + debug { |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + lintOptions { |
| 120 | + abortOnError false |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +dependencies { |
| 125 | + provided 'com.squareup.okhttp3:okhttp:3.5.0' |
| 126 | + |
| 127 | +} |
| 128 | +//新增内容 |
| 129 | +publish { |
| 130 | + userOrg = 'linsir'//bintray.com用户名 |
| 131 | + repoName = 'linlog' |
| 132 | + groupId = 'com.linsir'//jcenter上的路径 |
| 133 | + artifactId = 'linlog'//项目名称 |
| 134 | + publishVersion = '1.0.0'//版本号 |
| 135 | + desc = 'An android sdk for easy to log and toast.'//描述 |
| 136 | + website = 'https://github.com/linsir6/linLog'//网站,尽量采用同样的格式 |
| 137 | +} |
| 138 | +//==========新增结束========== |
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +### 5. 执行上传命令 |
| 144 | + |
| 145 | +在androidstudio中的Terminal中,找到当前的路径,然后执行以下命令: |
| 146 | + |
| 147 | +```java |
| 148 | +./gradlew clean build bintrayUpload -PbintrayUser=linsir -PbintrayKey=XXX -PdryRun=false |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +上述命令有两个地方需要替换成自己的,``-PbintrayUser=linsir``这个里面的linsir需要替换成自己在bintray上面的用户名,``-PbintrayKey=XXX``这里面的XXX需要替换成我们在``3. 查看自己的key``这步获取到的key,然后按下回车执行命令,当看到build success就完成了。 |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +### 6.add to jcenter |
| 158 | + |
| 159 | +当以上步骤全部完成之后,我们就可以在网站项目的界面中看到了,然后点击Add to Jcenter,然后添加一段描述就可以了。 |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +大概就应该在红色的这个位置,然后就可以等待工作人员的审核了,大概两个小时左右,审核通过之后会有站内信,邮箱内也收到邮件的,然后就可以通过``compile 'com.linsir:linLog:1.0.0``这种形式引用了。 |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +> 我自己也写了一个简单的这样的库,大家如果参考的话可以看一下,[代码地址](https://github.com/linsir6/linLog)。 |
0 commit comments