继Jenkins构建java项目后来实现Jenkins构建Android。且基本环境与其一致。
文章目录
一、Jenkins安装gradle插件
二、安装gradle和Android SDK
选版本号的时候先和安卓开发人员沟通近一点的版本号,如果版本差异太大会导致很多错误。
1.安装gradle
下载gradle压缩包
[root@localhost ~]# wget https://services.gradle.org/distributions/gradle-3.5-all.zip
--2021-06-30 10:18:03-- https://services.gradle.org/distributions/gradle-3.5-all.zip
Resolving services.gradle.org (services.gradle.org)... failed: Name or service not known.
wget: unable to resolve host address ‘services.gradle.org’
若下载时出现这这种情况只需多只需几次下载命令即可
[root@localhost ~]# wget https://services.gradle.org/distributions/gradle-3.5-all.zip
--2021-06-30 10:18:18-- https://services.gradle.org/distributions/gradle-3.5-all.zip
Resolving services.gradle.org (services.gradle.org)... 104.18.190.9, 104.18.191.9
Connecting to services.gradle.org (services.gradle.org)|104.18.190.9|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://downloads.gradle-dn.com/distributions/gradle-3.5-all.zip [following]
--2021-06-30 10:18:20-- https://downloads.gradle-dn.com/distributions/gradle-3.5-all.zip
Resolving downloads.gradle-dn.com (downloads.gradle-dn.com)... 1.86.232.204, 240e:658:2401:1e3c:ac0c:0:6812:a463
Connecting to downloads.gradle-dn.com (downloads.gradle-dn.com)|1.86.232.204|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 95084513 (91M) [application/zip]
Saving to: ‘gradle-3.5-all.zip’
100%[==================================================================================================================================================>] 95,084,513 1.40MB/s in 61s
2021-06-30 10:19:22 (1.48 MB/s) - ‘gradle-3.5-all.zip’ saved [95084513/95084513]
解压缩并设置环境变量
[root@localhost ~]# unzip gradle-3.5-all.zip -d /opt/
[root@localhost ~]# ll /opt/
total 0
drwxr-xr-x 9 root root 220 Jun 22 10:24 apache-tomcat-8.5.58
drwxr-xr-x 9 root root 150 Apr 10 2017 gradle-3.5
drwxr-xr-x 2 root root 48 Jun 9 15:48 product
drwxr-xr-x. 2 root root 6 Oct 31 2018 rh
drwxr-xr-x 3 root root 34 Jun 9 11:46 svn
[root@localhost ~]# vim /etc/profile
export GRADLE_HOME=/opt/gradle-3.5
export PATH=$GRADLE_HOME/bin:$PATH
[root@localhost ~]# source /etc/profile
2.安装Android SDK
下载
[root@localhost ~]# wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
解压缩
[root@localhost ~]# tar zxf android-sdk_r24.4.1-linux.tgz -C /opt/
[root@localhost ~]# ll /opt/
total 0
drwxrwxr-x 5 249512 5000 73 Oct 14 2015 android-sdk-linux
drwxr-xr-x 9 root root 220 Jun 22 10:24 apache-tomcat-8.5.58
drwxr-xr-x 9 root root 150 Apr 10 2017 gradle-3.5
drwxr-xr-x 2 root root 48 Jun 9 15:48 product
drwxr-xr-x. 2 root root 6 Oct 31 2018 rh
drwxr-xr-x 3 root root 34 Jun 9 11:46 svn
设置环境变量
[root@localhost ~]# vim /etc/profile
export ANDROID_HOME=/opt/android-sdk-linux
export PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH
[root@localhost ~]# source /etc/profile
三、Jenkins系统配置
1.全局工具配置gradle
2.系统环境变量配置SDK
四、构建任务
1.新建任务
2.配置构建参数
重点来了
为方便构建apk时自动将版本信息更新到数据库中这里用参数化构建。
说白了就一个字:省事!
以下参数根据自身需求来进行增加或者删减
apk版本类型
apk版本数
apk版本号
apk版本类型
日期参数
3.配置url
4.替换安卓签名文件路径
在构建的时候增加一个shell来替换
#!/bin/bash
sed -i "s/D:\\\\\\\Android Studio\\\\\\\project\\\\\\\YouXinZhiHui\\\\\\\YouXinkeystore.jks/\/root\/.jenkins\/jobs\/Android\/workspace\/YouXinkeystore.jks/g" /root/.jenkins/jobs/Android/workspace/app/build.gradle
以上多个\是为了转义。
5.配置gradle
6.增加构建步骤分发apk包并更新数据库里的 apk版本信息
远程部署apk
更新数据库apk版本信息
五、构建
常见问题
在配置不存在问题的情况下,构建失败的最多原因就是安卓开发人员使用的gradle、SDK版本和你用来构建的所下载的gradle、SDK版本差太大。
其他错误可以根据控制台输出来进行调整。
欢迎各路大佬留言交流