Android 工具命令——发布项目到JitPack

主要分以下几步:

  1. 本地创建Library工具类;
  2. 配置JitPack相关信息;
  3. 排查Library中错误并上传至GitHub;
  4. 创建release并在JitPack中编译;
  5. 在项目中引用

本文链接:https://rainmonth.github.io/posts/A201230.html

创建Library工具类

新建了一个新Android项目RandyJitPack,然后给改项目添加一个名为utils的Library Module,Library Module的包名为com.rainmonth.utils

配置JitPack相关信息

在RandyJitPack项目的根目录的build.gradle文件中添加如下配置:

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
// 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.6.1'
// 注意插件和gradle的对应关系,可以在 https://developer.android.google.cn/studio/releases/gradle-plugin.html#updating-gradle查询
// 查询插件版本: https://github.com/dcendents/android-maven-gradle-plugin/releases
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'


// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io'}
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

插件版本和gradle版本(gradle-wrapper.properties中的版本)对应关系速查

插件版本 所需的Gradle版本 Dependencie版本
1.0.0-1.1.3 2.2.1-2.3 com.github.dcendents:android-maven-gradle-plugin:1.2
1.2.0-1.3.1 2.2.1-2.9 com.github.dcendents:android-maven-gradle-plugin:1.2
1.5.0 2.2.1-2.13 com.github.dcendents:android-maven-gradle-plugin:1.2
2.0.0-2.1.2 2.10-2.13 com.github.dcendents:android-maven-gradle-plugin:1.1
2.1.3-2.2.3 2.14.1+ com.github.dcendents:android-maven-gradle-plugin:1.5.1
2.3.0+ 3.3+ com.github.dcendents:android-maven-gradle-plugin:1.5
3.0.0+ 4.1+ com.github.dcendents:android-maven-gradle-plugin:2.0
3.1.0+ 4.4+ com.github.dcendents:android-maven-gradle-plugin:2.0
3.2.0-3.2.1 4.6+ com.github.dcendents:android-maven-gradle-plugin:2.1
3.3.0-3.3.2 4.10.1+ com.github.dcendents:android-maven-gradle-plugin:2.1
3.4.0-3.4.1 5.1.1+ com.github.dcendents:android-maven-gradle-plugin:2.1
3.5.0+ 5.4.1-5.6.4 com.github.dcendents:android-maven-gradle-plugin:2.1

注意上面的dependencies列的内容不一定准确,可以多试几次。

utils这个mudule的build.gradle中,添加如下配置:

1
2
3
4
// 应用maven插件
apply plugin: 'com.github.dcendents.android-maven'
// 声明group
group = 'com.github.rainmonth'

排查Library中错误并上传至GitHub

将项目中的utils module以及跟项目的配置文件上传至GitHub,如下图:

Android上传项目到JitPack

只需要上传红河框框里面的部分。(前提先要在自己的GitHub账户上创建一个响应的项目,如RandyJitPack,然后将上面截图部分提交至改地址,用一下命令:

1
2
git remote add origin https://github.com/Rainmonth/RandyJitPack.git
git push --set-upstream origin master

创建release并在JitPack中编译

创建release

RandyJitPack的项目主页,找到release,点击,就可以看到create a new release(之前没有创建过release)或着Draft a new relase(之前创建过)按钮,点击即可完成release的创建,如下图:

创建release

JitPack中编译

信息录入号后,点击Publish release即可将项目发布到https://jitpack.io。这个时候我们复制我们项目的地址,然后在https://jitpack.io中查找,即可看到下图所示的界面

jitpack.io查找项目

看到后,点击Get it:

  • 如果log为红色的,说明编译失败了,可以点击log查看失败的原因
  • 如果是绿色的,说明成功的,可以通过项目名称 + tab号来引用该项目;

在项目中引用

还是在jitpack.io中搜到的项目那个页面,在编译成功后,下面会有如下所示的一段引用代码,将其加入到要引用的项目的build.gradle文件的dependencies中即可完成引用,代码如下:

Root project下的build.gradle添加如下代码:

1
2
3
4
5
6
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

要应用的module的build.gradle中添加如下代码:

1
2
3
dependencies {
implementation 'com.github.Rainmonth:RandyJitPack:Tag'// tag为创建release时填入的内容
}

通过以上几个步骤,我们就完成了在线库的发布,是不是很容易!