使用shell命令自动打包和处理和安装apk

我最近在工作中遇到一个经常重复的安装和复制操作,需要不定期给某几台设备安装apk并且要预装文件和设置设备序列号以及Mac地址。我今天尝试将这些操作用shell脚本一键处理,结果很成功。

在shell脚本中执行adb命令的方式有多种,可以直接adb shell + 命令执行也可以将命令全部放入一个txt文本文件,通过adb shell < 文件路径, 来批量执行。

脚本运行过程

定义路径

1
2
3
4
5
6
7
shell_path="/home/shuihan/Documents/shell/"
model1_name="M_Liveness_Antispoofing_Binocular.model"
model2_name="M_Verify_MIMICG2_Common_3.17.0_v1.model"
project_path="/home/shuihan/workspace/Faceye/"
apk_path=$project_path"app/build/outputs/apk/release/"
assets_path="/home/shuihan/workspace/Faceye/app/src/main/assets/"
target_path="/sdcard/sensetime/"

shell_path是我当前shell脚本文件的所在路径
model1_name和model2_name是我需要写入移动设备的两个文件名称,这两个文件在assets_path目录下。
project_path是我Android工程的根目录。
target_path是需要写入到移动设备内的路径。

写入文件到移动设备

1
2
3
4
5
echo "写入第一个model文件"
adb push $assets_path$model1_name $target_path$model1_name

echo "写入第二个model文件"
adb push $assets_path$model2_name $target_path$model2_name

使用adb push命令将PC中的两个文件写入到移动设备中。

构建打包

1
2
3
4
5
6
7
read -p "是否需要执行构建打包操作y/n?" pk
if [ "$pk" == "y" ];then
echo "开始打包"
cd $project_path
chmod +x gradlew
./gradlew assembleRelease
fi

询问是否需要打包,如果需要打包,则执行项目根目录下的gradlew脚本,这个脚本是Android中gradle自动生成的,在windows系统中是gradlew.bat文件。

如果要打debug包,就执行./gradlew assembleDebug

卸载、安装、启动

1
2
3
4
5
6
7
8
9
10
11
echo "卸载旧包"
adb uninstall com.guanying.faceye

echo "安装新包"
adb install $apk_path/com.guanying.faceye-release-*.apk

echo "启动应用"

#adb shell < $shell_path"startapp.txt"
adb shell am start -n com.guanying.faceye/com.guanying.faceye.activity.SplashActivity
adb shell exit

执行adb shell am start -n 包名/启动Activity名字 来启动应用。然后调用adb shell exit退出adb shell.

脚本代码

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
#!/bin/bash

#路径和变量
shell_path="/home/shuihan/Documents/shell/"
model1_name="M_Liveness_Antispoofing_Binocular.model"
model2_name="M_Verify_MIMICG2_Common_3.17.0_v1.model"
project_path="/home/shuihan/workspace/Faceye/"
apk_path=$project_path"app/build/outputs/apk/release/"
assets_path="/home/shuihan/workspace/Faceye/app/src/main/assets/"
target_path="/sdcard/sensetime/"


echo "写入第一个model文件"
adb push $assets_path$model1_name $target_path$model1_name

echo "写入第二个model文件"
adb push $assets_path$model2_name $target_path$model2_name

read -p "是否需要执行构建打包操作y/n?" pk
if [ "$pk" == "y" ];then
echo "开始打包"
cd $project_path
chmod +x gradlew
./gradlew assembleRelease
fi

echo "卸载旧包"
#adb uninstall com.guanying.faceye

echo "安装新包"
#adb install $apk_path/com.guanying.faceye-release-*.apk

echo "启动应用"

#adb shell < $shell_path"startapp.txt"
adb shell am start -n com.guanying.faceye/com.guanying.faceye.activity.SplashActivity
adb shell exit

read -p "是否要修改sn或者mac地址y/n?" sn

if [ "$sn" == "y" ];then
echo "输入两位数字设备序号:"
read -n2 dnum
if [ -n "$dnum" ];then
echo "开始修改设备序列号为:GY01010000"$dnum
adb shell ./system/bin/rk_vendor_rw -s "GY01010000"$dnum
echo "开始修改Mac地址为:0c:6a:b5:0a:00:"$dnum
adb shell ./system/bin/rk_vendor_rw -e "0c:6a:b5:0a:00:"$dnum
echo "修改结果"
adb shell ./system/bin/rk_vendor_rw
adb shell exit
fi
fi

echo "是否要重启y/n?"
read reb

if [ "$reb" == "y" ];then
adb reboot
fi