Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const android = @import("android");
pub fn build(b: *std.Build) !void {
const android_sdk = android.Sdk.create(b, .{});
const apk = android_sdk.createApk(.{
.name = "example",
.api_level = .android15,
.build_tools_version = "35.0.1",
.ndk_version = "29.0.13113456",
Expand All @@ -28,12 +29,15 @@ pub fn build(b: *std.Build) !void {
apk.addResourceDirectory(b.path("android/res"));
apk.addJavaSourceFile(.{ .file = b.path("android/src/NativeInvocationHandler.java") });
for (android.standardTargets(b, b.standardTargetOptions(.{}))) |target| {
apk.addArtifact(b.addSharedLibrary(.{
.name = exe_name,
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}))
apk.addArtifact(b.addLibrary(.{
.name = "main",
.linkage = .dynamic,
.root_module = b.createModule(
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
),
}));
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion examples/minimal/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn build(b: *std.Build) void {

const android_sdk = android.Sdk.create(b, .{});
const apk = android_sdk.createApk(.{
.name = exe_name,
.api_level = .android15,
.build_tools_version = "35.0.1",
.ndk_version = "29.0.13113456",
Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn build(b: *std.Build) void {
});

var exe: *std.Build.Step.Compile = if (target.result.abi.isAndroid()) b.addLibrary(.{
.name = exe_name,
.name = "main",
.root_module = app_module,
.linkage = .dynamic,
}) else b.addExecutable(.{
Expand Down
6 changes: 3 additions & 3 deletions examples/raylib/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ const std = @import("std");
const android = @import("android");
const LinkMode = std.builtin.LinkMode;

const exe_name = "raylib";

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

const android_sdk = android.Sdk.create(b, .{});
const apk = android_sdk.createApk(.{
.name = exe_name,
.api_level = .android15,
.build_tools_version = "35.0.1",
.ndk_version = "29.0.13113456",
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn build(b: *std.Build) void {

apk.addArtifact(b.addLibrary(.{
.linkage = .dynamic,
.name = exe_name,
.name = "main",
.root_module = app,
}));
} else {
Expand Down
5 changes: 3 additions & 2 deletions examples/sdl2/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const builtin = @import("builtin");
const android = @import("android");

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

const android_sdk = android.Sdk.create(b, .{});
const apk = android_sdk.createApk(.{
.name = exe_name,
.api_level = .android15,
.build_tools_version = "36.1.0",
.ndk_version = "29.0.14206865",
Expand Down Expand Up @@ -68,7 +70,6 @@ pub fn build(b: *std.Build) void {
};

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

const exe_lib = b.addLibrary(.{
.name = exe_name,
.name = "main",
.root_module = app_module,
.linkage = .dynamic,
});
Expand Down
13 changes: 8 additions & 5 deletions src/androidbuild/Apk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub const Resource = union(enum) {
};

b: *std.Build,
/// APK file output name, ie. "{name}.apk"
name: []const u8,
Comment thread
ypsvlq marked this conversation as resolved.
sdk: *Sdk,
/// Path to Native Development Kit, this includes various C-code headers, libraries, and more.
/// ie. $ANDROID_HOME/ndk/29.0.13113456
Expand All @@ -53,6 +55,8 @@ resources: std.ArrayListUnmanaged(Resource),
assets: std.ArrayListUnmanaged(Resource),

pub const Options = struct {
/// APK file output name, ie. "{name}.apk"
name: []const u8,
Comment thread
ypsvlq marked this conversation as resolved.
/// ie. "35.0.0"
build_tools_version: []const u8,
/// ie. "27.0.12077973"
Expand Down Expand Up @@ -87,6 +91,7 @@ pub fn create(sdk: *Sdk, options: Options) *Apk {
const apk: *Apk = b.allocator.create(Apk) catch @panic("OOM");
apk.* = .{
.b = b,
.name = options.name,
.sdk = sdk,
.ndk = ndk,
.build_tools = build_tools,
Expand Down Expand Up @@ -477,7 +482,7 @@ fn doInstallApk(apk: *Apk) Allocator.Error!*Step.InstallFile {
.x86 => "x86",
else => @panic(b.fmt("unsupported or unhandled arch: {s}", .{@tagName(target.result.cpu.arch)})),
};
_ = apk_files.addCopyFile(artifact.getEmittedBin(), b.fmt("lib/{s}/libmain.so", .{so_dir}));
_ = apk_files.addCopyFile(artifact.getEmittedBin(), b.fmt("lib/{s}/lib{s}.so", .{ so_dir, artifact.name }));

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

const apk_name = apk.artifacts.items[0].name;

// Align contents of .apk (zip)
const aligned_apk_file: LazyPath = blk: {
var zipalign = b.addSystemCommand(&[_][]const u8{
Expand Down Expand Up @@ -748,7 +751,7 @@ fn doInstallApk(apk: *Apk) Allocator.Error!*Step.InstallFile {
zipalign.addFileArg(zip_file);
zipalign.step.dependOn(update_zip);

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

Expand All @@ -769,7 +772,7 @@ fn doInstallApk(apk: *Apk) Allocator.Error!*Step.InstallFile {
break :blk signed_output_apk_file;
};

const install_apk = b.addInstallBinFile(signed_apk_file, b.fmt("{s}.apk", .{apk_name}));
const install_apk = b.addInstallBinFile(signed_apk_file, b.fmt("{s}.apk", .{apk.name}));
return install_apk;
}

Expand Down
Loading