前言:android studio中把项目的lib库提交到jcenter仓库中,需要使用到bintray,bintray是jcenter的提供商,他支持上传lib到多个平台,jcenter只是众多平台中的一个,形象的说jcenter是位于某地的仓库,bintray是送货的卡车,你写的库就是货了。
第一部分:在bintray上注册账号,并创建package。
注册bintray ,注意:注册时尽量使用国外的邮箱,避免接收不到验证码。例如我使用雅虎邮箱。
完成注册之后,登录网站,然后点击maven。
点击add new package,为我们的library创建一个新的package。
假设你已经注册账你并按照上面步骤操作,或者使用我提供的账号,登陆成功后会出现如下界面,点击maven进入该仓库,并点击add new package创建新的包。
填写package相关信息,如下:
第二部分:操作as项目,配置相关信息,命令行操作lib包上传。
android studio安装上传bintray插件和填写相关信息:(下面选用我测试通过并且操作路径最短的方式)
在项目的根build文件中补充如下标红内容
这是根build源文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' classpath 'com.novoda:bintray-release:+' // 新增 // note: do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } tasks.withtype(javadoc) { // 新增 options.addstringoption( 'xdoclint:none' , '-quiet' ) options.addstringoption( 'encoding' , 'utf-8' ) } } task clean(type: delete) { delete rootproject.builddir } |
然后在lib的build文件中补充如下内容:
这是lib的源build文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
`apply plugin: `` 'com.android.library' ` `apply plugin: `` 'com.novoda.bintray-release' ` ` // 新增` `android {` `compilesdkversion `` 28 ` `defaultconfig {` `minsdkversion `` 15 ` `targetsdkversion `` 28 ` `versioncode `` 2 ` `versionname `` "1.0.2" ` `testinstrumentationrunner `` "android.support.test.runner.androidjunitrunner" ` `}` `buildtypes {` `release {` `minifyenabled `` false ` `proguardfiles getdefaultproguardfile(`` 'proguard-android.txt' ``), `` 'proguard-rules.pro' ` `}` `}` `lintoptions { `` // 新增` `abortonerror `` false ` `}` `}` `dependencies {` `implementation filetree(dir: `` 'libs' ``, include: [`` '*.jar' ``])` `implementation `` 'com.android.support:appcompat-v7:28.0.0-rc02' ` `testimplementation `` 'junit:junit:4.12' ` `androidtestimplementation `` 'com.android.support.test:runner:1.0.2' ` `androidtestimplementation `` 'com.android.support.test.espresso:espresso-core:3.0.2' ` `}` `publish { `` // 新增` `userorg = `` 'huangweicai' ` ` // 注册bintray时的username` `groupid = `` 'com.infinitus_demo_lib' ` ` // 项目包名` `artifactid = `` 'infinitus_demo_lib' ` ` // 项目名` `publishversion = `` '1.0.2' ` ` // 发布版本号` `desc = `` 'summarize the tools or methods commonly used in routine development' ` ` // 项目描述,可选项` `website = `` '[https://github.com/huangweicai/infinitus_demo_lib' ](https://github.com/huangweicai/infinitus_demo_lib')` `// 项目站点,可选项` `}` |
在android studio的命令行窗口依次输入如下命令:
1
2
3
|
gradlew generatepomfileforreleasepublication gradlew publishreleasepublicationtomavenlocal gradlew bintrayupload -pbintrayuser=xxx -pbintraykey=xxx -pdryrun= false |
其中,pbintrayuser是bintray的用户名,pbintraykey是bintray的api key。(api key在注册成功后,可以在修改信息的界面找到,最好在第一次注册成功后就记录好)
等待执行,看到build successful说明上传bintray成功。
进入bintray,可以找到我们上传的包,在页面的左下角看到maven地址说明上传内容正确,第一次在页面的右下角会看到add to jcenter,需要我们手动点击一下这个add to jcenter按钮,然后等待lib包审核通过后,我们就可以引用jcenter上的包了。
以上就是android studio打包上传到jcenter的完整流程。
测试:as引入implementation ‘com.infinitus_demo_lib:infinitus_demo_lib:1.0.2',代码中调用演示工具类testutil.test(context);查看吐司是否提示,提示成功说明已经成功发布并引入jcenter包。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://huangweicai.github.io/2018/10/21/sendto_jcenter/