-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapkdev
executable file
·384 lines (318 loc) · 8.32 KB
/
apkdev
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
# variables
if [ -z $APKDEV_ROOT ];
then
_apkdev=~/apk-devtool
else
_apkdev=$APKDEV_ROOT
fi
source $_apkdev/config
# end
_cyan=""
_yellow=""
_green=""
_reset=""
if [ ! $(tput colors) -eq -1 ]; then
if [ -t 1 ]; then
_cyan="\033[36m"
_yellow="\033[33m"
_green="\033[32m"
_reset="\033[m"
fi
fi
build(){
case $1 in
r*)
_release=1
;;
*)
_release=0
;;
esac
if [ -d .apkdev ]; then
echo -e $_cyan"reading project configuration..."$_yellow
cd .apkdev
if [ -f project_tree ]; then
source project_tree
fi
if [ -f signing ]; then
source signing
fi
cd ..
fi
if [ -d build ]; then
echo -e $_cyan"clearing build directory..."$_yellow
rm -r build
fi
echo -e $_cyan"building build directory..."$_yellow
mkdir -p build/gen build/java build/dex build/obj
if [ $_release -eq 1 ]; then
echo -e $_cyan"copying build resources..."$_yellow
cp $_apkdev/android-proguard-defaults build/android-proguard-defaults
fi
echo -e $_cyan"compiling resources..."$_yellow
aapt2 compile --dir $_res -o build/obj
echo -e $_cyan"linking resources..."$_yellow
_link_opts="
-o build/output.unsigned.apk
--manifest $_manifest
--java build/gen/
build/obj/*.flat
-I $_android_jar
--auto-add-overlay
--override-styles-instead-of-overlaying
--allow-reserved-package-id
--no-version-vectors
--no-auto-version
--no-version-transitions
--min-sdk-version 16
--target-sdk-version 32
"
if [ -d $_assets ];then
_link_opts=$_link_opts"
-A $_assets
"
fi
if [ $_release -eq 1 ]; then
_link_opts=$_link_opts"
--proguard build/proguard_options
--proguard-minimal-keep-rules
--no-xml-namespaces
"
fi
aapt2 link $_link_opts
echo -e $_cyan"searching for java code..."$_yellow
_java_files=$(find -name "*.java" | grep "$_java")
echo -e $_cyan"searching for kotlin code..."$_yellow
_kotlin_files=$(find -name "*.kt" | grep "$_java")
_package=$(aapt2 dump packagename build/output.unsigned.apk | sed -r 's/[.]+/\//g')
if [ ! -z "$_kotlin_files" ];
then
echo -e $_cyan"compiling kotlin code..."
kotlinc $_kotlin_files $_java_files -cp $_android_jar build/gen/$_package/R.java -d build/java
fi
if [ ! -z "$_java_files" ];
then
echo -e $_cyan"compiling java code..."$_yellow
javac -cp $_android_jar:build/java $_java_files build/gen/$_package/R.java -d build/java
fi
echo -e $_cyan"converting java bytecode to dex..."$_yellow
if [ $_release -eq 1 ]; then
r8 --dex --output build/dex/ --lib $_android_jar --pg-conf build/android-proguard-defaults --pg-conf build/proguard_options $(find -name "*.class" | grep "./build/java")
else
d8 --output build/dex/ --lib $_android_jar $(find -name "*.class" | grep "./build/java")
fi
echo -e $_cyan"linking dex code..."$_yellow
cd build/dex/
zip -ur ../output.unsigned.apk classes.dex
cd ../..
if [ $_release -eq 1 ]; then
echo -e $_cyan"optimising apk structure..."$_yellow
# optimize
aapt2 optimize --shorten-resource-paths --collapse-resource-names build/output.unsigned.apk -o build/output.optimized.apk
echo -e $_cyan"aligning apk..."$_yellow
zipalign -f -p 4 build/output.optimized.apk build/output.aligned.apk
else
echo -e $_cyan"aligning apk..."$_yellow
zipalign -f -p 4 build/output.unsigned.apk build/output.aligned.apk
fi
echo -e $_cyan"signing apk..."$_yellow
apksigner sign -ks $_jks --ks-pass pass:$_jks_pass build/output.aligned.apk
mv build/output.aligned.apk build/output.signed.apk
termux-open build/output.signed.apk & 2&> /dev/null
echo -e $_green"done"$_yellow
}
help(){
echo -e "
apkdev is a development helper for building apks.
notice:
expected structure
\t<project-root>
\t |-.apkdev/ (configurations)
\t |-app/src/
\t |-main/ (_branch) (_base)
\t |-res/ (_res)
\t |-java/ (_java)
\t |-AndroidManifest.xml (_manifest)
\tto change the expected structure,
\tchange the variables in the script.
\t(ie. _branch, _java, etc.)
to use:
eg. apkdev build debug
eg. apkdev h
eg. apkdev c n
build (shortcut: b)
\tbuilds an apk.
\twipes and make a build/ directory for compiling.
\tcreate a apk as build/output.signed.apk
\tavailable options: release/debug
\trelease (r):
\t\tbuilds a release apk.
\t\thave optimization on.
\tdebug (d) (default when no options):
\t\tbuilds a debug apk.
\t\tcontains debug info.
\texecute command at <project-root>
maven (m)
\toperations using maven repos
\tavailable options: download
\tdownload (d):
\t\tdownload jars requested
\t\tapkdev m d <group_id>:<id>:<version> <repo url>
\t\tdefault repo url:
\t\t\thttps://dl.google.com/android/maven2
\t\t\thttps://repo1.maven.org/maven2
\t\teg. apkdev m d androidx.core:core:1.9.0
\t\teg.apkdev m d org.mozilla.components:support-base:105.0.6 https://maven.mozilla.org/maven2
new (n)
\tcreate a new android project.
\thave a basic hello world setup.
config (c)
\toperation to configurate projects
\tavailable option: new
\tnew (n):
\t\tcreate a configuration for an existing project
help (h) (default if no options)
\tprint this help message.
"
}
new(){
echo -e $_green"enter app name:"$_reset
read -p "> " _name
if [ -d "$_name" ]; then
echo "$_name already exist!"
echo "aborting!"
return
fi
echo -e $_green"enter app package name:"$_reset
read -p "> " _package
echo -e $_cyan"creating structure..."$_yellow
mkdir "$_name"
cd "$_name"
_package_dir=$(echo $_package | sed -r 's/[.]+/\//g')
mkdir -p $_java/$_package_dir $_res/layout $_res/drawable $_res/values $_assets
config new
echo -e $_cyan"filling resources..."$_yellow
cp $_apkdev/res/ic_launcher.png $_res/drawable
echo "<resources>
<string name=\"app_name\">$_name</string>
</resources>" > $_res/values/strings.xml
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\">
<TextView
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"Hello World!\" />
</LinearLayout>" > $_res/layout/activity_main.xml
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:versionCode=\"1\"
android:versionName=\"1.0\"
package=\"$_package\">
<application
android:theme=\"@android:style/Theme\"
android:label=\"@string/app_name\"
android:icon=\"@drawable/ic_launcher\">
<activity android:name=\".MainActivity\"
android:exported=\"true\">
<intent-filter>
<action android:name=\"android.intent.action.MAIN\" />
<category android:name=\"android.intent.category.LAUNCHER\" />
</intent-filter>
</activity>
</application>
</manifest>" > $_base/AndroidManifest.xml
echo "package $_package
import android.app.Activity
import android.os.Bundle
class MainActivity(): Activity() {
override fun onCreate(bundle: Bundle?) {
super.onCreate(bundle)
this.setContentView(R.layout.activity_main)
}
}
" > $_java/$_package_dir/MainActivity.kt
cd ..
echo -e $_green"done"$_yellow
}
config(){
_option=0
case $1 in
n*)
_option=1
;;
*)
echo "$1 o"
help
return
;;
esac
if [[ $_option -eq 1 ]]; then
echo -e $_cyan"setting default configurations..."$_yellow
mkdir -p .apkdev
echo "#!/bin/bash
#configuration for apk-devtool
_java=$_java
_res=$_res
_assets=$_assets
_base=$_base
" > .apkdev/project_tree
echo "#!/bin/bash
#signing configuration for apk-devtool
#_jks=$_jks
#_jks_pass=$_jks_pass
" > .apkdev/signing
fi
}
maven(){
case $1 in
d*)
mavendl $2 $3
;;
*)
help
;;
esac
}
mavendl(){
_maven="
$2
https://dl.google.com/android/maven2
https://repo1.maven.org/maven2
"
_name="$1"
# split to block
_b=($(echo $_name | tr ":" "\n"))
# group id
_gid=$(echo ${_b[0]} | tr "." "/")
# id
_id=${_b[1]}
# version
_v=${_b[2]}
for url in $_maven; do
wget "$url/$_gid/$_id/$_v/$_id-$_v-sources.jar"
if [[ $? -eq 0 ]]; then
break
fi
done
}
case $1 in
b*)
build $2
;;
n*)
new
;;
c*)
config $2
;;
m*)
maven $2 $3 $4
;;
*)
help
;;
esac
echo -e $_reset