-
Notifications
You must be signed in to change notification settings - Fork 25
/
build.sh
executable file
·232 lines (168 loc) · 4.29 KB
/
build.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/data/data/com.termux/files/usr/bin/bash
## This script to build "Termux Launcher"
## "Termux Launcher" is android application to
## make "Termux" as launcher without hack anything
## of "Termux" application.
## This apps will creating shell script located on
## /sdcard/termuxlauncher/.apps-launcher
## Recomended: if you want use "launch" command to
## launch app with cmmand on termux anytime.
## write to your ~/.bashrc or your ~/bash_profile
## like this:
## source /storage/shared/termuxlauncher/.apps-launcher
##
## Author: Amsit (@amsitlab) <[email protected]>
## Thank to:
## forwall (author of the termux)
## sdrausty (refference to build apk on termux)
## BlackHoleSecurity (community to learning code)
##
## Application name
APP_NAME="termuxlauncher"
APP_VERSION="1.1"
## main source directory
SRC_MAIN_DIR="app/src/main"
## java source directory
JAVA_SRC_DIR=$SRC_MAIN_DIR/java
## android.jar location
ANDROIDJAR=${PREFIX}/share/java/android.jar
ANDROIDMANIFEST=${SRC_MAIN_DIR}/AndroidManifest.xml
## generated binary directory
BIN_DIR="bin"
# java classes directory
CLASSES_DIR=${BIN_DIR}/classes
## Required packages
REQ_PKG=("ecj" "dx" "aapt" "apksigner")
## Requred package length
REQ_LEN=${#REQ_PKG[@]}
## Apk output diractory"
BUILD_APK_PATH=${EXTERNAL_STORAGE}/Download/buildAPKs
## Java file list
JAVA_FILE_LIST=`find ${JAVA_SRC_DIR} -type f -name \*.java`
KEYSTORE_DIR=".keystore"
KEY_PK8=${KEYSTORE_DIR}/key.pk8
X509_PEM=${KEYSTORE_DIR}/cert.x509.pem
REQ_PEM=${KEYSTORE_DIR}/req.pem
KEY_PEM=${KEYSTORE_DIR}/key.pem
APK=${APP_NAME}-${APP_VERSION}.apk
APK_UNSIGNED=${APP_NAME}.unsigned.apk
APK_ALIGNED=${APP_NAME}.aligned.apk
## Check required package and install if not installed
for((i=0; i<$REQ_LEN; i++));
do
exists=`which ${REQ_PKG[$i]}`
if [ -z "$exists" ]; then
apt install -y ${REQ_PKG[$i]}
fi
done
OSSL=openssl
if [[ "x${OSSL}" = "x" ]] ; then
apt install -y openssl-tool
fi
## Make sure shared storage has set up
test ! -d ~/storage &&
termux-setup-storage
##
test ! -d $BUILD_APK_PATH &&
mkdir -p $BUILD_APK_PATH
rm -f $BIN_DIR
## Creating R.java
echo
echo "aapt begun: creating R.java"
echo
sleep 1
aapt package -v -f \
-M ${ANDROIDMANIFEST} \
-S ${SRC_MAIN_DIR}/res \
-J ${JAVA_SRC_DIR} \
-I ${ANDROIDJAR} \
-m
sleep 1
echo
## Compile to classes
echo "ecj begun: compiling"
echo
sleep 1
ecj -verbose \
-d $CLASSES_DIR \
-classpath $ANDROIDJAR \
-sourcepath $JAVA_SRC_DIR \
$JAVA_FILE_LIST
sleep 1
echo
## Dexing all .class on ./obj path
echo "dx begun: creating ${BIN_DIR}/classes.dex"
echo
test ! -d $BIN_DIR &&
mkdir -p $BIN_DIR
sleep 1
dx --dex --verbose --output ${BIN_DIR}/classes.dex $CLASSES_DIR
sleep 1
echo
## Creating unsigned appliaction file
echo "aapt bagun: creating ${BIN_DIR}/${APK_UNSIGNED}"
echo
sleep 1
aapt package -v -f \
-M $ANDROIDMANIFEST \
-S ${SRC_MAIN_DIR}/res \
-F ${BIN_DIR}/${APK_UNSIGNED}
sleep 1
echo
if [[ ! -d $KEYSTORE_DIR ]] ; then
echo "make dir ${KEYSTORE_DIR}"
mkdir -p $KEYSTORE_DIR
fi
if [ ! -f "$X509_PEM" ] ; then
openssl genrsa -out $KEY_PEM 2048
openssl req -new -key $KEY_PEM -out $REQ_PEM
openssl x509 -req -days 10000 -in $REQ_PEM \
-signkey $KEY_PEM -out $X509_PEM
openssl pkcs8 -topk8 -outform DER -in $KEY_PEM \
-inform PEM -out $KEY_PK8 -nocrypt
fi
## Change to ./bin directory
echo "Go to ${BIN_DIR} directory"
echo
sleep 1
cd ${BIN_DIR}
## Adding classes.dex to application file
echo "aapt begun: adding classes.dex to ${APK_UNSIGNED}"
echo
sleep 1
aapt add -f ${APK_UNSIGNED} classes.dex
echo
echo "[zip aligned]"
zipalign 4 ${APK_UNSIGNED} ${APK_ALIGNED}
## Sign application file
echo "apksigner begun: creating ${APK}"
echo
sleep 1
apksigner sign \
-in $APK_ALIGNED \
-out $APK \
-key ../${KEY_PK8} \
-cert ../${X509_PEM}
sleep 1
## Change permission of signed appliaction file
echo "Change permission ${APK} to 644"
echo
sleep 1
chmod 644 ${APK}
## Removing unsigned aplication file
echo "Removing ${APK_UNSIGNED}"
echo
sleep 1
rm -fr ${APK_UNSIGNED}
## Copy application file
echo "Copy ${APK} to ${BUILD_APK_PATH}/${APK}"
echo
sleep 1
cp ${APK} ${BUILD_APK_PATH}/${APK}
## Go back to prev directory
echo "back to prev directory"
echo
sleep 1
cd ..
echo "You can install \"${APP_NAME}\" apps by clicking file located on ${BUILD_APK_PATH}/${APK} with your favorite file manager."
echo