Skip to content

Commit a67f9b2

Browse files
authored
add assets support (#61)
1 parent b124e1d commit a67f9b2

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

examples/minimal/build.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ pub fn build(b: *std.Build) void {
2828
apk.setAndroidManifest(b.path("android/AndroidManifest.xml"));
2929
apk.addResourceDirectory(b.path("android/res"));
3030

31+
// Assets support - put your textures and sounds here, not in the resources:
32+
// apk.addAssetDirectory(b.path("android/assets"));
33+
3134
// Add Java files
3235
// - If you have 'android:hasCode="false"' in your AndroidManifest.xml then no Java files are required
3336
// see: https://developer.android.com/ndk/samples/sample_na

src/androidbuild/apk.zig

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ android_manifest: ?LazyPath,
4848
artifacts: std.ArrayListUnmanaged(*Step.Compile),
4949
java_files: std.ArrayListUnmanaged(LazyPath),
5050
resources: std.ArrayListUnmanaged(Resource),
51+
assets: std.ArrayListUnmanaged(Resource),
5152

5253
pub const Options = struct {
5354
/// ie. "35.0.0"
@@ -93,6 +94,7 @@ pub fn create(sdk: *Sdk, options: Options) *Apk {
9394
.artifacts = .empty,
9495
.java_files = .empty,
9596
.resources = .empty,
97+
.assets = .empty
9698
};
9799
return apk;
98100
}
@@ -117,6 +119,15 @@ pub fn addResourceDirectory(apk: *Apk, dir: LazyPath) void {
117119
}) catch @panic("OOM");
118120
}
119121

122+
pub fn addAssetDirectory(apk: *Apk, dir: LazyPath) void {
123+
const b = apk.b;
124+
apk.assets.append(b.allocator, Resource{
125+
.directory = .{
126+
.source = dir,
127+
},
128+
}) catch @panic("OOM");
129+
}
130+
120131
/// Add artifact to the Android build, this should be a shared library (*.so)
121132
/// that targets x86, x86_64, aarch64, etc
122133
pub fn addArtifact(apk: *Apk, compile: *std.Build.Step.Compile) void {
@@ -345,14 +356,15 @@ fn doInstallApk(apk: *Apk) std.mem.Allocator.Error!*Step.InstallFile {
345356
aapt2link.addArg("-o");
346357
const resources_apk_file = aapt2link.addOutputFileArg("resources.apk");
347358

348-
// TODO(jae): 2024-09-17
349-
// Add support for asset directories
350-
// Additional directory
351-
// aapt.step.dependOn(&resource_write_files.step);
352-
// for (app_config.asset_directories) |dir| {
353-
// make_unsigned_apk.addArg("-A"); // additional directory in which to find raw asset files
354-
// make_unsigned_apk.addArg(sdk.b.pathFromRoot(dir));
355-
// }
359+
// Add assets
360+
for (apk.assets.items) |asset| {
361+
switch (asset) {
362+
.directory => |asset_dir| {
363+
aapt2link.addArg("-A");
364+
aapt2link.addDirectoryArg(asset_dir.source);
365+
}
366+
}
367+
}
356368

357369
// Add resource files
358370
for (apk.resources.items) |resource| {

0 commit comments

Comments
 (0)