|
| 1 | +const android = @import("android"); |
| 2 | +const std = @import("std"); |
| 3 | + |
| 4 | +//This is targeting android version 10 / API level 29. |
| 5 | +//Change the value here and in android/AndroidManifest.xml to target your desired API level |
| 6 | +const android_version: android.APILevel = .android10; |
| 7 | +const android_api = std.fmt.comptimePrint("{}", .{@intFromEnum(android_version)}); |
| 8 | +const exe_name = "raylib"; |
| 9 | + |
| 10 | +pub fn build(b: *std.Build) void { |
| 11 | + const root_target = b.standardTargetOptions(.{}); |
| 12 | + const optimize = b.standardOptimizeOption(.{}); |
| 13 | + const android_targets = android.standardTargets(b, root_target); |
| 14 | + |
| 15 | + var root_target_single = [_]std.Build.ResolvedTarget{root_target}; |
| 16 | + const targets: []std.Build.ResolvedTarget = if (android_targets.len == 0) |
| 17 | + root_target_single[0..] |
| 18 | + else |
| 19 | + android_targets; |
| 20 | + |
| 21 | + const android_apk: ?*android.APK = blk: { |
| 22 | + if (android_targets.len == 0) { |
| 23 | + break :blk null; |
| 24 | + } |
| 25 | + const android_tools = android.Tools.create(b, .{ |
| 26 | + .api_level = android_version, |
| 27 | + .build_tools_version = "35.0.1", |
| 28 | + .ndk_version = "29.0.13113456", |
| 29 | + }); |
| 30 | + const apk = android.APK.create(b, android_tools); |
| 31 | + |
| 32 | + const key_store_file = android_tools.createKeyStore(android.CreateKey.example()); |
| 33 | + apk.setKeyStore(key_store_file); |
| 34 | + apk.setAndroidManifest(b.path("android/AndroidManifest.xml")); |
| 35 | + apk.addResourceDirectory(b.path("android/res")); |
| 36 | + |
| 37 | + break :blk apk; |
| 38 | + }; |
| 39 | + |
| 40 | + for (targets) |target| { |
| 41 | + const lib_mod = b.createModule(.{ |
| 42 | + .root_source_file = b.path("src/main.zig"), |
| 43 | + .target = target, |
| 44 | + .optimize = optimize, |
| 45 | + }); |
| 46 | + const lib = b.addLibrary(.{ |
| 47 | + .linkage = .dynamic, |
| 48 | + .name = exe_name, |
| 49 | + .root_module = lib_mod, |
| 50 | + }); |
| 51 | + lib.linkLibC(); |
| 52 | + b.installArtifact(lib); |
| 53 | + |
| 54 | + const android_ndk_path = if(android_apk) |apk| (b.fmt("{s}/ndk/{s}", .{ apk.tools.android_sdk_path, apk.tools.ndk_version })) else ""; |
| 55 | + const raylib_dep = if (target.result.abi.isAndroid()) ( |
| 56 | + b.dependency("raylib_zig", .{ |
| 57 | + .target = target, |
| 58 | + .optimize = optimize, |
| 59 | + .android_api_version = @as([]const u8, android_api), |
| 60 | + .android_ndk = @as([]const u8, android_ndk_path), |
| 61 | + })) else ( |
| 62 | + b.dependency("raylib_zig", .{ |
| 63 | + .target = target, |
| 64 | + .optimize = optimize, |
| 65 | + .shared = true |
| 66 | + })); |
| 67 | + const raylib_artifact = raylib_dep.artifact("raylib"); |
| 68 | + lib.linkLibrary(raylib_artifact); |
| 69 | + const raylib_mod = raylib_dep.module("raylib"); |
| 70 | + lib.root_module.addImport("raylib", raylib_mod); |
| 71 | + |
| 72 | + if (target.result.abi.isAndroid()) { |
| 73 | + const apk: *android.APK = android_apk orelse @panic("Android APK should be initialized"); |
| 74 | + const android_dep = b.dependency("android", .{ |
| 75 | + .optimize = optimize, |
| 76 | + .target = target, |
| 77 | + }); |
| 78 | + lib.root_module.linkSystemLibrary("android", .{ .preferred_link_mode = .dynamic }); |
| 79 | + lib.root_module.addImport("android", android_dep.module("android")); |
| 80 | + |
| 81 | + const native_app_glue_dir: std.Build.LazyPath = .{ .cwd_relative = b.fmt("{s}/sources/android/native_app_glue", .{android_ndk_path}) }; |
| 82 | + lib.root_module.addCSourceFile(.{ .file = native_app_glue_dir.path(b, "android_native_app_glue.c") }); |
| 83 | + lib.root_module.addIncludePath(native_app_glue_dir); |
| 84 | + |
| 85 | + lib.root_module.linkSystemLibrary("log", .{ .preferred_link_mode = .dynamic }); |
| 86 | + apk.addArtifact(lib); |
| 87 | + } else { |
| 88 | + const exe = b.addExecutable(.{ .name = exe_name, .optimize = optimize, .root_module = lib_mod }); |
| 89 | + b.installArtifact(exe); |
| 90 | + |
| 91 | + const run_exe = b.addRunArtifact(exe); |
| 92 | + const run_step = b.step("run", "Run the application"); |
| 93 | + run_step.dependOn(&run_exe.step); |
| 94 | + } |
| 95 | + } |
| 96 | + if (android_apk) |apk| { |
| 97 | + apk.installApk(); |
| 98 | + } |
| 99 | +} |
0 commit comments