-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathmake.sh
More file actions
executable file
·848 lines (697 loc) · 26.6 KB
/
make.sh
File metadata and controls
executable file
·848 lines (697 loc) · 26.6 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#!/usr/bin/env bash
set -eu
# Color definitions
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
readonly BOLD='\033[1m'
# Info for keystore generation
INFO="CN=Developer, OU=Organization, O=Company, L=City, S=State, C=US"
log() {
echo -e "${GREEN}[+]${NC} $1"
}
info() {
echo -e "${BLUE}[*]${NC} $1"
}
warn() {
echo -e "${YELLOW}[!]${NC} $1"
}
error() {
echo -e "${RED}[!]${NC} $1"
exit 1
}
try() {
local log_file=$(mktemp)
if [ $# -eq 1 ]; then
# Если передан один аргумент - используем eval для сложных команд
if ! eval "$1" &> "$log_file"; then
echo -e "${RED}[!]${NC} Failed: $1"
cat "$log_file"
rm -f "$log_file"
exit 1
fi
else
# Если несколько аргументов - запускаем напрямую
if ! "$@" &> "$log_file"; then
echo -e "${RED}[!]${NC} Failed: $*"
cat "$log_file"
rm -f "$log_file"
exit 1
fi
fi
rm -f "$log_file"
}
set_var() {
local java_file="app/src/main/java/com/$appname/webtoapk/MainActivity.java"
[ ! -f "$java_file" ] && error "MainActivity.java not found"
local pattern="$@"
[ -z "$pattern" ] && error "Empty pattern. Usage: set_var \"varName = value\""
# Extract variable name and new value
local var_name="${pattern%% =*}"
local new_value="${pattern#*= }"
# Check if variable exists
if ! grep -q "$var_name *= *.*;" "$java_file"; then
error "Variable '$var_name' not found in MainActivity.java"
fi
# Remove surrounding quotes (double or single) if provided in the config
new_value="${new_value%\"}"
new_value="${new_value#\"}"
new_value="${new_value%\'}"
new_value="${new_value#\'}"
# Add quotes if value is not boolean AND not a number/integer
if [[ ! "$new_value" =~ ^(true|false)$ ]] && [[ ! "$new_value" =~ ^[0-9]+$ ]]; then
new_value="\"$new_value\""
fi
local tmp_file=$(mktemp)
awk -v var="$var_name" -v val="$new_value" '
{
if (!found && $0 ~ var " *= *.*;" ) {
# Save start of line up to =
match($0, "^.*" var " *=")
before = substr($0, RSTART, RLENGTH)
# Replace value
print before " " val ";"
# Only replace the first occurrence
found = 1
} else {
print $0
}
}' "$java_file" > "$tmp_file"
if ! diff -q "$java_file" "$tmp_file" >/dev/null; then
mv "$tmp_file" "$java_file"
log "Updated $var_name to $new_value"
# Clean value from quotes for logic checks
local val_clean="${new_value//\"/}"
# Handle permissions based on specific variable names
case "$var_name" in
"geolocationEnabled")
update_permission "android.permission.ACCESS_FINE_LOCATION" "$val_clean"
;;
"cameraEnabled")
update_permission "android.permission.CAMERA" "$val_clean"
;;
"microphoneEnabled")
update_permission "android.permission.RECORD_AUDIO" "$val_clean"
update_permission "android.permission.MODIFY_AUDIO_SETTINGS" "$val_clean"
;;
esac
else
rm "$tmp_file"
fi
}
merge_config_with_default() {
local default_conf="app/default.conf"
local user_conf="$1"
local merged_conf
merged_conf=$(mktemp)
# Temporary file for default lines that are missing in user config
local temp_defaults
temp_defaults=$(mktemp)
# For each non-empty, non-comment line in default.conf
while IFS= read -r line; do
# Extract key (everything up to '=')
key=$(echo "$line" | cut -d '=' -f1 | xargs)
if [ -n "$key" ]; then
# Check if the key is missing in the user config
if ! grep -q -E "^[[:space:]]*$key[[:space:]]*=" "$user_conf"; then
# Key is missing – add the default line
echo "$line" >> "$temp_defaults"
fi
fi
done < <(grep -vE '^[[:space:]]*(#|$)' "$default_conf")
# Now combine default lines (if any) with the user configuration.
# The defaults will be added on top, but since they are defined earlier they
# can be overridden by any subsequent assignment (если вдруг порядок имеет значение).
cat "$temp_defaults" "$user_conf" > "$merged_conf"
rm -f "$temp_defaults"
echo "$merged_conf"
}
apply_config() {
local config_file="${1:-webapk.conf}"
# If config file is not found in project root, try in caller's directory
if [ ! -f "$config_file" ] && [ -f "$ORIGINAL_PWD/$config_file" ]; then
config_file="$ORIGINAL_PWD/$config_file"
fi
[ ! -f "$config_file" ] && error "Config file not found: $config_file"
export CONFIG_DIR="$(dirname "$config_file")"
info "Using config: $config_file"
config_file=$(merge_config_with_default "$config_file")
while IFS='=' read -r key value || [ -n "$key" ]; do
# Skip empty lines and comments
[[ -z "$key" || "$key" =~ ^[[:space:]]*# ]] && continue
# Remove spaces, tabs, and invisible unicode characters (zero-width space)
key=$(echo "$key" | tr -cd '[:alnum:]_')
# Trim whitespaces
value=$(echo "$value" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
case "$key" in
"id")
chid "$value"
;;
"name")
rename "$value"
;;
"deeplink")
set_deep_link "$value"
;;
"trustUserCA")
set_network_security_config "$value"
;;
"icon")
set_icon "$value"
;;
"scripts")
set_userscripts $value
;;
*)
set_var "$key = $value"
;;
esac
done < <(sed -e '/^[[:space:]]*#/d' -e 's/[[:space:]]\+#.*//' "$config_file")
}
apk() {
if [ ! -f "app/my-release-key.jks" ]; then
error "Keystore file not found. Run './make.sh keygen' first"
fi
rm -f app/build/outputs/apk/release/app-release.apk
info "Building APK..."
try "./gradlew assembleRelease --no-daemon --quiet"
if [ -f "app/build/outputs/apk/release/app-release.apk" ]; then
log "APK successfully built and signed"
try "cp app/build/outputs/apk/release/app-release.apk '$appname.apk'"
echo -e "${BOLD}----------------"
echo -e "Final APK copied to: ${GREEN}$appname.apk${NC}"
echo -e "Size: ${BLUE}$(du -h app/build/outputs/apk/release/app-release.apk | cut -f1)${NC}"
echo -e "Package: ${BLUE}com.${appname}.webtoapk${NC}"
echo -e "App name: ${BLUE}$(grep -o 'app_name">[^<]*' app/src/main/res/values/strings.xml | cut -d'>' -f2)${NC}"
echo -e "URL: ${BLUE}$(grep 'String mainURL' app/src/main/java/com/$appname/webtoapk/*.java | cut -d'"' -f2)${NC}"
echo -e "${BOLD}----------------${NC}"
else
error "Build failed"
fi
}
test() {
info "Detected app name: $appname"
try "adb install app/build/outputs/apk/release/app-release.apk"
try "adb logcat -c" # clean logs
try "adb shell am start -n com.$appname.webtoapk/.MainActivity"
echo "=========================="
adb logcat | grep -oP "(?<=WebToApk: ).*"
# adb logcat *:I | grep com.$appname.webtoapk
# https://stackoverflow.com/questions/29072501/how-to-unlock-android-phone-through-adb
# adb shell input keyevent 26 #Pressing the lock button
# sleep 1s
# adb shell input touchscreen swipe 930 880 930 380 #Swipe UP
}
keygen() {
if [ -f "app/my-release-key.jks" ]; then
warn "Keystore already exists"
read -p "Do you want to replace it? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
info "Cancelled"
return 1
fi
rm app/my-release-key.jks
fi
info "Generating keystore..."
try "keytool -genkey -v -keystore app/my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my -storepass '123456' -keypass '123456' -dname '$INFO'"
log "Keystore generated successfully"
}
clean() {
info "Cleaning build files..."
try rm -rf app/build .gradle
apply_config app/default.conf
log "Clean completed"
}
chid() {
[ -z "$1" ] && error "Please provide an application ID"
if ! [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9_]*$ ]]; then
error "Invalid application ID. Use only letters, numbers and underscores, start with a letter"
fi
try "find . -type f \( -name '*.gradle' -o -name '*.java' -o -name '*.xml' \) -exec \
sed -i 's/com\.\([a-zA-Z0-9_]*\)\.webtoapk/com.$1.webtoapk/g' {} +"
if [ "$1" = "$appname" ]; then
return 0
fi
info "Old name: com.$appname.webtoapk"
info "Renaming to: com.$1.webtoapk"
try "mv app/src/main/java/com/$appname app/src/main/java/com/$1"
appname=$1
log "Application ID changed successfully"
}
rename() {
local new_name="$*"
if [ -z "$new_name" ]; then
error "Please provide a display name\nUsage: $0 display_name \"My App Name\""
fi
# Найти все файлы strings.xml в различных языковых директориях
find app/src/main/res/values* -name "strings.xml" | while read xml_file; do
current_name=$(grep -o 'app_name">[^<]*' "$xml_file" | cut -d'>' -f2)
if [ "$current_name" = "$new_name" ]; then
continue
fi
escaped_name=$(echo "$new_name" | sed 's/[\/&]/\\&/g')
try sed -i "s|<string name=\"app_name\">[^<]*</string>|<string name=\"app_name\">$escaped_name</string>|" "$xml_file"
# Получаем код языка из пути файла
lang_code=$(echo "$xml_file" | grep -o 'values-[^/]*' | cut -d'-' -f2)
if [ -z "$lang_code" ]; then
lang_code="default"
fi
log "Display name changed to: $new_name (${lang_code})"
done
}
set_deep_link() {
local manifest_file="app/src/main/AndroidManifest.xml"
local hosts_input="$@"
local tmp_file
tmp_file=$(mktemp)
# First, create a version of the manifest without any VIEW/BROWSER intent-filter.
# This prepares a clean slate.
awk '
# Find any intent-filter block
/<intent-filter>/, /<\/intent-filter>/ {
# Buffer the lines of the block
buffer = buffer $0 ORS
# When the block ends...
if (/<\/intent-filter>/) {
# ...check if it is NOT the browser/deeplink one (by looking for action.VIEW).
if (buffer !~ /android.intent.action.VIEW/) {
# If it is the LAUNCHER filter, print it.
printf "%s", buffer
}
# Reset buffer for the next potential block.
buffer = ""
}
# Do not print the line yet.
next
}
# Print all other lines that are not in a VIEW intent-filter block.
{ print }
' "$manifest_file" > "$tmp_file"
# If hosts were provided, add the complete intent-filter block back in.
if [ -n "$hosts_input" ]; then
local new_tmp_file
new_tmp_file=$(mktemp)
# Use awk to insert the new block after the main launcher intent-filter.
awk -v hosts="$hosts_input" '
# After the first (launcher) intent-filter is closed...
/<\/intent-filter>/ && !inserted {
# ...print the closing tag first.
print
# Then print the new block for our deeplink.
print " <intent-filter>"
print " <action android:name=\"android.intent.action.VIEW\" />"
print " <category android:name=\"android.intent.category.DEFAULT\" />"
print " <category android:name=\"android.intent.category.BROWSABLE\" />"
print " <data android:scheme=\"http\" />"
print " <data android:scheme=\"https\" />"
# Split hosts by space and iterate to create data tags for each host
n = split(hosts, h_array, " ")
for (i = 1; i <= n; i++) {
if (h_array[i] != "") {
print " <data android:host=\"" h_array[i] "\" />"
}
}
print " </intent-filter>"
# Set a flag to ensure we only do this once.
inserted=1
next
}
# Print all other lines as usual.
{ print }
' "$tmp_file" > "$new_tmp_file"
# The final content is now in new_tmp_file.
mv "$new_tmp_file" "$tmp_file"
fi
# Apply changes only if the file is actually different.
if ! diff -q "$manifest_file" "$tmp_file" >/dev/null; then
if [ -z "$hosts_input" ]; then
log "Removing deeplink"
else
log "Setting deeplinks for: $hosts_input"
fi
try mv "$tmp_file" "$manifest_file"
else
rm "$tmp_file"
fi
}
set_network_security_config() {
local manifest_file="app/src/main/AndroidManifest.xml"
local config_attr='android:networkSecurityConfig="@xml/network_security_config"'
local enabled="$1"
local tmp_file
tmp_file=$(mktemp)
if [ "$enabled" = "true" ]; then
# Add config to the <application> tag if not present
if ! grep -q "networkSecurityConfig" "$manifest_file"; then
awk -v attr=" $config_attr" '
/<\s*application/ { in_app_tag = 1 }
in_app_tag && />/ {
sub(/>/, attr ">")
in_app_tag = 0
}
{ print }
' "$manifest_file" > "$tmp_file"
log "Enabling user CA support in AndroidManifest.xml"
try mv "$tmp_file" "$manifest_file"
else
rm -f "$tmp_file"
fi
else
# Remove config from the <application> tag if present
if grep -q "networkSecurityConfig" "$manifest_file"; then
sed "s# ${config_attr}##" "$manifest_file" > "$tmp_file"
log "Disabling user CA support in AndroidManifest.xml"
try mv "$tmp_file" "$manifest_file"
else
rm -f "$tmp_file"
fi
fi
}
set_icon() {
local icon_path="$@"
local default_icon="$PWD/app/example.png"
local dest_file="app/src/main/res/mipmap/ic_launcher.png"
# If no icon provided, use default
if [ -z "$icon_path" ]; then
icon_path="$default_icon"
fi
# If icon_path is not absolute, prepend CONFIG_DIR
if [ -n "${CONFIG_DIR:-}" ] && [[ "$icon_path" != /* ]]; then
icon_path="$CONFIG_DIR/$icon_path"
fi
# Validate icon
[ ! -f "$icon_path" ] && error "Icon file not found: $icon_path"
# Check if file is PNG
file_type=$(file -b --mime-type "$icon_path")
if [ "$file_type" != "image/png" ]; then
error "Icon must be in PNG format, got: $file_type"
fi
# Create destination directory if needed
mkdir -p "$(dirname "$dest_file")"
# Check if icon needs to be updated
if [ -f "$dest_file" ] && cmp -s "$icon_path" "$dest_file"; then
return 0
fi
if [ -z "$@" ]; then
warn "Using example.png for icon"
fi
# Copy icon
try "cp \"$icon_path\" \"$dest_file\""
log "Icon updated successfully"
}
set_userscripts() {
local scripts_dir="app/src/main/assets/userscripts"
# Create destination directory if it doesn't exist
mkdir -p "$scripts_dir"
# If no arguments provided, clean destination and exit
if [ $# -eq 0 ] || [ -z "$1" ]; then
if [ -n "$(ls -A $scripts_dir 2>/dev/null)" ]; then
find "$scripts_dir" -mindepth 1 -delete
log "Userscripts directory cleared"
fi
return 0
fi
# Track changes for reporting
local added=()
local updated=()
local removed=()
# Get a list of currently existing script basenames in the destination
local existing_scripts=()
# Use find to properly handle filenames with spaces
while IFS= read -r file; do
existing_scripts+=("$(basename "$file")")
done < <(find "$scripts_dir" -mindepth 1 -type f)
# Build a list of all source files from arguments
local source_files=()
for pattern in "$@"; do
# If CONFIG_DIR is defined and pattern is relative, prepend it
if [ -n "${CONFIG_DIR:-}" ] && [[ "$pattern" != /* ]]; then
pattern="$CONFIG_DIR/$pattern"
fi
# Use a nullglob to avoid errors if a pattern doesn't match any files
shopt -s nullglob
for file in $pattern; do
if [ -f "$file" ]; then
source_files+=("$file")
fi
done
shopt -u nullglob # Revert glob option
done
# Process source files: copy new/updated files and track which scripts should exist
local current_scripts=()
for src_file in "${source_files[@]}"; do
local base_name
base_name=$(basename "$src_file")
local dest_file="$scripts_dir/$base_name"
# Add basename to a list of scripts that are currently in config
current_scripts+=("$base_name")
if [ ! -f "$dest_file" ]; then
# New file
cp "$src_file" "$dest_file"
added+=("$base_name")
elif ! cmp -s "$src_file" "$dest_file"; then
# Changed file
cp "$src_file" "$dest_file"
updated+=("$base_name")
fi
done
# Determine which scripts to remove by comparing old and new lists
for script in "${existing_scripts[@]}"; do
is_current=false
for current in "${current_scripts[@]}"; do
if [[ "$script" == "$current" ]]; then
is_current=true
break
fi
done
# If a script existed before but is not in the new list, remove it
if ! $is_current; then
rm -f "$scripts_dir/$script"
removed+=("$script")
fi
done
# Report all changes
if [ ${#removed[@]} -gt 0 ]; then
for script in "${removed[@]}"; do
log "Removed userscript: $script"
done
fi
if [ ${#added[@]} -gt 0 ]; then
for script in "${added[@]}"; do
log "Added userscript: $script"
done
fi
if [ ${#updated[@]} -gt 0 ]; then
for script in "${updated[@]}"; do
log "Updated userscript: $script"
done
fi
# If no changes were made, stay silent
if [ ${#removed[@]} -eq 0 ] && [ ${#added[@]} -eq 0 ] && [ ${#updated[@]} -eq 0 ]; then
return 0
fi
}
update_permission() {
local permission_name="$1"
local enabled="$2"
local manifest_file="app/src/main/AndroidManifest.xml"
# Construct the full permission tag
local permission_tag="<uses-permission android:name=\"$permission_name\" />"
local tmp_file=$(mktemp)
if [ "$enabled" = "true" ]; then
# Add permission if not already present
if ! grep -q "$permission_name" "$manifest_file"; then
awk -v tag="$permission_tag" '
{
print $0
# Insert permission right after the opening manifest tag
if ($0 ~ /<manifest /) {
print " " tag
}
}' "$manifest_file" > "$tmp_file"
log "Added permission: $permission_name"
try mv "$tmp_file" "$manifest_file"
else
rm "$tmp_file"
fi
else
# Remove permission if present
if grep -q "$permission_name" "$manifest_file"; then
# Filter out lines containing the permission name
grep -v "$permission_name" "$manifest_file" > "$tmp_file"
log "Removed permission: $permission_name"
try mv "$tmp_file" "$manifest_file"
else
rm "$tmp_file"
fi
fi
}
get_tools() {
info "Downloading Android Command Line Tools..."
case "$(uname -s)" in
Linux*) os_type="linux";;
# Darwin*) os_type="mac";;
*) error "Unsupported OS";;
esac
tmp_dir=$(mktemp -d)
cd "$tmp_dir"
try "wget -q --show-progress 'https://dl.google.com/android/repository/commandlinetools-${os_type}-11076708_latest.zip' -O cmdline-tools.zip"
info "Extracting tools..."
try "unzip -q cmdline-tools.zip"
try "mkdir -p '$ANDROID_HOME/cmdline-tools/latest'"
try "mv cmdline-tools/* '$ANDROID_HOME/cmdline-tools/latest/'"
cd "$OLDPWD"
rm -rf "$tmp_dir"
info "Accepting licenses..."
try "yes | '$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager' --sdk_root=$ANDROID_HOME --licenses"
info "Installing necessary SDK components..."
try "'$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager' --sdk_root=$ANDROID_HOME \
'platform-tools' \
'platforms;android-33' \
'build-tools;33.0.2'"
log "Android SDK successfully installed!"
}
regradle() {
info "Reinstalling Gradle..."
try rm -rf gradle gradlew .gradle .gradle-cache
try mkdir -p gradle/wrapper
cat > gradle/wrapper/gradle-wrapper.properties << EOL
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
EOL
try wget -q --show-progress https://raw.githubusercontent.com/gradle/gradle/v7.4.0/gradle/wrapper/gradle-wrapper.jar -O gradle/wrapper/gradle-wrapper.jar
try wget -q --show-progress https://raw.githubusercontent.com/gradle/gradle/v7.4.0/gradlew -O gradlew
try chmod +x gradlew
log "Gradle reinstalled successfully"
}
get_java() {
local install_dir="$PWD/jvm"
local jdk_version="17.0.2"
local jdk_hash="0022753d0cceecacdd3a795dd4cea2bd7ffdf9dc06e22ffd1be98411742fbb44"
local jdk_url="https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz"
if [ -d "$install_dir/jdk-${jdk_version}" ]; then
info "OpenJDK ${jdk_version} already downloaded"
export JAVA_HOME="$install_dir/jdk-${jdk_version}"
export PATH="$JAVA_HOME/bin:$PATH"
return 0
fi
local tmp_dir=$(mktemp -d)
cd "$tmp_dir"
info "Downloading OpenJDK ${jdk_version}..."
try "wget -q --show-progress '$jdk_url' -O openjdk.tar.gz"
info "Verifying checksum..."
try "echo '${jdk_hash} openjdk.tar.gz' | sha256sum -c -"
info "Unpacking to ${install_dir}..."
try "mkdir -p '$install_dir'"
try "tar xf openjdk.tar.gz"
try "mv jdk-${jdk_version} '$install_dir/'"
cd "$OLDPWD"
rm -rf "$tmp_dir"
export JAVA_HOME="$install_dir/jdk-${jdk_version}"
export PATH="$JAVA_HOME/bin:$PATH"
log "OpenJDK ${jdk_version} downloaded successfully!"
}
# Check Java version and update JAVA_HOME if needed
check_and_find_java() {
# First check existing JAVA_HOME
if [ -n "${JAVA_HOME:-}" ] && [ -x "$JAVA_HOME/bin/java" ]; then
version=$("$JAVA_HOME/bin/java" -version 2>&1 | head -n 1 | cut -d'"' -f2 | cut -d'.' -f1)
if [ "$version" = "17" ]; then
info "Using system JAVA_HOME: $JAVA_HOME"
export PATH="$JAVA_HOME/bin:$PATH"
return 0
else
warn "Current JAVA_HOME points to wrong version: $version"
fi
fi
# Then check local installation
if [ -d "$PWD/jvm/jdk-17.0.2" ]; then
info "Using local Java installation"
export JAVA_HOME="$PWD/jvm/jdk-17.0.2"
export PATH="$JAVA_HOME/bin:$PATH"
return 0
fi
# Finally check /usr/lib/jvm
if [ -d "/usr/lib/jvm" ]; then
while IFS= read -r java_path; do
if [ -x "$java_path/bin/java" ]; then
version=$("$java_path/bin/java" -version 2>&1 | head -n 1 | cut -d'"' -f2 | cut -d'.' -f1)
if [ "$version" = "17" ]; then
info "Found system Java 17: $java_path"
export JAVA_HOME="$java_path"
export PATH="$JAVA_HOME/bin:$PATH"
return 0
fi
fi
done < <(find /usr/lib/jvm -maxdepth 1 -type d)
fi
# No suitable Java found
return 1
}
build() {
local config_arg="${1-}"
if [ -n "$config_arg" ] && [ -d "$config_arg" ]; then
config_arg="$config_arg/webapk.conf"
fi
apply_config "$config_arg"
apk
}
###############################################################################
ORIGINAL_PWD="$PWD"
# Change directory to the directory where make.sh resides (project root)
try cd "$(dirname "$0")"
export ANDROID_HOME=$PWD/cmdline-tools/
appname=$(grep -Po '(?<=applicationId "com\.)[^.]*' app/build.gradle)
# Set Gradle's cache directory to be local to the project
export GRADLE_USER_HOME=$PWD/.gradle-cache
command -v wget >/dev/null 2>&1 || error "wget not found. Please install wget"
# Try to find Java 17
if ! check_and_find_java; then
warn "Java 17 not found"
read -p "Would you like to download OpenJDK 17 to ./jvm? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
get_java
if ! command -v java >/dev/null 2>&1; then
error "Java installation failed"
fi
else
error "Java 17 is required"
fi
fi
# Final verification
java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2 | cut -d'.' -f1)
if [ "$java_version" != "17" ]; then
error "Wrong Java version: $java_version. Java 17 is required"
fi
command -v adb >/dev/null 2>&1 || warn "adb not found. './make.sh try' will not work"
if [ ! -d "$ANDROID_HOME" ]; then
warn "Android Command Line Tools not found: ./cmdline-tools"
read -p "Do you want to download them now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
get_tools
else
error "Cannot continue without Android Command Line Tools"
fi
fi
if [ $# -eq 0 ]; then
echo -e "${BOLD}Usage:${NC}"
echo -e " ${BLUE}$0 keygen${NC} - Generate signing key"
echo -e " ${BLUE}$0 build${NC} [config] - Apply configuration and build"
echo -e " ${BLUE}$0 test${NC} - Install and test APK via adb, show logs"
echo -e " ${BLUE}$0 clean${NC} - Clean build files, reset settings"
echo
echo -e " ${BLUE}$0 apk${NC} - Build APK without apply_config"
echo -e " ${BLUE}$0 apply_config${NC} - Apply settings from config file"
echo -e " ${BLUE}$0 get_java${NC} - Download OpenJDK 17 locally"
echo -e " ${BLUE}$0 regradle${NC} - Reinstall gradle. You don't need it"
exit 1
fi
eval $@