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
47 changes: 46 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
with:
packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;29.0.13113456'

#
# Stable Zig Builds
#

- name: Setup Zig Stable (0.14.0)
# note(jae): 2024-09-15
# Uses download mirror first as preferred by Zig Foundation
Expand All @@ -50,9 +54,50 @@ jobs:
working-directory: examples/minimal

- name: Build SDL2 Example (Zig Stable)
run: zig build -Dandroid=true --verbose
run: |
zig build -Dandroid=true --verbose
zig build -Dandroid=true -Dcrash-on-exception --verbose
working-directory: examples/sdl2

# TODO(jae): 2025-03-30
# Need to figure out how to get 'adb shell monkey' to return an error code or be able to return an error code
# if the stdout of the command has 'Monkey aborted due to error.'

# - name: Enable KVM (For Android emulation)
# if: startsWith(matrix.os, 'ubuntu-')
# run: |
# echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
# sudo udevadm control --reload-rules
# sudo udevadm trigger --name-match=kvm

# - name: Run Minimal Example (Android Emulator)
# if: startsWith(matrix.os, 'ubuntu-')
# uses: reactivecircus/android-emulator-runner@v2
# with:
# api-level: 34
# arch: x86_64
# profile: Nexus 6
# script: |
# adb install ./zig-out/bin/minimal.apk
# adb shell am start -S -W -n com.zig.minimal/android.app.NativeActivity
# working-directory: examples/minimal

# - name: Run SDL2 Example (Android Emulator)
# if: startsWith(matrix.os, 'ubuntu-')
# uses: reactivecircus/android-emulator-runner@v2
# with:
# api-level: 34
# arch: x86_64
# profile: Nexus 6
# script: |
# adb install ./zig-out/bin/sdl-zig-demo.apk
# adb shell monkey --kill-process-after-error --monitor-native-crashes --pct-touch 100 -p com.zig.sdl2 --throttle 1000 -v 2
# working-directory: examples/sdl2

#
# Nightly Zig Builds
#

- name: Setup Zig Nightly
uses: mlugg/setup-zig@v1
with:
Expand Down
3 changes: 2 additions & 1 deletion examples/minimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

As of 2024-09-19, this is a thrown together, very quick copy-paste of the minimal example from the original [ZigAndroidTemplate](https://github.com/ikskuh/ZigAndroidTemplate/blob/master/examples/minimal/main.zig) repository.

### Build and install to test one target against a local emulator
### Build, install to test one target against a local emulator and run

```sh
zig build -Dtarget=x86_64-linux-android
adb install ./zig-out/bin/minimal.apk
adb shell am start -S -W -n com.zig.minimal/android.app.NativeActivity
```

### Build and install for all supported Android targets
Expand Down
3 changes: 2 additions & 1 deletion examples/sdl2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

This is a copy-paste of [Andrew Kelly's SDL Zig Demo](https://github.com/andrewrk/sdl-zig-demo) but running on Android. The build is setup so you can also target your native operating system as well.

### Build and install to test one target against a local emulator
### Build, install to test one target against a local emulator and run

```sh
zig build -Dtarget=x86_64-linux-android
adb install ./zig-out/bin/sdl-zig-demo.apk
adb shell am start -S -W -n com.zig.sdl2/com.zig.sdl2.ZigSDLActivity
```

### Build and install for all supported Android targets
Expand Down
23 changes: 23 additions & 0 deletions examples/sdl2/android/androidCrashTest/ZigSDLActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.zig.sdl2; // <- Your game package name

import org.libsdl.app.SDLActivity;
import android.os.Bundle;
import android.util.AndroidRuntimeException;

/**
* Used by ci.yml to make the application crash if an error occurs initializing your application.
*
* This allows the following commands to catch crash errors on startup:
* - adb install ./zig-out/bin/sdl-zig-demo.apk
* - adb shell monkey --kill-process-after-error --monitor-native-crashes --pct-touch 100 -p com.zig.sdl2 -v 50
*/
public class ZigSDLActivity extends SDLActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (mBrokenLibraries) {
throw new AndroidRuntimeException("SDL Error, has broken libraries");
}
}
}
13 changes: 1 addition & 12 deletions examples/sdl2/android/src/ZigSDLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,4 @@
* A sample wrapper class that just calls SDLActivity
*/
public class ZigSDLActivity extends SDLActivity {
// @Override
// protected String[] getLibraries() {
// return new String[] {
// "SDL2",
// // "SDL2_image",
// // "SDL2_mixer",
// // "SDL2_net",
// // "SDL2_ttf",
// "main"
// };
// }
}
}
13 changes: 12 additions & 1 deletion examples/sdl2/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const android_targets = android.standardTargets(b, root_target);

const crash_on_exception = b.option(bool, "crash-on-exception", "if true then we'll use the activity from androidCrashTest folder") orelse false;

var root_target_single = [_]std.Build.ResolvedTarget{root_target};
const targets: []std.Build.ResolvedTarget = if (android_targets.len == 0)
root_target_single[0..]
Expand Down Expand Up @@ -40,7 +42,16 @@ pub fn build(b: *std.Build) void {
apk.addResourceDirectory(b.path("android/res"));

// Add Java files
apk.addJavaSourceFile(.{ .file = b.path("android/src/ZigSDLActivity.java") });
if (!crash_on_exception) {
apk.addJavaSourceFile(.{ .file = b.path("android/src/ZigSDLActivity.java") });
} else {
// This is used for testing in Github Actions, so that we can call "adb shell monkey" and trigger
// an error.
// - adb shell monkey --kill-process-after-error --monitor-native-crashes --pct-touch 100 -p com.zig.sdl2 -v 50
//
// This alternate SDLActivity skips the nice dialog box you get when doing manual human testing.
apk.addJavaSourceFile(.{ .file = b.path("android/androidCrashTest/ZigSDLActivity.java") });
}

// Add SDL2's Java files like SDL.java, SDLActivity.java, HIDDevice.java, etc
const sdl_dep = b.dependency("sdl2", .{
Expand Down