-
Notifications
You must be signed in to change notification settings - Fork 8
/
build_apk.sh
executable file
·129 lines (112 loc) · 2.92 KB
/
build_apk.sh
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash -e
#
# build debug or (signed) release apk and open it in default file manager
VARIANTS=("bitcoinSignet" "bitcoinTestnet" "bitcoinMainnet")
OUT_BASE="app/build/outputs/apk"
RELEASE_PATH="app/release"
APK_NAME=""
TARGET="Debug"
_die() {
echo "$@"
exit 1
}
_log() {
echo "-" "$@"
}
_tit() {
echo
echo "--------------------------------"
echo "$@"
echo "--------------------------------"
}
_checks() {
./gradlew lint
./gradlew spotlessCheck
}
_confirm() {
echo
echo "Ready to build $TARGET APKs"
read -r -p "Proceed? (y/n) " ANS
case $ANS in
[Yy]) echo;;
*) _die "aborting";;
esac
}
_set_apk_name() {
APK_NAME="app-$VARIANT-${TARGET,,}"
}
_prepare() {
local variant="$1"
local debug_out="$OUT_BASE/$variant/debug"
local release_out="$OUT_BASE/$variant/release"
local creating="creating $TARGET output path..."
local removing="removing existing APK(s) $APK_NAME..."
if [ "$TARGET" = "Debug" ]; then
_log "$creating"
mkdir -vp "$debug_out"
_log "$removing"
rm -vf "$debug_out/$APK_NAME.apk"
else
_log "$creating"
mkdir -vp "$release_out"
mkdir -vp "$RELEASE_PATH"
_log "$removing"
rm -vf "$release_out/$APK_NAME-aligned.apk"
rm -vf "$RELEASE_PATH/$APK_NAME.apk"
fi
}
_build() {
./gradlew "assemble$TARGET"
}
_align_and_sign() {
local variant="$1"
local release_out="$OUT_BASE/$variant/release"
local apk="$release_out/$APK_NAME"
_log "aligning $APK_NAME APK..."
zipalign -v -p 4 "$apk.apk" "$apk-aligned.apk"
_log "signing $APK_NAME APK..."
apksigner sign --ks "$KEYSTORE" \
--out "$RELEASE_PATH/$APK_NAME.apk" "$apk-aligned.apk"
}
_output_info() {
if [ "$TARGET" = "Debug" ]; then
for variant in "${VARIANTS[@]}"; do
realpath "$OUT_BASE/$variant/debug/"
done
else
realpath "$RELEASE_PATH/"
fi
}
# target selection
[ "${1,,}" = release ] && TARGET="Release"
if [ "$TARGET" = "Release" ]; then
KEYSTORE=$(grep -A10 'signingConfigs {' app/build.gradle \
|grep -A5 'release {' |grep 'storeFile' |awk -F'"' '{print $2}' \
|sed "s#\${System.properties\['user.home'\]}#$HOME#")
fi
# preliminary checks
_tit "running preliminary checks..."
[ "$TARGET" != "Debug" ] && [ "$TARGET" != "Release" ] && \
_die "unrecognized target specified"
[ "$TARGET" = "Release" ] && [ -z "$KEYSTORE" ] && \
_die "Please specify a keystore for signing"
_checks
_confirm
# APK build
_tit "preparing directories..."
for VARIANT in "${VARIANTS[@]}"; do
_set_apk_name
_prepare "$VARIANT"
done
_tit "building $TARGET APKs..."
_build
if [ "$TARGET" = "Release" ]; then
_tit "signing $TARGET APKs..."
for VARIANT in "${VARIANTS[@]}"; do
_set_apk_name
_align_and_sign "$VARIANT"
done
fi
# output path info
_tit "Built APKs have been output to:"
_output_info