Skip to content

Commit d6f20c3

Browse files
authored
Set APK name explicitly (#80)
- Prevents link issues caused by renaming a library but not changing its SONAME - Allows multiple artifacts to be added for the same architecture Fixes #39
1 parent a32c14d commit d6f20c3

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const android = @import("android");
2020
pub fn build(b: *std.Build) !void {
2121
const android_sdk = android.Sdk.create(b, .{});
2222
const apk = android_sdk.createApk(.{
23+
.name = "example",
2324
.api_level = .android15,
2425
.build_tools_version = "35.0.1",
2526
.ndk_version = "29.0.13113456",
@@ -28,12 +29,15 @@ pub fn build(b: *std.Build) !void {
2829
apk.addResourceDirectory(b.path("android/res"));
2930
apk.addJavaSourceFile(.{ .file = b.path("android/src/NativeInvocationHandler.java") });
3031
for (android.standardTargets(b, b.standardTargetOptions(.{}))) |target| {
31-
apk.addArtifact(b.addSharedLibrary(.{
32-
.name = exe_name,
33-
.root_source_file = b.path("src/main.zig"),
34-
.target = target,
35-
.optimize = optimize,
36-
}))
32+
apk.addArtifact(b.addLibrary(.{
33+
.name = "main",
34+
.linkage = .dynamic,
35+
.root_module = b.createModule(
36+
.root_source_file = b.path("src/main.zig"),
37+
.target = target,
38+
.optimize = optimize,
39+
),
40+
}));
3741
}
3842
}
3943
```

examples/minimal/build.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn build(b: *std.Build) void {
1919

2020
const android_sdk = android.Sdk.create(b, .{});
2121
const apk = android_sdk.createApk(.{
22+
.name = exe_name,
2223
.api_level = .android15,
2324
.build_tools_version = "35.0.1",
2425
.ndk_version = "29.0.13113456",
@@ -50,7 +51,7 @@ pub fn build(b: *std.Build) void {
5051
});
5152

5253
var exe: *std.Build.Step.Compile = if (target.result.abi.isAndroid()) b.addLibrary(.{
53-
.name = exe_name,
54+
.name = "main",
5455
.root_module = app_module,
5556
.linkage = .dynamic,
5657
}) else b.addExecutable(.{

examples/raylib/build.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ const std = @import("std");
22
const android = @import("android");
33
const LinkMode = std.builtin.LinkMode;
44

5-
const exe_name = "raylib";
6-
75
pub fn build(b: *std.Build) void {
6+
const exe_name = "raylib";
87
const root_target = b.standardTargetOptions(.{});
98
const optimize = b.standardOptimizeOption(.{});
109
const android_targets = android.standardTargets(b, root_target);
@@ -20,6 +19,7 @@ pub fn build(b: *std.Build) void {
2019

2120
const android_sdk = android.Sdk.create(b, .{});
2221
const apk = android_sdk.createApk(.{
22+
.name = exe_name,
2323
.api_level = .android15,
2424
.build_tools_version = "35.0.1",
2525
.ndk_version = "29.0.13113456",
@@ -72,7 +72,7 @@ pub fn build(b: *std.Build) void {
7272

7373
apk.addArtifact(b.addLibrary(.{
7474
.linkage = .dynamic,
75-
.name = exe_name,
75+
.name = "main",
7676
.root_module = app,
7777
}));
7878
} else {

examples/sdl2/build.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const builtin = @import("builtin");
33
const android = @import("android");
44

55
pub fn build(b: *std.Build) void {
6+
const exe_name: []const u8 = "sdl-zig-demo";
67
const root_target = b.standardTargetOptions(.{});
78
const optimize = b.standardOptimizeOption(.{});
89
const android_targets = android.standardTargets(b, root_target);
@@ -20,6 +21,7 @@ pub fn build(b: *std.Build) void {
2021

2122
const android_sdk = android.Sdk.create(b, .{});
2223
const apk = android_sdk.createApk(.{
24+
.name = exe_name,
2325
.api_level = .android15,
2426
.build_tools_version = "36.1.0",
2527
.ndk_version = "29.0.14206865",
@@ -68,7 +70,6 @@ pub fn build(b: *std.Build) void {
6870
};
6971

7072
for (targets) |target| {
71-
const exe_name: []const u8 = "sdl-zig-demo";
7273
const app_module = b.createModule(.{
7374
.target = target,
7475
.optimize = optimize,
@@ -115,7 +116,7 @@ pub fn build(b: *std.Build) void {
115116
app_module.addImport("android", android_dep.module("android"));
116117

117118
const exe_lib = b.addLibrary(.{
118-
.name = exe_name,
119+
.name = "main",
119120
.root_module = app_module,
120121
.linkage = .dynamic,
121122
});

src/androidbuild/Apk.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub const Resource = union(enum) {
3333
};
3434

3535
b: *std.Build,
36+
/// APK file output name, ie. "{name}.apk"
37+
name: []const u8,
3638
sdk: *Sdk,
3739
/// Path to Native Development Kit, this includes various C-code headers, libraries, and more.
3840
/// ie. $ANDROID_HOME/ndk/29.0.13113456
@@ -51,6 +53,8 @@ resources: std.ArrayListUnmanaged(Resource),
5153
assets: std.ArrayListUnmanaged(Resource),
5254

5355
pub const Options = struct {
56+
/// APK file output name, ie. "{name}.apk"
57+
name: []const u8,
5458
/// ie. "35.0.0"
5559
build_tools_version: []const u8,
5660
/// ie. "27.0.12077973"
@@ -85,6 +89,7 @@ pub fn create(sdk: *Sdk, options: Options) *Apk {
8589
const apk: *Apk = b.allocator.create(Apk) catch @panic("OOM");
8690
apk.* = .{
8791
.b = b,
92+
.name = options.name,
8893
.sdk = sdk,
8994
.ndk = ndk,
9095
.build_tools = build_tools,
@@ -475,7 +480,7 @@ fn doInstallApk(apk: *Apk) Allocator.Error!*Step.InstallFile {
475480
.x86 => "x86",
476481
else => @panic(b.fmt("unsupported or unhandled arch: {s}", .{@tagName(target.result.cpu.arch)})),
477482
};
478-
_ = apk_files.addCopyFile(artifact.getEmittedBin(), b.fmt("lib/{s}/libmain.so", .{so_dir}));
483+
_ = apk_files.addCopyFile(artifact.getEmittedBin(), b.fmt("lib/{s}/lib{s}.so", .{ so_dir, artifact.name }));
479484

480485
// Add module
481486
// - If a module has no `root_source_file` (e.g you're only compiling C files using `addCSourceFiles`)
@@ -717,8 +722,6 @@ fn doInstallApk(apk: *Apk) Allocator.Error!*Step.InstallFile {
717722
// lint.setEnvironmentVariable("JAVA_HOME", apk.tools.jdk_path);
718723
// lint.addFileArg(android_manifest_file);
719724

720-
const apk_name = apk.artifacts.items[0].name;
721-
722725
// Align contents of .apk (zip)
723726
const aligned_apk_file: LazyPath = blk: {
724727
var zipalign = b.addSystemCommand(&[_][]const u8{
@@ -746,7 +749,7 @@ fn doInstallApk(apk: *Apk) Allocator.Error!*Step.InstallFile {
746749
zipalign.addFileArg(zip_file);
747750
zipalign.step.dependOn(update_zip);
748751

749-
const apk_file = zipalign.addOutputFileArg(b.fmt("aligned-{s}.apk", .{apk_name}));
752+
const apk_file = zipalign.addOutputFileArg(b.fmt("aligned-{s}.apk", .{apk.name}));
750753
break :blk apk_file;
751754
};
752755

@@ -767,7 +770,7 @@ fn doInstallApk(apk: *Apk) Allocator.Error!*Step.InstallFile {
767770
break :blk signed_output_apk_file;
768771
};
769772

770-
const install_apk = b.addInstallBinFile(signed_apk_file, b.fmt("{s}.apk", .{apk_name}));
773+
const install_apk = b.addInstallBinFile(signed_apk_file, b.fmt("{s}.apk", .{apk.name}));
771774
return install_apk;
772775
}
773776

0 commit comments

Comments
 (0)