-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_extensions.v
845 lines (775 loc) · 27.9 KB
/
package_extensions.v
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
/*
MIT License
Copyright (c) 2020-2022 Lars Pontoppidan
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.
*/
import os
import regex
import semver
import x.json2
import vab.java
import vab.android
import vab.android.sdk
import vab.android.util
// package outputs one of the supported Android package formats based on
// `PackageOptions`
pub fn vab_extension_package(opt android.PackageOptions) ! {
error_tag := @MOD + '.' + @FN
if opt.verbosity > 0 {
println('Preparing package "${opt.package_id}"...')
}
// Validate package_id to our best effort
android.is_valid_package_id(opt.package_id) or {
return error('${error_tag}: "${opt.package_id}" is not a valid package id:\n${err}.
Please consult the Android documentation for details:
https://developer.android.com/studio/build/application-id')
}
if opt.is_prod && opt.package_id == android.default_package_id {
return error('${error_tag}: Package id "${opt.package_id}" is used by the V team.
Please do not deploy to app stores using package id "${android.default_package_id}".')
}
// Build APK
match opt.format {
.apk {
vab_extension_package_apk(opt)!
}
.aab {
error('ASAPP does not support the AAB format yet')
}
}
}
// package_apk ouputs an Android .apk package file based on the `PackageOptions`.
fn vab_extension_package_apk(opt android.PackageOptions) ! {
error_tag := @MOD + '.' + @FN
pwd := os.getwd()
build_path := os.join_path(opt.work_dir, 'build')
artifacts_path := os.join_path(opt.work_dir, 'artifacts')
libs_extra_path := os.join_path(opt.work_dir, 'libs')
build_tools_path := os.join_path(sdk.build_tools_root(), opt.build_tools)
// Remove any previous extra libs
if os.is_dir(libs_extra_path) {
os.rmdir_all(libs_extra_path) or {
return error('${error_tag}: could not remove "${libs_extra_path}":\n${err}')
}
}
for supported_lib_folder in android.supported_lib_folders {
supported_lib_folder_path := os.join_path(libs_extra_path, 'lib', supported_lib_folder)
os.mkdir_all(supported_lib_folder_path) or {
return error('${error_tag}: error while making directory "${supported_lib_folder_path}":\n${err}')
}
}
// Remove any previous artifacts libs
os.rmdir_all(artifacts_path) or {}
os.mkdir_all(artifacts_path) or {
return error('${error_tag}: error while making directory "${artifacts_path}":\n${err}')
}
// Used for various bug workarounds below
build_tools_semantic_version := semver.from(opt.build_tools) or {
return error('${error_tag}:' + @LINE +
' error converting build-tools version "${opt.build_tools}" to semantic version.\nsemver: ${err}')
}
// Used for various bug workarounds below
jdk_semantic_version := semver.from(java.jdk_version()) or {
return error('${error_tag}:' + @LINE +
' error converting jdk_version "${java.jdk_version()}" to semantic version.\nsemver: ${err}')
}
javac := os.join_path(java.jdk_bin_path(), 'javac')
aapt := os.join_path(build_tools_path, 'aapt')
mut dx := os.join_path(build_tools_path, 'dx')
mut d8 := os.join_path(build_tools_path, 'd8') // Not available prior to build-tools v28.0.1
$if windows {
dx += '.bat'
d8 += '.bat'
}
zipalign := os.join_path(build_tools_path, 'zipalign')
mut apksigner := os.join_path(build_tools_path, 'apksigner')
$if windows {
apksigner += '.bat'
}
// Prepare and modify package skeleton shipped with vab
// Copy assets etc.
package_path, assets_path := vab_extension_prepare_base(opt)
output_fn := os.file_name(opt.output_file).replace(os.file_ext(opt.output_file), '')
tmp_product := os.join_path(opt.work_dir, '${output_fn}.apk')
tmp_unsigned_product := os.join_path(opt.work_dir, '${output_fn}.unsigned.apk')
tmp_unaligned_product := os.join_path(opt.work_dir, '${output_fn}.unaligned.apk')
os.rm(tmp_product) or {}
os.rm(tmp_unsigned_product) or {}
os.rm(tmp_unaligned_product) or {}
android_runtime := os.join_path(sdk.platforms_root(), 'android-' + opt.api_level,
'android.jar')
src_path := os.join_path(package_path, 'src')
res_path := os.join_path(package_path, 'res')
obj_path := os.join_path(package_path, 'obj')
os.mkdir_all(obj_path) or {
return error('${error_tag}: error while making directory "${obj_path}":\n${err}')
}
bin_path := os.join_path(package_path, 'bin')
os.mkdir_all(bin_path) or {
return error('${error_tag}: error while making directory "${bin_path}":\n${err}')
}
mut aapt_cmd := [
aapt,
'package',
'-v',
'-f',
'-m',
'-M "' + os.join_path(package_path, 'AndroidManifest.xml') + '"',
'-S "' + res_path + '"',
'-J "' + src_path + '"',
'-A "' + assets_path + '"',
'-I "' + android_runtime + '"',
// TODO use '--target-sdk-version ${ANDROIDTARGET}' ?
]
util.verbosity_print_cmd(aapt_cmd, opt.verbosity)
util.run_or_error(aapt_cmd)!
os.chdir(package_path) or {
return error('${error_tag}: error while changing work directory to "${package_path}":\n${err}')
}
mut javac_source_version := '1.7'
mut javac_target_version := '1.7'
if jdk_semantic_version >= semver.build(20, 0, 0) {
javac_source_version = '1.8'
javac_target_version = '1.8'
}
// Compile java sources
if opt.verbosity > 1 {
println('Compiling java sources ${javac_source_version}/${javac_target_version}')
}
java_sources := os.walk_ext(os.join_path(package_path, 'src'), '.java')
mut javac_cmd := [
javac,
'-d obj',
// NOTE `+obj_path` can be specified
'-source ${javac_source_version}',
'-target ${javac_target_version}',
'-classpath .',
'-sourcepath src',
'-bootclasspath "' + android_runtime + '"',
]
javac_cmd << java_sources
util.verbosity_print_cmd(javac_cmd, opt.verbosity)
util.run_or_error(javac_cmd)!
$if windows {
// TODO Workaround dx and Java > 8 (1.8.0) BUG
// Error message we are trying to prevent:
// -Djava.ext.dirs=C:<path>lib is not supported. Use -classpath instead.
if jdk_semantic_version > semver.build(1, 8, 0) {
if os.exists(dx) {
mut patched_dx := os.join_path(os.dir(dx), os.file_name(dx).all_before_last('.') +
'_patched.bat')
if !os.exists(patched_dx) {
mut dx_contents := os.read_file(dx) or { '' }
if dx_contents != '' && dx_contents.contains('-Djava.ext.dirs=') {
dx_contents = dx_contents.replace_once('-Djava.ext.dirs=', '-classpath ')
os.write_file(patched_dx, dx_contents) or { patched_dx = dx }
} else {
patched_dx = dx
}
if opt.verbosity > 1 {
println('Using patched dx (${patched_dx})')
}
}
dx = patched_dx
}
if os.exists(d8) {
mut patched_d8 := os.join_path(os.dir(d8), os.file_name(d8).all_before_last('.') +
'_patched.bat')
if !os.exists(patched_d8) {
mut d8_contents := os.read_file(d8) or { '' }
if d8_contents != '' && d8_contents.contains('-Djava.ext.dirs=') {
d8_contents = d8_contents.replace_once('-Djava.ext.dirs=', '-classpath ')
os.write_file(patched_d8, d8_contents) or { patched_d8 = d8 }
} else {
patched_d8 = d8
}
if opt.verbosity > 1 {
println('Using patched d8 (${patched_d8})')
}
}
d8 = patched_d8
}
}
// Workaround END
}
// Dex either with `dx` or `d8`
if build_tools_semantic_version >= semver.build(28, 0, 1) {
mut class_files := os.walk_ext('obj', '.class')
class_files = class_files.map(fn (e string) string {
$if windows {
return '"${e}"'
}
return "'${e}'" // NOTE inclosing with ' is important since the files can contain `$` chars
})
mut dex_type := if opt.is_prod { '--release' } else { '--debug' }
mut d8_cmd := [
d8,
'--min-api ${opt.min_sdk_version}',
dex_type,
'--lib "' + android_runtime + '"',
'--classpath obj',
'--output ' + 'classes.zip',
]
d8_cmd << class_files
util.verbosity_print_cmd(d8_cmd, opt.verbosity)
util.run_or_error(d8_cmd)!
util.unzip('classes.zip', 'bin') or {
return error('${error_tag}:' + @LINE + ' error unzipping classes.zip to bin: ${err}')
}
os.cp('classes.zip', os.join_path(artifacts_path, 'classes.zip')) or {
return error(err.msg())
}
os.rm('classes.zip') or { return error(err.msg()) }
} else {
dx_cmd := [
dx,
'--verbose',
'--dex',
'--output=' + os.join_path('bin', 'classes.dex'),
'obj',
// TODO specify obj_path ?
]
util.verbosity_print_cmd(dx_cmd, opt.verbosity)
util.run_or_error(dx_cmd)!
}
/*
$if !windows {
// TODO
if opt.verbosity > 2 {
dexdump_cmd := [
os.join_path(build_tools_path, 'dexdump'),
'dexdump',
'-d',
os.join_path('bin', 'classes.dex'),
]
util.verbosity_print_cmd(dexdump_cmd, 3)
util.run_or_error(dexdump_cmd) !
}
}*/
// Second run
aapt_cmd = [
aapt,
'package',
'-v',
'-f',
'-S "' + res_path + '"',
'-M "' + os.join_path(package_path, 'AndroidManifest.xml') + '"',
'-A "' + assets_path + '"',
'-I "' + android_runtime + '"',
'-F "' + tmp_unaligned_product + '"',
'bin',
// TODO specify bin_path ?
]
util.verbosity_print_cmd(aapt_cmd, opt.verbosity)
util.run_or_error(aapt_cmd)!
if opt.verbosity > 1 {
println('Adding libs to "${tmp_unaligned_product}"...')
}
// Add libs to product
mut collected_libs := map[string][]string{}
collected_libs[build_path] = os.walk_ext(os.join_path(build_path, 'lib'), '.so')
for lib_extra_path in opt.libs_extra {
collected_extra_libs := os.walk_ext(os.join_path(lib_extra_path), '.so')
for collected_extra_lib in collected_extra_libs {
path_split := collected_extra_lib.split(os.path_separator)
for path_part in path_split {
if path_part in android.supported_lib_folders {
if opt.verbosity > 2 {
println('Adding extra lib "${collected_extra_lib}"...')
}
os.cp(collected_extra_lib, os.join_path(libs_extra_path, 'lib', path_part,
os.file_name(collected_extra_lib))) or { return error(err.msg()) }
break
}
}
}
}
collected_libs[libs_extra_path] = os.walk_ext(os.join_path(libs_extra_path, 'lib'),
'.so')
for lib_path, libs in collected_libs {
os.chdir(lib_path) or {}
for lib in libs {
mut lib_s := lib.replace(lib_path + os.path_separator, '')
$if windows {
// NOTE This is necessary for paths to work when packaging up on Windows
lib_s = lib_s.replace(os.path_separator, '/')
}
aapt_cmd = [
aapt,
'add',
'-v',
'"' + tmp_unaligned_product + '"',
'"' + lib_s + '"',
]
util.verbosity_print_cmd(aapt_cmd, opt.verbosity)
util.run_or_error(aapt_cmd)!
}
}
os.chdir(pwd) or {
return error('${error_tag}: error while changing work directory to "${pwd}":\n${err}')
}
zipalign_cmd := [
zipalign,
'-v',
'-f 4',
'"' + tmp_unaligned_product + '"',
'"' + tmp_unsigned_product + '"',
]
util.verbosity_print_cmd(zipalign_cmd, opt.verbosity)
util.run_or_error(zipalign_cmd)!
// Sign the APK
keystore := android.resolve_keystore(opt.keystore)!
if opt.is_prod && os.file_name(keystore.path) == 'debug.keystore' {
eprintln('Warning: It looks like you are using the debug.keystore file to sign your application built in production mode ("-prod").')
}
$if windows {
// TODO Workaround apksigner and Java > 8 (1.8.0) BUG
// Error message we are trying to prevent:
// -Djava.ext.dirs=C:<path>lib is not supported. Use -classpath instead.
if jdk_semantic_version > semver.build(1, 8, 0) && os.exists(apksigner) {
mut patched_apksigner := os.join_path(os.dir(apksigner),
os.file_name(apksigner).all_before_last('.') + '_patched.bat')
if !os.exists(patched_apksigner) {
mut contents := os.read_file(apksigner) or { '' }
if contents != '' && contents.contains('-Djava.ext.dirs=') {
contents = contents.replace_once('-Djava.ext.dirs=', '-classpath ')
os.write_file(patched_apksigner, contents) or { patched_apksigner = apksigner }
} else {
patched_apksigner = apksigner
}
if opt.verbosity > 1 {
println('Using patched apksigner (${patched_apksigner})')
}
}
apksigner = patched_apksigner
}
// Workaround END
}
mut apksigner_cmd := [
apksigner,
'sign',
'--ks "' + keystore.path + '"',
'--ks-pass pass:' + keystore.password,
'--ks-key-alias "' + keystore.alias + '"',
'--key-pass pass:' + keystore.alias_password,
'--out "' + tmp_product + '"',
'"' + tmp_unsigned_product + '"',
]
util.verbosity_print_cmd(apksigner_cmd, opt.verbosity)
util.run_or_error(apksigner_cmd)!
apksigner_cmd = [
apksigner,
'verify',
'-v',
'"' + tmp_product + '"',
]
util.verbosity_print_cmd(apksigner_cmd, opt.verbosity)
util.run_or_error(apksigner_cmd)!
if os.is_file(opt.output_file) {
if opt.verbosity > 1 {
println('Removing previous output "${opt.output_file}"')
}
os.rm(opt.output_file) or {
return error('${error_tag}: error while removing "${opt.output_file}":\n${err}')
}
}
if opt.verbosity > 1 {
println('Moving product from "${tmp_product}" to "${opt.output_file}"')
}
os.mv_by_cp(tmp_product, opt.output_file) or {
return error('${error_tag}: error while moving product "${tmp_product}" to "${opt.output_file}": ${err}')
}
}
fn vab_extension_prepare_base(opt android.PackageOptions) (string, string) {
format := match opt.format {
.apk {
'apk'
}
.aab {
'aab'
}
}
package_path := os.join_path(opt.work_dir, 'package', format)
if opt.verbosity > 0 {
println('Removing previous package directory "${package_path}"')
}
os.rmdir_all(package_path) or {}
os.mkdir_all(package_path) or { panic(err) }
base_files_path := opt.base_files
if os.is_dir(base_files_path) {
if opt.verbosity > 0 {
println('Copying base files from "${base_files_path}" to "${package_path}"')
if opt.verbosity > 2 {
os.walk(base_files_path, fn (entry string) {
println(entry)
})
}
}
os.cp_all(base_files_path, package_path, true) or { panic(err) }
}
// Figure out path overrides
// TODO overrides is currently based on heuristics - this should probably change to avoid accidental copying of huge data amounts
mut overrides_path := opt.overrides_path
if overrides_path == '' {
if os.is_dir(opt.input) {
if os.is_dir(os.join_path(opt.input, 'java')) {
overrides_path = os.join_path(opt.input, 'java')
}
} else {
if os.is_dir(os.join_path(os.dir(opt.input), 'java')) {
overrides_path = os.join_path(os.dir(opt.input), 'java')
}
}
}
mut is_override := false
if os.is_dir(overrides_path) {
if opt.verbosity > 0 {
println('Copying base file overrides from "${overrides_path}" to "${package_path}"')
if opt.verbosity > 2 {
os.walk(overrides_path, fn (entry string) {
println(entry)
})
}
}
os.cp_all(overrides_path, package_path, true) or { panic(err) }
is_override = true
} else {
if opt.verbosity > 2 {
println('No overrides found in "${overrides_path}"')
}
}
if opt.verbosity > 0 {
println('Modifying base files')
}
is_default_pkg_id := opt.package_id == android.default_package_id
if opt.is_prod && (is_default_pkg_id || opt.package_id.starts_with(android.default_package_id)) {
if opt.package_id.starts_with(android.default_package_id) {
panic('Do not deploy to app stores using the default V package id namespace "${android.default_package_id}"\nYou can set your own package ID with the --package-id flag')
} else {
panic('Do not deploy to app stores using the default V package id "${android.default_package_id}"\nYou can set your own package ID with the --package-id flag')
}
}
pkg_id_split := opt.package_id.split('.')
package_id_path := pkg_id_split.join(os.path_separator)
os.mkdir_all(os.join_path(package_path, 'src', package_id_path)) or { panic(err) }
default_pkg_id_split := android.default_package_id.split('.')
default_pkg_id_path := default_pkg_id_split.join(os.path_separator)
native_activity_path := os.join_path(package_path, 'src', default_pkg_id_path)
activity_file_name := android.default_activity_name + '.java'
native_activity_file := os.join_path(native_activity_path, activity_file_name)
$if debug {
eprintln('Native activity file: "${native_activity_file}"')
}
if os.is_file(native_activity_file) {
if opt.verbosity > 1 {
println('Modifying native activity "${native_activity_file}"')
}
mut java_src := os.read_file(native_activity_file) or { panic(err) }
if !is_override {
// Change package id in template
// r'.*package\s+(io.v.android).*'
mut re := regex.regex_opt(r'.*package\s+(' + android.default_package_id + r');') or {
panic(err)
}
mut start, _ := re.match_string(java_src)
// Set new package ID if found
if start >= 0 && re.groups.len > 0 {
if opt.verbosity > 1 {
r := java_src[re.groups[0]..re.groups[1]]
println('Replacing package id "${r}" with "${opt.package_id}"')
}
java_src = java_src[0..re.groups[0]] + opt.package_id +
java_src[re.groups[1]..java_src.len]
}
} else {
if opt.verbosity > 1 {
println('Skipping replacing package id since "${opt.package_id}" is user provided')
}
}
// Set lib_name
mut re := regex.regex_opt(r'.*loadLibrary.*"(.*)".*') or { panic(err) }
mut start, _ := re.match_string(java_src)
// Set new package ID if found
if start >= 0 && re.groups.len > 0 {
if opt.verbosity > 1 {
r := java_src[re.groups[0]..re.groups[1]]
println('Replacing init library "${r}" with "${opt.lib_name}"')
}
java_src = java_src[0..re.groups[0]] + opt.lib_name +
java_src[re.groups[1]..java_src.len]
}
os.write_file(os.join_path(package_path, 'src', package_id_path, activity_file_name),
java_src) or { panic(err) }
// Remove left-overs from vab's copied skeleton
if opt.package_id != android.default_package_id {
os.rm(native_activity_file) or { panic(err) }
mut v_default_package_id := default_pkg_id_split.clone()
for i := v_default_package_id.len - 1; i >= 0; i-- {
if os.is_dir_empty(os.join_path(package_path, 'src', v_default_package_id.join(os.path_separator))) {
if opt.verbosity > 1 {
p := os.join_path(package_path, 'src', v_default_package_id.join(os.path_separator))
println('Removing default left-over directory "${p}"')
}
os.rmdir_all(os.join_path(package_path, 'src', v_default_package_id.join(os.path_separator))) or {
panic(err)
}
}
v_default_package_id.pop()
}
}
}
// Replace in AndroidManifest.xml
if !is_override {
manifest_path := os.join_path(package_path, 'AndroidManifest.xml')
if os.is_file(manifest_path) {
if opt.verbosity > 1 {
println('Modifying manifest "${manifest_path}"')
}
mut manifest := os.read_file(manifest_path) or { panic(err) }
mut re := regex.regex_opt(r'.*<manifest\s.*\spackage\s*=\s*"(.+)".*>') or { panic(err) }
mut start, _ := re.match_string(manifest)
// Set package ID if found
if start >= 0 && re.groups.len > 0 {
if opt.verbosity > 1 {
r := manifest[re.groups[0]..re.groups[1]]
println('Replacing package id "${r}" with "${opt.package_id}"')
}
manifest = manifest[0..re.groups[0]] + opt.package_id +
manifest[re.groups[1]..manifest.len]
}
re = regex.regex_opt(r'.*<manifest\s.*\sandroid:versionCode\s*=\s*"(.+)".*>') or {
panic(err)
}
start, _ = re.match_string(manifest)
if start >= 0 && re.groups.len > 0 {
if opt.verbosity > 1 {
r := manifest[re.groups[0]..re.groups[1]]
println('Replacing version code "${r}" with "${opt.version_code}"')
}
manifest = manifest[0..re.groups[0]] + opt.version_code.str() +
manifest[re.groups[1]..manifest.len]
}
is_debug_build := ('-cg' in opt.v_flags)
re = regex.regex_opt(r'.*<application\s.*android:debuggable\s*=\s*"(.*)".*>') or {
panic(err)
}
start, _ = re.match_string(manifest)
// Set debuggable attribute if found
if start >= 0 && re.groups.len > 0 {
if opt.verbosity > 1 {
r := manifest[re.groups[0]..re.groups[1]]
println('Replacing debuggable "${r}" with "${is_debug_build}"')
}
manifest = manifest[0..re.groups[0]] + is_debug_build.str() +
manifest[re.groups[1]..manifest.len]
}
re = regex.regex_opt(r'.*\s+android:minSdkVersion\s*=\s*"(.*)".*') or { panic(err) }
start, _ = re.match_string(manifest)
// TODO figure out this absolute mess.
// When building with Android native it's recommended (even, sometimes, quite necessary) that minSdkVersion is equal to compiled sdk version :(
// Otherwise you have all kinds of cryptic errors when the app is started.
// Google Play, at the time of writing, requires to build against level 29 as a minimum (App will be rejected otherwise).
// OTOH According to e.g. http://android-doc.github.io/ndk/guides/stable_apis.html it *should* be safe
// that we specify 21 as a minimum API level and then live happily ever after...
// What a complete mess :(
if start >= 0 && re.groups.len > 0 {
if opt.verbosity > 1 {
r := manifest[re.groups[0]..re.groups[1]]
println('Replacing minimum SDK version "${r}" with "${opt.min_sdk_version}"')
}
manifest = manifest[0..re.groups[0]] + opt.min_sdk_version.str() +
manifest[re.groups[1]..manifest.len]
}
re = regex.regex_opt(r'.*\s+android:targetSdkVersion\s*=\s*"(.*)".*') or { panic(err) }
start, _ = re.match_string(manifest)
if start >= 0 && re.groups.len > 0 {
if opt.verbosity > 1 {
r := manifest[re.groups[0]..re.groups[1]]
println('Replacing target SDK version "${r}" with "${opt.api_level}"')
}
manifest = manifest[0..re.groups[0]] + opt.api_level +
manifest[re.groups[1]..manifest.len]
}
re = regex.regex_opt(r'.*uses-feature android:glEsVersion\s*=\s*"(.*)".*') or {
panic(err)
}
start, _ = re.match_string(manifest)
if start >= 0 && re.groups.len > 0 {
gles_version_hex := '0x000' + opt.gles_version.str() + '0000'
if opt.verbosity > 1 {
r := manifest[re.groups[0]..re.groups[1]]
println('Replacing declaration of OpenGL ES version "${r}" with "${gles_version_hex}"')
}
manifest = manifest[0..re.groups[0]] + gles_version_hex +
manifest[re.groups[1]..manifest.len]
}
re = regex.regex_opt(r'.*<activity\s.*android:name\s*=\s*"(.*)".*>') or { panic(err) }
start, _ = re.match_string(manifest)
if start >= 0 && re.groups.len > 0 {
fq_activity_name := opt.package_id + '.' + opt.activity_name
if opt.verbosity > 1 {
r := manifest[re.groups[0]..re.groups[1]]
println('Replacing activity name "${r}" with "${fq_activity_name}"')
}
manifest = manifest[0..re.groups[0]] + fq_activity_name +
manifest[re.groups[1]..manifest.len]
}
if os.exists('asapp_manifest.json') {
asapp_manifest := json2.raw_decode(os.read_file('asapp_manifest.json') or {
panic(err)
}) or { panic(err) }.as_map()
if 'permissions' in asapp_manifest {
mut lines := manifest.split_into_lines()
mut line_number := 1
for line in lines {
if line.contains('uses-feature android:glEsVersion') {
break
}
line_number++
}
for permission in asapp_manifest['permissions'] or { panic('Unreachable') }.arr() {
lines.insert(line_number, '<uses-permission android:name="android.permission.${permission}" />')
line_number++
}
manifest = lines.join('\n')
}
}
os.write_file(manifest_path, manifest) or { panic(err) }
}
}
// Replace in res/values/strings.xml
strings_path := os.join_path(package_path, 'res', 'values', 'strings.xml')
if os.is_file(strings_path) {
mut content := os.read_file(strings_path) or { panic(err) }
mut re := regex.regex_opt(r'.*<resources>.*<string\s*name\s*=\s*"v_app_name"\s*>(.*)</string.*') or {
panic(err)
}
mut start, _ := re.match_string(content)
// Set app name if found
if start >= 0 && re.groups.len > 0 {
content = content[0..re.groups[0]] + opt.app_name + content[re.groups[1]..content.len]
}
// Set lib name if found
re = regex.regex_opt(r'.*<resources>.*<string\s*name\s*=\s*"v_lib_name"\s*>(.*)</string.*') or {
panic(err)
}
start, _ = re.match_string(content)
if start >= 0 && re.groups.len > 0 {
content = content[0..re.groups[0]] + opt.lib_name + content[re.groups[1]..content.len]
}
// Set package ID if found
re = regex.regex_opt(r'.*<resources>.*<string\s*name\s*=\s*"v_package_name"\s*>(.*)</string.*') or {
panic(err)
}
start, _ = re.match_string(content)
if start >= 0 && re.groups.len > 0 {
content = content[0..re.groups[0]] + opt.package_id + content[re.groups[1]..content.len]
}
os.write_file(strings_path, content) or { panic(err) }
}
if opt.verbosity > 0 {
println('Copying assets')
}
if !is_default_pkg_id && os.is_file(opt.icon) && os.file_ext(opt.icon) == '.png' {
icon_path := os.join_path(package_path, 'res', 'mipmap', 'icon.png')
if opt.verbosity > 0 {
println('Copying icon')
}
os.rm(icon_path) or { panic(err) }
os.cp(opt.icon, icon_path) or { panic(err) }
}
assets_path := os.join_path(package_path, 'assets')
os.mkdir_all(assets_path) or { panic(err) }
mut included_asset_paths := []string{}
/*
NOTE kept for debugging purposes
test_asset := os.join_path(assets_path, 'test.txt')
os.rm(test_asset)
mut fh := open_file(test_asset, 'w+', 0o755) or { panic(err) }
fh.write('test')
fh.close()
*/
mut assets_by_side_path := opt.input
if os.is_file(opt.input) {
assets_by_side_path = os.dir(opt.input)
}
// Look for "assets" dir in same location as input
assets_by_side := os.join_path(assets_by_side_path, 'assets')
if os.is_dir(assets_by_side) {
if opt.verbosity > 0 {
println('Including assets from "${assets_by_side}"')
}
os.cp_all(assets_by_side, assets_path, false) or { panic(err) }
included_asset_paths << os.real_path(assets_by_side)
}
// Look for "assets" in dir above input dir.
// This is mostly an exception for the shared example assets in V examples.
if os.real_path(assets_by_side_path).contains(os.join_path('v', 'examples')) {
assets_above := os.real_path(os.join_path(assets_by_side_path, '..', 'assets'))
if os.is_dir(assets_above) {
if os.real_path(assets_above) in included_asset_paths {
if opt.verbosity > 1 {
println('Skipping "${assets_above}" since it\'s already included')
}
} else {
if opt.verbosity > 0 {
println('Including assets from "${assets_above}"')
}
os.cp_all(assets_above, assets_path, false) or { panic(err) }
included_asset_paths << os.real_path(assets_by_side)
}
}
}
// Look for "assets" dir in current dir
assets_in_dir := 'assets'
if os.is_dir(assets_in_dir) {
assets_in_dir_resolved := os.real_path(os.join_path(os.getwd(), assets_in_dir))
if assets_in_dir_resolved in included_asset_paths {
if opt.verbosity > 1 {
println('Skipping "${assets_in_dir}" since it\'s already included')
}
} else {
if opt.verbosity > 0 {
println('Including assets from "${assets_in_dir}"')
}
os.cp_all(assets_in_dir, assets_path, false) or { panic(err) }
included_asset_paths << assets_in_dir_resolved
}
}
// Look in user provided dir
for user_asset in opt.assets_extra {
if os.is_dir(user_asset) {
user_asset_resolved := os.real_path(user_asset)
if user_asset_resolved in included_asset_paths {
if opt.verbosity > 1 {
println('Skipping "${user_asset}" since it\'s already included')
}
} else {
if opt.verbosity > 0 {
println('Including assets from "${user_asset}"')
}
os.cp_all(user_asset, assets_path, false) or { panic(err) }
included_asset_paths << user_asset_resolved
}
} else {
os.cp(user_asset, assets_path) or {
eprintln('Skipping invalid or non-existent asset file "${user_asset}"')
}
}
}
return package_path, assets_path
}