forked from ikskuh/ZigAndroidTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sdk.zig
1488 lines (1298 loc) · 50.5 KB
/
Sdk.zig
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! External dependencies:
//! - `keytool` from OpenJDK
//! - `apksigner`, `aapt`, `zipalign`, and `adb` from the Android tools package
const std = @import("std");
const builtin = @import("builtin");
const auto_detect = @import("build/auto-detect.zig");
fn sdkRootIntern() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
fn sdkRoot() *const [sdkRootIntern().len]u8 {
comptime var buffer = sdkRootIntern();
return buffer[0..buffer.len];
}
// linux-x86_64
pub fn toolchainHostTag() []const u8 {
const os = builtin.os.tag;
const arch = builtin.cpu.arch;
if (os == .macos and arch == .aarch64) {
// Android NDK does not support Apple Silicon, and must be ran with Rosetta, JFC!
return "darwin-x86_64";
}
return @tagName(os) ++ "-" ++ @tagName(arch);
}
/// This file encodes a instance of an Android SDK interface.
const Sdk = @This();
/// The builder instance associated with this object.
b: *Builder,
/// A set of tools that run on the build host that are required to complete the
/// project build. Must be created with the `hostTools()` function that passes in
/// the correct relpath to the package.
host_tools: HostTools,
/// The configuration for all non-shipped system tools.
/// Contains the normal default config for each tool.
system_tools: SystemTools = .{},
/// Contains paths to each required input folder.
folders: UserConfig,
versions: ToolchainVersions,
launch_using: ADBLaunchMethod = .monkey,
pub const ADBLaunchMethod = enum {
monkey,
am,
};
/// Initializes the android SDK.
/// It requires some input on which versions of the tool chains should be used
pub fn init(b: *Builder, user_config: ?UserConfig, toolchains: ToolchainVersions) *Sdk {
const actual_user_config = user_config orelse auto_detect.findUserConfig(b, toolchains) catch |err| @panic(@errorName(err));
const system_tools = blk: {
const exe = if (builtin.os.tag == .windows) ".exe" else "";
const bat = if (builtin.os.tag == .windows) ".bat" else "";
const zipalign = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "build-tools", toolchains.build_tools_version, "zipalign" ++ exe }) catch unreachable;
const aapt = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "build-tools", toolchains.build_tools_version, "aapt" ++ exe }) catch unreachable;
const d8 = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "build-tools", toolchains.build_tools_version, "d8" ++ exe }) catch unreachable;
const adb = blk1: {
const adb_sdk = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "platform-tools", "adb" ++ exe }) catch unreachable;
if (!auto_detect.fileExists(adb_sdk)) {
break :blk1 auto_detect.findProgramPath(b.allocator, "adb") orelse @panic("No adb found");
}
break :blk1 adb_sdk;
};
const apksigner = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "build-tools", toolchains.build_tools_version, "apksigner" ++ bat }) catch unreachable;
const keytool = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.java_home, "bin", "keytool" ++ exe }) catch unreachable;
const javac = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.java_home, "bin", "javac" ++ exe }) catch unreachable;
break :blk SystemTools{
.zipalign = zipalign,
.aapt = aapt,
.adb = adb,
.apksigner = apksigner,
.keytool = keytool,
.javac = javac,
.d8 = d8,
};
};
// Compiles all required additional tools for toolchain.
const host_tools = blk: {
const zip_add = b.addExecutable(.{
.name = "zip_add",
.root_source_file = .{ .path = sdkRoot() ++ "/tools/zip_add.zig" },
});
zip_add.addCSourceFile(.{
.file = .{ .path = sdkRoot() ++ "/vendor/kuba-zip/zip.c" },
.flags = &[_][]const u8{
"-std=c99",
"-fno-sanitize=undefined",
"-D_POSIX_C_SOURCE=200112L",
},
});
zip_add.addIncludePath(.{ .path = sdkRoot() ++ "/vendor/kuba-zip" });
zip_add.linkLibC();
break :blk HostTools{
.zip_add = zip_add,
};
};
const sdk = b.allocator.create(Sdk) catch @panic("out of memory");
sdk.* = Sdk{
.b = b,
.host_tools = host_tools,
.system_tools = system_tools,
.folders = actual_user_config,
.versions = toolchains,
};
return sdk;
}
pub const ToolchainVersions = struct {
build_tools_version: []const u8 = "33.0.1",
ndk_version: []const u8 = "24.0.8215888",
};
pub const AndroidVersion = enum(u16) {
android4 = 19, // KitKat
android5 = 21, // Lollipop
android6 = 23, // Marshmallow
android7 = 24, // Nougat
android8 = 26, // Oreo
android9 = 28, // Pie
android10 = 29, // Quince Tart
android11 = 30, // Red Velvet Cake
android12 = 31, // Snow Cone
android13 = 33, // Tiramisu
_, // we allow to overwrite the defaults
};
pub const UserConfig = struct {
android_sdk_root: []const u8 = "",
android_ndk_root: []const u8 = "",
java_home: []const u8 = "",
};
/// Configuration of the Android toolchain.
pub const Config = struct {
/// Path to the SDK root folder.
/// Example: `/home/ziggy/android-sdk`.
sdk_root: []const u8,
/// Path to the NDK root folder.
/// Example: `/home/ziggy/android-sdk/ndk/21.1.6352462`.
ndk_root: []const u8,
/// Path to the build tools folder.
/// Example: `/home/ziggy/android-sdk/build-tools/28.0.3`.
build_tools: []const u8,
/// A key store. This is required when an APK is created and signed.
/// If you don't care for production code, just use the default here
/// and it will work. This needs to be changed to a *proper* key store
/// when you want to publish the app.
key_store: KeyStore = KeyStore{
.file = "zig-cache/",
.alias = "default",
.password = "ziguana",
},
};
/// A resource that will be packed into the appliation.
pub const Resource = struct {
/// This is the relative path to the resource root
path: []const u8,
/// This is the content of the file.
content: std.build.FileSource,
};
/// Configuration of an application.
pub const AppConfig = struct {
/// The display name of the application. This is shown to the users.
display_name: []const u8,
/// Application name, only lower case letters and underscores are allowed.
app_name: []const u8,
/// Java package name, usually the reverse top level domain + app name.
/// Only lower case letters, dots and underscores are allowed.
package_name: []const u8,
/// The android version which is embedded in the manifset.
/// The default is Android 9, it's more than 4 years old by now and should be widespread enough to be a reasonable default.
target_version: AndroidVersion = .android9,
/// The resource directory that will contain the manifest and other app resources.
/// This should be a distinct directory per app.
resources: []const Resource = &[_]Resource{},
/// If true, the app will be started in "fullscreen" mode, this means that
/// navigation buttons as well as the top bar are not shown.
/// This is usually relevant for games.
fullscreen: bool = false,
/// One or more asset directories. Each directory will be added into the app assets.
asset_directories: []const []const u8 = &[_][]const u8{},
permissions: []const []const u8 = &[_][]const u8{
//"android.permission.SET_RELEASE_APP",
//"android.permission.RECORD_AUDIO",
},
libraries: []const []const u8 = &app_libs,
};
/// One of the legal targets android can be built for.
pub const Target = enum {
aarch64,
arm,
x86,
x86_64,
};
pub const KeyStore = struct {
file: []const u8,
alias: []const u8,
password: []const u8,
};
pub const HostTools = struct {
zip_add: *std.build.LibExeObjStep,
};
/// Configuration of the binary paths to all tools that are not included in the android SDK.
pub const SystemTools = struct {
mkdir: []const u8 = "mkdir",
rm: []const u8 = "rm",
zipalign: []const u8 = "zipalign",
aapt: []const u8 = "aapt",
adb: []const u8 = "adb",
apksigner: []const u8 = "apksigner",
keytool: []const u8 = "keytool",
javac: []const u8 = "javac",
d8: []const u8 = "d8",
};
/// The configuration which targets a app should be built for.
pub const AppTargetConfig = struct {
aarch64: ?bool = null,
arm: ?bool = null,
x86_64: ?bool = null,
x86: ?bool = null,
};
pub const CreateAppStep = struct {
sdk: *Sdk,
first_step: *std.build.Step,
final_step: *std.build.Step,
libraries: []const *std.build.LibExeObjStep,
build_options: *BuildOptionStep,
apk_file: std.build.FileSource,
package_name: []const u8,
pub fn getAndroidPackage(self: @This(), name: []const u8) std.build.Pkg {
return self.sdk.b.dupePkg(std.build.Pkg{
.name = name,
.source = .{ .path = sdkRoot() ++ "/src/android-support.zig" },
.dependencies = &[_]std.build.Pkg{
self.build_options.getPackage("build_options"),
},
});
}
pub fn install(self: @This()) *Step {
return self.sdk.installApp(self.apk_file);
}
pub fn run(self: @This()) *Step {
return self.sdk.startApp(self.package_name);
}
};
const NdkVersionRange = struct {
ndk: []const u8,
min: u16,
max: u16,
pub fn validate(range: []const NdkVersionRange, ndk: []const u8, api: u16) void {
const ndk_version = std.SemanticVersion.parse(ndk) catch {
std.debug.print("Could not parse NDK version {s} as semantic version. Could not perform NDK validation!\n", .{ndk});
return;
};
std.debug.assert(range.len > 0);
for (range) |vers| {
const r_version = std.SemanticVersion.parse(vers.ndk) catch unreachable;
if (ndk_version.order(r_version) == .eq) {
// Perfect version match
if (api < vers.min) {
std.debug.print("WARNING: Selected NDK {s} does not support api level {d}. Minimum supported version is {d}!\n", .{
ndk,
api,
vers.min,
});
}
if (api > vers.max) {
std.debug.print("WARNING: Selected NDK {s} does not support api level {d}. Maximum supported version is {d}!\n", .{
ndk,
api,
vers.max,
});
}
}
return;
}
// NDK old X => min=5, max=8
// NDK now Y => api=7
// NDK future Z => min=6, max=13
var older_version: NdkVersionRange = range[0]; // biggest Y <= X
for (range[1..]) |vers| {
const r_version = std.SemanticVersion.parse(vers.ndk) catch unreachable;
if (r_version.order(ndk_version) != .gt) { // r_version <= ndk_version
older_version = vers;
} else {
// range is ordered, so we know that we can't find anything smaller now anyways
break;
}
}
var newer_version: NdkVersionRange = range[range.len - 1]; // smallest Z >= X
for (range[1..]) |vers| {
const r_version = std.SemanticVersion.parse(vers.ndk) catch unreachable;
if (r_version.order(ndk_version) != .lt) {
newer_version = vers;
break;
}
}
// take for max api, as we assume that an older NDK than Z might not support Z.max yet
if (api < newer_version.min) {
std.debug.print("WARNING: Selected NDK {s} might not support api level {d}. Minimum supported version is guessed as {d}, as NDK {s} only supports that!\n", .{
ndk,
api,
newer_version.min,
newer_version.ndk,
});
}
// take for min api, as we assume that a newer NDK than X might not support X.min anymore
if (api > older_version.max) {
std.debug.print("WARNING: Selected NDK {s} might not support api level {d}. Maximum supported version is guessed as {d}, as NDK {s} only supports that!\n", .{
ndk,
api,
older_version.max,
older_version.ndk,
});
}
}
};
// ls ~/software/android-sdk/ndk/*/toolchains/llvm/prebuilt/${hosttag}/sysroot/usr/lib/arm-linux-androideabi | code
const arm_ndk_ranges = [_]NdkVersionRange{
NdkVersionRange{ .ndk = "19.2.5345600", .min = 16, .max = 28 },
NdkVersionRange{ .ndk = "20.1.5948944", .min = 16, .max = 29 },
NdkVersionRange{ .ndk = "21.4.7075529", .min = 16, .max = 30 },
NdkVersionRange{ .ndk = "22.1.7171670", .min = 16, .max = 30 },
NdkVersionRange{ .ndk = "23.2.8568313", .min = 16, .max = 31 },
NdkVersionRange{ .ndk = "24.0.8215888", .min = 19, .max = 32 },
NdkVersionRange{ .ndk = "25.1.8937393", .min = 19, .max = 33 },
};
// ls ~/software/android-sdk/ndk/*/toolchains/llvm/prebuilt/${hosttag}/sysroot/usr/lib/i686* | code
const i686_ndk_ranges = [_]NdkVersionRange{
NdkVersionRange{ .ndk = "19.2.5345600", .min = 16, .max = 28 },
NdkVersionRange{ .ndk = "20.1.5948944", .min = 16, .max = 29 },
NdkVersionRange{ .ndk = "21.4.7075529", .min = 16, .max = 30 },
NdkVersionRange{ .ndk = "22.1.7171670", .min = 16, .max = 30 },
NdkVersionRange{ .ndk = "23.2.8568313", .min = 16, .max = 31 },
NdkVersionRange{ .ndk = "24.0.8215888", .min = 19, .max = 32 },
NdkVersionRange{ .ndk = "25.1.8937393", .min = 19, .max = 33 },
};
// ls ~/software/android-sdk/ndk/*/toolchains/llvm/prebuilt/${hosttag}/sysroot/usr/lib/x86_64-linux-android | code
const x86_64_ndk_ranges = [_]NdkVersionRange{
NdkVersionRange{ .ndk = "19.2.5345600", .min = 21, .max = 28 },
NdkVersionRange{ .ndk = "20.1.5948944", .min = 21, .max = 29 },
NdkVersionRange{ .ndk = "21.4.7075529", .min = 21, .max = 30 },
NdkVersionRange{ .ndk = "22.1.7171670", .min = 21, .max = 30 },
NdkVersionRange{ .ndk = "23.2.8568313", .min = 21, .max = 31 },
NdkVersionRange{ .ndk = "24.0.8215888", .min = 21, .max = 32 },
NdkVersionRange{ .ndk = "25.1.8937393", .min = 21, .max = 33 },
};
// ls ~/software/android-sdk/ndk/*/toolchains/llvm/prebuilt/${hosttag}/sysroot/usr/lib/aarch64-linux-android | code
const aarch64_ndk_ranges = [_]NdkVersionRange{
NdkVersionRange{ .ndk = "19.2.5345600", .min = 21, .max = 28 },
NdkVersionRange{ .ndk = "20.1.5948944", .min = 21, .max = 29 },
NdkVersionRange{ .ndk = "21.4.7075529", .min = 21, .max = 30 },
NdkVersionRange{ .ndk = "22.1.7171670", .min = 21, .max = 30 },
NdkVersionRange{ .ndk = "23.2.8568313", .min = 21, .max = 31 },
NdkVersionRange{ .ndk = "24.0.8215888", .min = 21, .max = 32 },
NdkVersionRange{ .ndk = "25.1.8937393", .min = 21, .max = 33 },
};
/// Instantiates the full build pipeline to create an APK file.
///
pub fn createApp(
sdk: *Sdk,
apk_filename: []const u8,
src_file: []const u8,
java_files_opt: ?[]const []const u8,
app_config: AppConfig,
mode: std.builtin.Mode,
wanted_targets: AppTargetConfig,
key_store: KeyStore,
) CreateAppStep {
const write_xml_step = sdk.b.addWriteFile("strings.xml", blk: {
var buf = std.ArrayList(u8).init(sdk.b.allocator);
errdefer buf.deinit();
var writer = buf.writer();
writer.writeAll(
\\<?xml version="1.0" encoding="utf-8"?>
\\<resources>
\\
) catch unreachable;
writer.print(
\\ <string name="app_name">{s}</string>
\\ <string name="lib_name">{s}</string>
\\ <string name="package_name">{s}</string>
\\
, .{
app_config.display_name,
app_config.app_name,
app_config.package_name,
}) catch unreachable;
writer.writeAll(
\\</resources>
\\
) catch unreachable;
break :blk buf.toOwnedSlice() catch unreachable;
});
const manifest_step = sdk.b.addWriteFile("AndroidManifest.xml", blk: {
var buf = std.ArrayList(u8).init(sdk.b.allocator);
errdefer buf.deinit();
var writer = buf.writer();
@setEvalBranchQuota(1_000_000);
writer.print(
\\<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" package="{s}">
\\
, .{app_config.package_name}) catch unreachable;
for (app_config.permissions) |perm| {
writer.print(
\\ <uses-permission android:name="{s}"/>
\\
, .{perm}) catch unreachable;
}
const theme = if (app_config.fullscreen)
\\android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
else
\\
;
writer.print(
\\ <application android:debuggable="true" android:hasCode="{[hasCode]}" android:label="@string/app_name" {[theme]s} tools:replace="android:icon,android:theme,android:allowBackup,label" android:icon="@mipmap/icon" >
\\ <activity android:configChanges="keyboardHidden|orientation" android:name="android.app.NativeActivity">
\\ <meta-data android:name="android.app.lib_name" android:value="@string/lib_name"/>
\\ <intent-filter>
\\ <action android:name="android.intent.action.MAIN"/>
\\ <category android:name="android.intent.category.LAUNCHER"/>
\\ </intent-filter>
\\ </activity>
\\ </application>
\\</manifest>
\\
, .{
.hasCode = java_files_opt != null,
.theme = theme,
}) catch unreachable;
break :blk buf.toOwnedSlice() catch unreachable;
});
const resource_dir_step = CreateResourceDirectory.create(sdk.b);
for (app_config.resources) |res| {
resource_dir_step.add(res);
}
resource_dir_step.add(Resource{
.path = "values/strings.xml",
.content = write_xml_step.files.items[0].getPath(), // strings.xml
});
const sdk_version_int = @intFromEnum(app_config.target_version);
if (sdk_version_int < 16) @panic("Minimum supported sdk version is 16.");
const targets = AppTargetConfig{
.aarch64 = wanted_targets.aarch64 orelse (sdk_version_int >= 21),
.x86_64 = wanted_targets.x86_64 orelse (sdk_version_int >= 21),
.x86 = wanted_targets.x86 orelse (sdk_version_int >= 16),
.arm = wanted_targets.arm orelse (sdk_version_int >= 16),
};
// These are hard assumptions
if (targets.aarch64.? and sdk_version_int < 21) @panic("Aarch64 android is only available since sdk version 21.");
if (targets.x86_64.? and sdk_version_int < 21) @panic("x86_64 android is only available since sdk version 21.");
if (targets.x86.? and sdk_version_int < 16) @panic("x86 android is only available since sdk version 16.");
if (targets.arm.? and sdk_version_int < 16) @panic("arm android is only available since sdk version 16.");
// Also perform a soft check for known NDK versions
if (targets.aarch64.?) NdkVersionRange.validate(&aarch64_ndk_ranges, sdk.versions.ndk_version, sdk_version_int);
if (targets.x86_64.?) NdkVersionRange.validate(&x86_64_ndk_ranges, sdk.versions.ndk_version, sdk_version_int);
if (targets.x86.?) NdkVersionRange.validate(&x86_64_ndk_ranges, sdk.versions.ndk_version, sdk_version_int);
if (targets.arm.?) NdkVersionRange.validate(&arm_ndk_ranges, sdk.versions.ndk_version, sdk_version_int);
const root_jar = std.fs.path.resolve(sdk.b.allocator, &[_][]const u8{
sdk.folders.android_sdk_root,
"platforms",
sdk.b.fmt("android-{d}", .{sdk_version_int}),
"android.jar",
}) catch unreachable;
const unaligned_apk_name = sdk.b.fmt("unaligned-{s}", .{std.fs.path.basename(apk_filename)});
const make_unsigned_apk = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.aapt,
"package",
"-f", // force overwrite of existing files
"-I", // add an existing package to base include set
root_jar,
"-F", // specify the apk file to output
});
const unaligned_apk_file = make_unsigned_apk.addOutputFileArg(unaligned_apk_name);
make_unsigned_apk.addArg("-M"); // specify full path to AndroidManifest.xml to include in zip
make_unsigned_apk.addFileSourceArg(manifest_step.files.items[0].getPath());
make_unsigned_apk.addArg("-S"); // directory in which to find resources. Multiple directories will be scanned and the first match found (left to right) will take precedence
make_unsigned_apk.addDirectorySourceArg(resource_dir_step.getOutputDirectory());
make_unsigned_apk.addArgs(&[_][]const u8{
"-v",
"--target-sdk-version",
sdk.b.fmt("{d}", .{sdk_version_int}),
});
for (app_config.asset_directories) |dir| {
make_unsigned_apk.addArg("-A"); // additional directory in which to find raw asset files
make_unsigned_apk.addArg(sdk.b.pathFromRoot(dir));
}
const copy_to_zip_step = WriteToZip.init(sdk, unaligned_apk_file, unaligned_apk_name);
copy_to_zip_step.run_step.step.dependOn(&make_unsigned_apk.step);
var libs = std.ArrayList(*std.build.LibExeObjStep).init(sdk.b.allocator);
defer libs.deinit();
const build_options = BuildOptionStep.create(sdk.b);
build_options.add([]const u8, "app_name", app_config.app_name);
build_options.add(u16, "android_sdk_version", sdk_version_int);
build_options.add(bool, "fullscreen", app_config.fullscreen);
const android_module = sdk.b.addModule("android", .{
.source_file = .{ .path = "src/android-support.zig" },
.dependencies = &.{.{
.name = "build_options",
.module = build_options.getModule(),
}},
});
_ = android_module;
const align_step = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.zipalign,
"-p", // ensure shared libraries are aligned to 4KiB
"-f", // overwrite existing files
"-v", // verbose
"4",
});
align_step.addFileSourceArg(copy_to_zip_step.output_source);
align_step.step.dependOn(&make_unsigned_apk.step);
const apk_file = align_step.addOutputFileArg(apk_filename);
const apk_install = sdk.b.addInstallBinFile(apk_file, apk_filename);
sdk.b.getInstallStep().dependOn(&apk_install.step);
const java_dir = sdk.b.getInstallPath(.lib, "java");
if (java_files_opt) |java_files| {
const d8_cmd_builder = sdk.b.addSystemCommand(&[_][]const u8{sdk.system_tools.d8});
d8_cmd_builder.addArg("--lib");
d8_cmd_builder.addArg(root_jar);
for (java_files) |java_file| {
const javac_cmd = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.javac,
"-cp",
root_jar,
"-d",
java_dir,
});
javac_cmd.addFileSourceArg(std.build.FileSource.relative(java_file));
const name = std.fs.path.stem(java_file);
const name_ext = sdk.b.fmt("{s}.class", .{name});
const class_file = std.fs.path.resolve(sdk.b.allocator, &[_][]const u8{ java_dir, name_ext }) catch unreachable;
d8_cmd_builder.addFileSourceArg(.{ .path = class_file });
d8_cmd_builder.step.dependOn(&javac_cmd.step);
}
d8_cmd_builder.addArg("--classpath");
d8_cmd_builder.addArg(java_dir);
d8_cmd_builder.addArg("--output");
d8_cmd_builder.addArg(java_dir);
// make_unsigned_apk.step.dependOn(&d8_cmd_builder.step);
d8_cmd_builder.step.dependOn(&make_unsigned_apk.step);
const dex_file = std.fs.path.resolve(sdk.b.allocator, &[_][]const u8{ java_dir, "classes.dex" }) catch unreachable;
// make_unsigned_apk.addArg("-I");
// make_unsigned_apk.addArg(dex_file);
copy_to_zip_step.addFile(.{ .path = dex_file }, "classes.dex");
copy_to_zip_step.run_step.step.dependOn(&make_unsigned_apk.step); // enforces creation of APK before the execution
align_step.step.dependOn(©_to_zip_step.run_step.step);
}
// const sign_step = sdk.signApk(apk_filename, key_store);
const sign_step = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.apksigner,
"sign",
"--ks", // keystore
key_store.file,
});
sign_step.step.dependOn(&align_step.step);
{
const pass = sdk.b.fmt("pass:{s}", .{key_store.password});
sign_step.addArgs(&.{ "--ks-pass", pass });
sign_step.addFileSourceArg(apk_file);
}
inline for (std.meta.fields(AppTargetConfig)) |fld| {
const target_name = @field(Target, fld.name);
if (@field(targets, fld.name).?) {
const step = sdk.compileAppLibrary(
src_file,
app_config,
mode,
target_name,
// build_options.getPackage("build_options"),
);
libs.append(step) catch unreachable;
// https://developer.android.com/ndk/guides/abis#native-code-in-app-packages
const so_dir = switch (target_name) {
.aarch64 => "lib/arm64-v8a/",
.arm => "lib/armeabi-v7a/",
.x86_64 => "lib/x86_64/",
.x86 => "lib/x86/",
};
const target_filename = sdk.b.fmt("{s}lib{s}.so", .{ so_dir, app_config.app_name });
copy_to_zip_step.addFile(step.getOutputSource(), target_filename);
copy_to_zip_step.run_step.step.dependOn(&step.step);
align_step.step.dependOn(©_to_zip_step.run_step.step);
}
}
// const compress_step = compressApk(b, android_config, apk_file, "zig-out/demo.packed.apk");
// compress_step.dependOn(sign_step);
return CreateAppStep{
.sdk = sdk,
.first_step = &make_unsigned_apk.step,
.final_step = &sign_step.step,
.libraries = libs.toOwnedSlice() catch unreachable,
.build_options = build_options,
.package_name = sdk.b.dupe(app_config.package_name),
.apk_file = apk_file.dupe(sdk.b),
};
}
const CreateResourceDirectory = struct {
const Self = @This();
builder: *std.build.Builder,
step: std.build.Step,
resources: std.ArrayList(Resource),
directory: std.build.GeneratedFile,
pub fn create(b: *std.build.Builder) *Self {
const self = b.allocator.create(Self) catch @panic("out of memory");
self.* = Self{
.builder = b,
.step = Step.init(.{
.id = .custom,
.name = "populate resource directory",
.owner = b,
.makeFn = CreateResourceDirectory.make,
}),
.directory = .{ .step = &self.step },
.resources = std.ArrayList(Resource).init(b.allocator),
};
return self;
}
pub fn add(self: *Self, resource: Resource) void {
self.resources.append(Resource{
.path = self.builder.dupe(resource.path),
.content = resource.content.dupe(self.builder),
}) catch @panic("out of memory");
resource.content.addStepDependencies(&self.step);
}
pub fn getOutputDirectory(self: *Self) std.build.FileSource {
return .{ .generated = &self.directory };
}
fn make(step: *Step, progress: *std.Progress.Node) !void {
_ = progress;
const self = @fieldParentPtr(Self, "step", step);
// if (std.fs.path.dirname(strings_xml)) |dir| {
// std.fs.cwd().makePath(dir) catch unreachable;
// }
var cacher = createCacheBuilder(self.builder);
for (self.resources.items) |res| {
cacher.addBytes(res.path);
try cacher.addFile(res.content);
}
const root = try cacher.createAndGetDir();
for (self.resources.items) |res| {
if (std.fs.path.dirname(res.path)) |folder| {
try root.dir.makePath(folder);
}
const src_path = res.content.getPath(self.builder);
try std.fs.Dir.copyFile(
std.fs.cwd(),
src_path,
root.dir,
res.path,
.{},
);
}
self.directory.path = root.path;
}
};
fn run_copy_to_zip(sdk: *Sdk, input_file: std.build.FileSource, apk_file: std.build.FileSource, target_file: []const u8) *std.Build.RunStep {
const run_cp = sdk.b.addRunArtifact(sdk.host_tools.zip_add);
run_cp.addFileSourceArg(apk_file);
run_cp.addFileSourceArg(input_file);
run_cp.addArg(target_file);
return run_cp;
}
const WriteToZip = struct {
output_source: std.Build.FileSource,
run_step: *std.Build.RunStep,
pub fn init(sdk: *Sdk, zip_file: std.Build.FileSource, out_name: []const u8) WriteToZip {
const run_cp = sdk.b.addRunArtifact(sdk.host_tools.zip_add);
run_cp.addFileSourceArg(zip_file);
const output_source = run_cp.addOutputFileArg(out_name);
return WriteToZip{
.output_source = output_source,
.run_step = run_cp,
};
}
pub fn addFile(step: *const WriteToZip, input_file: std.Build.FileSource, target_file: []const u8) void {
step.run_step.addFileSourceArg(input_file);
step.run_step.addArg(target_file);
}
};
/// Compiles a single .so file for the given platform.
/// Note that this function assumes your build script only uses a single `android_config`!
pub fn compileAppLibrary(
sdk: *const Sdk,
src_file: []const u8,
app_config: AppConfig,
mode: std.builtin.Mode,
target: Target,
// build_options: std.build.Pkg,
) *std.build.LibExeObjStep {
const ndk_root = sdk.b.pathFromRoot(sdk.folders.android_ndk_root);
const TargetConfig = struct {
lib_dir: []const u8,
include_dir: []const u8,
out_dir: []const u8,
target: std.zig.CrossTarget,
};
const config: TargetConfig = switch (target) {
.aarch64 => TargetConfig{
.lib_dir = "aarch64-linux-android",
.include_dir = "aarch64-linux-android",
.out_dir = "arm64",
.target = zig_targets.aarch64,
},
.arm => TargetConfig{
.lib_dir = "arm-linux-androideabi",
.include_dir = "arm-linux-androideabi",
.out_dir = "armeabi",
.target = zig_targets.arm,
},
.x86 => TargetConfig{
.lib_dir = "i686-linux-android",
.include_dir = "i686-linux-android",
.out_dir = "x86",
.target = zig_targets.x86,
},
.x86_64 => TargetConfig{
.lib_dir = "x86_64-linux-android",
.include_dir = "x86_64-linux-android",
.out_dir = "x86_64",
.target = zig_targets.x86_64,
},
};
const lib_dir = sdk.b.fmt("{s}/toolchains/llvm/prebuilt/{s}/sysroot/usr/lib/{s}/{d}/", .{
ndk_root,
toolchainHostTag(),
config.lib_dir,
@intFromEnum(app_config.target_version),
});
const include_dir = std.fs.path.resolve(sdk.b.allocator, &[_][]const u8{
ndk_root,
"toolchains",
"llvm",
"prebuilt",
toolchainHostTag(),
"sysroot",
"usr",
"include",
}) catch unreachable;
const system_include_dir = std.fs.path.resolve(sdk.b.allocator, &[_][]const u8{ include_dir, config.include_dir }) catch unreachable;
const exe = sdk.b.addSharedLibrary(.{
.name = app_config.app_name,
.root_source_file = .{ .path = src_file },
.target = config.target,
.optimize = mode,
});
exe.link_emit_relocs = true;
exe.link_eh_frame_hdr = true;
exe.force_pic = true;
exe.link_function_sections = true;
exe.bundle_compiler_rt = true;
exe.strip = (mode == .ReleaseSmall);
exe.export_table = true;
exe.defineCMacro("ANDROID", null);
exe.linkLibC();
for (app_config.libraries) |lib| {
exe.linkSystemLibraryName(lib);
}
// exe.addIncludePath(include_dir);
exe.addLibraryPath(.{ .path = lib_dir });
// exe.addIncludePath(include_dir);
// exe.addIncludePath(system_include_dir);
exe.setLibCFile(sdk.createLibCFile(app_config.target_version, config.out_dir, include_dir, system_include_dir, lib_dir) catch unreachable);
exe.libc_file.?.addStepDependencies(&exe.step);
// TODO: Remove when https://github.com/ziglang/zig/issues/7935 is resolved:
if (exe.target.getCpuArch() == .x86) {
exe.link_z_notext = true;
}
return exe;
}
fn createLibCFile(sdk: *const Sdk, version: AndroidVersion, folder_name: []const u8, include_dir: []const u8, sys_include_dir: []const u8, crt_dir: []const u8) !std.build.FileSource {
const fname = sdk.b.fmt("android-{d}-{s}.conf", .{ @intFromEnum(version), folder_name });
var contents = std.ArrayList(u8).init(sdk.b.allocator);
errdefer contents.deinit();
var writer = contents.writer();
// The directory that contains `stdlib.h`.
// On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null
try writer.print("include_dir={s}\n", .{include_dir});
// The system-specific include directory. May be the same as `include_dir`.
// On Windows it's the directory that includes `vcruntime.h`.
// On POSIX it's the directory that includes `sys/errno.h`.
try writer.print("sys_include_dir={s}\n", .{sys_include_dir});
try writer.print("crt_dir={s}\n", .{crt_dir});
try writer.writeAll("msvc_lib_dir=\n");
try writer.writeAll("kernel32_lib_dir=\n");
try writer.writeAll("gcc_dir=\n");
const step = sdk.b.addWriteFile(fname, contents.items);
return step.files.items[0].getPath();
}
pub fn compressApk(sdk: Sdk, input_apk_file: []const u8, output_apk_file: []const u8) *Step {
const temp_folder = sdk.b.pathFromRoot("zig-cache/apk-compress-folder");
const mkdir_cmd = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.mkdir,
temp_folder,
});
const unpack_apk = sdk.b.addSystemCommand(&[_][]const u8{
"unzip",
"-o",
sdk.builder.pathFromRoot(input_apk_file),
"-d",
temp_folder,
});
unpack_apk.step.dependOn(&mkdir_cmd.step);
const repack_apk = sdk.b.addSystemCommand(&[_][]const u8{
"zip",
"-D9r",
sdk.builder.pathFromRoot(output_apk_file),
".",
});
repack_apk.cwd = temp_folder;
repack_apk.step.dependOn(&unpack_apk.step);
const rmdir_cmd = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.rm,
"-rf",
temp_folder,
});
rmdir_cmd.step.dependOn(&repack_apk.step);
return &rmdir_cmd.step;
}
pub fn installApp(sdk: Sdk, apk_file: std.build.FileSource) *Step {
const step = sdk.b.addSystemCommand(&[_][]const u8{ sdk.system_tools.adb, "-d", "install" });
step.addFileSourceArg(apk_file);
return &step.step;
}
pub fn startApp(sdk: Sdk, package_name: []const u8) *Step {
const command: []const []const u8 = switch (sdk.launch_using) {
.am => &.{
sdk.system_tools.adb,
"-d",
"shell",
"am",
"start",
"-n",
sdk.b.fmt("{s}/android.app.NativeActivity", .{package_name}),
},
.monkey => &.{
sdk.system_tools.adb,
"-d",
"shell",
"monkey",
"-p",
package_name,
"1",
},
};
const step = sdk.b.addSystemCommand(command);
return &step.step;
}
/// Configuration for a signing key.
pub const KeyConfig = struct {
pub const Algorithm = enum { RSA };
key_algorithm: Algorithm = .RSA,
key_size: u32 = 2048, // bits
validity: u32 = 10_000, // days