This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apk_generator.sh
executable file
·171 lines (154 loc) · 6.18 KB
/
apk_generator.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh
#
# Automatically creates a temi app from a TBO-file.
#
# Usage
# ./apk_generator.sh <app-name> <.tbo>
#
# MIT License
#
# Copyright (c) 2020 Raymond Oung
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# immediately exit if any command exits with a non-zero status
set -e
# constants
DEFAULT_ANDROID_HOME=~/Android/Sdk # default location on Linux
TEMPLATE_DIR="temi-apk-template"
TMP_DIR="tmp"
# display usage instructions
usage()
{
echo ""
echo "usage: apk_generator.sh <tbo-file> <app-name>"
echo ""
echo "Creates a temi app from a TBO-file"
echo ""
echo "dependencies:"
echo ""
echo " - Android-SDK with the ANDROID_HOME environment variable"
echo " set appropriately."
echo ""
echo "positional arguments:"
echo ""
echo " tbo-file TBO file."
echo " app-name App name. Use double-quotes to encapsulate"
echo " a name with whitespace."
echo ""
exit 1
}
# attempt to automatically set ANDROID_HOME environment variable
set_android_home()
{
echo "ANDROID_HOME environment variable not set. Attempting to set automatically..."
if [ -d "${DEFAULT_ANDROID_HOME}" ]; then
export ANDROID_HOME=${DEFAULT_ANDROID_HOME}
echo "export ANDROID_HOME=${ANDROID_HOME}"
else
echo "[Error] Android SDK cannot be found. Please set the ANDROID_HOME environment variable to your Android-SDK's (Android/Sdk) path."
fi
}
# check path to Android SDK
if env | grep -q "^ANDROID_HOME="; then
if [ -d "${ANDROID_HOME}" ]; then
echo "ANDROID_HOME environment variable set to ${ANDROID_HOME}."
else
set_android_home
fi
else
set_android_home
fi
# check for TBO file
if [ -z "$1" ]; then
echo "Missing TBO file"
usage
else
TBO_FILE=${1%/}
fi
# check for app name
if [ -z "$2" ]; then
echo "Missing app name"
usage
else
APP_NAME=${2%/}
fi
# make a copy of the template
cp -avr "${TEMPLATE_DIR}" "${TMP_DIR}"
# rename package
# - lower case letters
# - substitute whitespace ' ' with underscore '_'
APP_NAME_UNDERSCORE="$(echo ${APP_NAME} | tr '[:upper:]' '[:lower:]' | tr ' ' '_')"
# check environment
UNAME_OUT="$(uname -s)"
case "${UNAME_OUT}" in
Linux*) MACHINE=Linux;;
Darwin*) MACHINE=Mac;;
CYGWIN*) MACHINE=Cygwin;;
MINGW*) MACHINE=MinGw;;
*) MACHINE="UNKNOWN:${UNAME_OUT}"
esac
# renames package
if [ ${MACHINE} = "Linux" ]; then
# modify files inline (Linux)
echo "Linux"
sed -i "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/build.gradle"
sed -i "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/main/AndroidManifest.xml"
sed -i "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/main/java/com/hapirobo/apk_template/MainActivity.java"
sed -i "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/androidTest/java/com/hapirobo/apk_template/ExampleInstrumentedTest.java"
sed -i "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/test/java/com/hapirobo/apk_template/ExampleUnitTest.java"
sed -i "s/apk_name/${APP_NAME}/" "${TMP_DIR}/app/src/main/res/values/strings.xml"
sed -i "s/com.hapirobo.package_name/com.hapirobo.${PACKAGE_NAME}/" "${TMP_DIR}/app/src/main/res/values/strings.xml"
elif [ ${MACHINE} = "Darwin" ]; then
# modify files inline
echo "macOS"
sed -i .bak "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/build.gradle"
sed -i .bak "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/main/AndroidManifest.xml"
sed -i .bak "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/main/java/com/hapirobo/apk_template/MainActivity.java"
sed -i .bak "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/androidTest/java/com/hapirobo/apk_template/ExampleInstrumentedTest.java"
sed -i .bak "s/apk_template/${APP_NAME_UNDERSCORE}/" "${TMP_DIR}/app/src/test/java/com/hapirobo/apk_template/ExampleUnitTest.java"
sed -i .bak "s/apk_name/${APP_NAME}/" "${TMP_DIR}/app/src/main/res/values/strings.xml"
sed -i .bak "s/com.hapirobo.package_name/com.hapirobo.${PACKAGE_NAME}/" "${TMP_DIR}/app/src/main/res/values/strings.xml"
else
echo "[Error] This script only supports Linux and Darwin."
exit 1
fi
# rename directories
mv -v "${TMP_DIR}/app/src/main/java/com/hapirobo/apk_template" "${TMP_DIR}/app/src/main/java/com/hapirobo/${APP_NAME_UNDERSCORE}"
mv -v "${TMP_DIR}/app/src/androidTest/java/com/hapirobo/apk_template" "${TMP_DIR}/app/src/androidTest/java/com/hapirobo/${APP_NAME_UNDERSCORE}"
mv -v "${TMP_DIR}/app/src/test/java/com/hapirobo/apk_template" "${TMP_DIR}/app/src/test/java/com/hapirobo/${APP_NAME_UNDERSCORE}"
# fill with custom code
# https://stackoverflow.com/questions/22497246/insert-multiple-lines-into-a-file-after-specified-pattern-using-shell-script#22497499
MAIN_ACTIVITY_FILE="${TMP_DIR}/app/src/main/java/com/hapirobo/${APP_NAME_UNDERSCORE}/MainActivity.java"
sed -i '/\/\/\ insert\ code\ here/r tbo' "${TMP_DIR}/app/src/main/java/com/hapirobo/${APP_NAME_UNDERSCORE}/MainActivity.java"
# build app
cd "${TMP_DIR}"
./gradlew clean
./gradlew build
cd ..
# move app to root directory
cp -v "${TMP_DIR}/app/build/outputs/apk/debug/app-debug.apk" "${APP_NAME_UNDERSCORE}.apk"
# clean up
echo "Cleaning up..."
rm -fr tmp
echo "[DONE]"
echo ""
# print path to APK
readlink -f "${APP_NAME_UNDERSCORE}.apk"
exit 0