Skip to content

Commit 3699fe1

Browse files
committed
优化使用 bundletool 安装应用的流程
修复 xapk 安装包派发 Android/obb 内容的代码逻辑
1 parent ecfcd36 commit 3699fe1

1 file changed

Lines changed: 38 additions & 11 deletions

File tree

shell/device-tools/InstallApp.sh

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,29 @@ installApksWithBundletool() {
8383
local outputPrint
8484
local javaMajorVersionCode
8585
javaMajorVersionCode=$(getJavaMajorVersionCode)
86-
if (( javaMajorVersionCode <= 11 )); then
86+
87+
local exitCode
88+
89+
if (( javaMajorVersionCode >= 11 )); then
90+
local bundletoolJar
91+
bundletoolJar="$(getBundletoolJarFilePath)"
92+
outputPrint=$(java -jar "${bundletoolJar}" install-apks --apks="${apksFilePath}" --device-id="${deviceId}" < /dev/null 2>&1)
93+
exitCode=$?
94+
else
95+
exitCode=1
96+
fi
97+
98+
if (( exitCode != 0 )); then
99+
# bundletool 安装失败时回退到解压 + install-multiple
87100
local tempDirPath
88101
tempDirPath=$(unzipFileToTempDir "${apksFilePath}")
89102
local -a apkList=()
90103
while IFS= read -r -d '' apk; do apkList+=("${apk}"); done < <(findApkPathForDir "${tempDirPath}")
91104
outputPrint=$(adb -s "${deviceId}" install-multiple -r "${apkList[@]}" < /dev/null 2>&1)
92-
else
93-
local bundletoolJar
94-
bundletoolJar="$(getBundletoolJarFilePath)"
95-
outputPrint=$(java -jar "${bundletoolJar}" install-apks --apks="${apksFilePath}" --device-id="${deviceId}" < /dev/null 2>&1)
105+
exitCode=$?
96106
fi
97-
local exitCode=$?
107+
108+
exitCode=$?
98109
if (( exitCode == 0 )); then
99110
echo "✅ [${deviceId}] 设备安装 [${baseName}] 成功"
100111
return 0
@@ -140,14 +151,30 @@ unzipFileToTempDir() {
140151
maybePushObb() {
141152
local deviceId=$1
142153
local unzipApkDirPath=$2
143-
local obbPath="${unzipApkDirPath}/Android/obb"
144-
if [[ -d "${obbPath}" ]]; then
145-
echo "⏳ [${deviceId}] 检测到 OBB,正在推送至 /sdcard/Android/obb"
146-
adb -s "${deviceId}" shell "mkdir -p /sdcard/Android/obb" < /dev/null > /dev/null 2>&1
147-
adb -s "${deviceId}" push "${obbPath}" "/sdcard/Android/obb" < /dev/null
154+
local obbDirPath="${unzipApkDirPath}/Android/obb"
155+
if [[ -d "${obbDirPath}" ]]; then
156+
echo "⏳ [${deviceId}] 检测到 OBB 目录,正在推送至手机"
157+
traversePushFile "${obbDirPath}" "${obbDirPath}"
148158
fi
149159
}
150160

161+
traversePushFile() {
162+
local rootDirPath="$1"
163+
local currentDirPath="$2"
164+
for childFilePath in "${currentDirPath}"/*; do
165+
if [[ -f "${childFilePath}" ]]; then
166+
sourceObbFilePath="${childFilePath}"
167+
targetObbFilePath="/sdcard/Android/obb/${childFilePath#$rootDirPath/}"
168+
targetObbParentDirPath=$(dirname "${targetObbFilePath}")
169+
MSYS_NO_PATHCONV=1 adb shell "[[ -d '${targetObbParentDirPath}' ]] || mkdir -p '${targetObbParentDirPath}'"
170+
MSYS_NO_PATHCONV=1 adb -s "${deviceId}" push "${sourceObbFilePath}" "${targetObbFilePath}" < /dev/null
171+
MSYS_NO_PATHCONV=1 adb -s "${deviceId}" shell "chmod 644 ${targetObbFilePath}" < /dev/null
172+
elif [[ -d "${childFilePath}" ]]; then
173+
traversePushFile "${rootDirPath}" "${childFilePath}"
174+
fi
175+
done
176+
}
177+
151178
getBashApkPath() {
152179
local dir="$1"
153180
local manifest="${dir}/manifest.json"

0 commit comments

Comments
 (0)