Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions examples/raylib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
A minimal example of using [zig-android-sdk](https://github.com/silbinarywolf/zig-android-sdk) to build raylib for android.

Build for a single target with, e.g. `zig build -Dtarget=aarch64-linux-android`, or for all android targets with `zig build -Dandroid=true`.

**Note**:
Due to [an upstream bug](https://github.com/ziglang/zig/issues/20476), you will probably receive a warning (or multiple warnings if building for multiple targets) like this:
```
error: warning(link): unexpected LLD stderr:
ld.lld: warning: <path-to-project>/.zig-cache/o/4227869d730f094811a7cdaaab535797/libraylib.a: archive member '<path-to-ndk>/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/35/libGLESv2.so' is neither ET_REL nor LLVM bitcode
```
You can ignore this error for now.

You should probably source the `android_native_app_glue.c/.h` files from the version of the SDK you download, rather than using the included ones, to ensure you are using the most up-to-date versions.


32 changes: 32 additions & 0 deletions examples/raylib/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.zig.minimal">
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="9" />

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:hasCode="false"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
tools:targetApi="29"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance"
android:configChanges="layoutDirection|locale|orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
>
<activity
android:name="android.app.NativeActivity"
android:exported="true"
android:label="@string/app_name">
<meta-data android:name="android.app.lib_name" android:value="main"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions examples/raylib/android/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Pretty name of your app -->
<string name="app_name">Zig Minimal</string>
<!--
This is required for the APK name. This identifies your app, Android will associate
your signing key with this identifier and will prevent updates if the key changes.
-->
<string name="package_name">com.zig.minimal</string>
</resources>
86 changes: 86 additions & 0 deletions examples/raylib/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const android = @import("android");
const std = @import("std");

//this is targeting android API level 29.
//You may have to change the values here and in android/AndroidManifest.xml to target your desired API level
const android_api = "29";
const android_version = .android10;

pub fn build(b: *std.Build) void {
const root_target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const android_targets = android.standardTargets(b, root_target);

var root_target_single = [_]std.Build.ResolvedTarget{root_target};
const targets: []std.Build.ResolvedTarget = if (android_targets.len == 0)
root_target_single[0..]
else
android_targets;

const android_apk: ?*android.APK = blk: {
if (android_targets.len == 0) {
break :blk null;
}
const android_tools = android.Tools.create(b, .{
.api_level = android_version,
.build_tools_version = "35.0.0",
.ndk_version = "27.2.12479018",
});
const apk = android.APK.create(b, android_tools);

const key_store_file = android_tools.createKeyStore(android.CreateKey.example());
apk.setKeyStore(key_store_file);
apk.setAndroidManifest(b.path("android/AndroidManifest.xml"));
apk.addResourceDirectory(b.path("android/res"));

break :blk apk;
};

for(targets) |target| {
if (target.result.abi.isAndroid()) {
const lib_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const lib = b.addLibrary(.{
.linkage = .dynamic,
.name = "minimal_android_raylib",
.root_module = lib_mod,
});

b.installArtifact(lib);
lib.linkLibC();
const raylib_dep = b.dependency("raylib_zig", .{
.target = target,
.optimize = optimize,
.android_api_version = @as([]const u8, android_api)
});
const raylib_artifact = raylib_dep.artifact("raylib");
lib.linkLibrary(raylib_artifact);

const raylib_mod = raylib_dep.module("raylib");
lib.root_module.addImport("raylib", raylib_mod);
const apk: *android.APK = android_apk orelse @panic("Android APK should be initialized");

const android_dep = b.dependency("android", .{
.optimize = optimize,
.target = target,
});
lib.root_module.addImport("android", android_dep.module("android"));
lib.root_module.linkSystemLibrary("android", .{.preferred_link_mode = .dynamic});
lib.root_module.linkSystemLibrary("EGL", .{.preferred_link_mode = .dynamic});
lib.root_module.linkSystemLibrary("GLESv2", .{.preferred_link_mode = .dynamic});
lib.root_module.linkSystemLibrary("log", .{ .preferred_link_mode = .dynamic });
lib.root_module.addCSourceFile(.{ .file = b.path("src/android_native_app_glue.c")});

apk.addArtifact(lib);
} else {
//non-android build logic...
}
}
if (android_apk) |apk| {
apk.installApk();
}
}
53 changes: 53 additions & 0 deletions examples/raylib/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.{
// This is the default name used by packages depending on this one. For
// example, when a user runs `zig fetch --save <url>`, this field is used
// as the key in the `dependencies` table. Although the user can choose a
// different name, most users will stick with this provided value.
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = .minimal_android_raylib,

// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",

// Together with name, this represents a globally unique package
// identifier. This field is generated by the Zig toolchain when the
// package is first created, and then *never changes*. This allows
// unambiguous detection of one package being an updated version of
// another.
//
// When forking a Zig project, this id should be regenerated (delete the
// field and run `zig build`) if the upstream project is still maintained.
// Otherwise, the fork is *hostile*, attempting to take control over the
// original project's identity. Thus it is recommended to leave the comment
// on the following line intact, so that it shows up in code reviews that
// modify the field.
.fingerprint = 0x7d5dc97ee62a8428, // Changing this has security and trust implications.

// Tracks the earliest Zig version that the package considers to be a
// supported use case.
.minimum_zig_version = "0.14.0",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
.raylib_zig = .{
.url = "git+https://github.com/lumenkeyes/raylib-zig#e421e2a16c7f965b8ff70c7db7a13bf9fe2d321c",
.hash = "raylib_zig-5.6.0-dev-KE8REAgyBQB4QKuE8SJZHBOw_KGcoLCybyyAIXRLCXKQ",
},
.android = .{ .path = "../.." },
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
// For example...
//"LICENSE",
//"README.md",
},
}
Loading