Skip to content

Commit 18348b6

Browse files
experiment with adding Android emulation to continuous integration (#25)
experiment with adding Android emulation to continuous integration (#25)
1 parent 3ef024e commit 18348b6

File tree

6 files changed

+86
-16
lines changed

6 files changed

+86
-16
lines changed

.github/workflows/ci.yml

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ jobs:
3737
with:
3838
packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;29.0.13113456'
3939

40+
#
41+
# Stable Zig Builds
42+
#
43+
4044
- name: Setup Zig Stable (0.14.0)
4145
# note(jae): 2024-09-15
4246
# Uses download mirror first as preferred by Zig Foundation
@@ -50,9 +54,50 @@ jobs:
5054
working-directory: examples/minimal
5155

5256
- name: Build SDL2 Example (Zig Stable)
53-
run: zig build -Dandroid=true --verbose
57+
run: |
58+
zig build -Dandroid=true --verbose
59+
zig build -Dandroid=true -Dcrash-on-exception --verbose
5460
working-directory: examples/sdl2
5561

62+
# TODO(jae): 2025-03-30
63+
# Need to figure out how to get 'adb shell monkey' to return an error code or be able to return an error code
64+
# if the stdout of the command has 'Monkey aborted due to error.'
65+
66+
# - name: Enable KVM (For Android emulation)
67+
# if: startsWith(matrix.os, 'ubuntu-')
68+
# run: |
69+
# echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
70+
# sudo udevadm control --reload-rules
71+
# sudo udevadm trigger --name-match=kvm
72+
73+
# - name: Run Minimal Example (Android Emulator)
74+
# if: startsWith(matrix.os, 'ubuntu-')
75+
# uses: reactivecircus/android-emulator-runner@v2
76+
# with:
77+
# api-level: 34
78+
# arch: x86_64
79+
# profile: Nexus 6
80+
# script: |
81+
# adb install ./zig-out/bin/minimal.apk
82+
# adb shell am start -S -W -n com.zig.minimal/android.app.NativeActivity
83+
# working-directory: examples/minimal
84+
85+
# - name: Run SDL2 Example (Android Emulator)
86+
# if: startsWith(matrix.os, 'ubuntu-')
87+
# uses: reactivecircus/android-emulator-runner@v2
88+
# with:
89+
# api-level: 34
90+
# arch: x86_64
91+
# profile: Nexus 6
92+
# script: |
93+
# adb install ./zig-out/bin/sdl-zig-demo.apk
94+
# adb shell monkey --kill-process-after-error --monitor-native-crashes --pct-touch 100 -p com.zig.sdl2 --throttle 1000 -v 2
95+
# working-directory: examples/sdl2
96+
97+
#
98+
# Nightly Zig Builds
99+
#
100+
56101
- name: Setup Zig Nightly
57102
uses: mlugg/setup-zig@v1
58103
with:

examples/minimal/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
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.
44

5-
### Build and install to test one target against a local emulator
5+
### Build, install to test one target against a local emulator and run
66

77
```sh
88
zig build -Dtarget=x86_64-linux-android
99
adb install ./zig-out/bin/minimal.apk
10+
adb shell am start -S -W -n com.zig.minimal/android.app.NativeActivity
1011
```
1112

1213
### Build and install for all supported Android targets

examples/sdl2/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
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.
44

5-
### Build and install to test one target against a local emulator
5+
### Build, install to test one target against a local emulator and run
66

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

1213
### Build and install for all supported Android targets
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.zig.sdl2; // <- Your game package name
2+
3+
import org.libsdl.app.SDLActivity;
4+
import android.os.Bundle;
5+
import android.util.AndroidRuntimeException;
6+
7+
/**
8+
* Used by ci.yml to make the application crash if an error occurs initializing your application.
9+
*
10+
* This allows the following commands to catch crash errors on startup:
11+
* - adb install ./zig-out/bin/sdl-zig-demo.apk
12+
* - adb shell monkey --kill-process-after-error --monitor-native-crashes --pct-touch 100 -p com.zig.sdl2 -v 50
13+
*/
14+
public class ZigSDLActivity extends SDLActivity {
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
19+
if (mBrokenLibraries) {
20+
throw new AndroidRuntimeException("SDL Error, has broken libraries");
21+
}
22+
}
23+
}

examples/sdl2/android/src/ZigSDLActivity.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,4 @@
66
* A sample wrapper class that just calls SDLActivity
77
*/
88
public class ZigSDLActivity extends SDLActivity {
9-
// @Override
10-
// protected String[] getLibraries() {
11-
// return new String[] {
12-
// "SDL2",
13-
// // "SDL2_image",
14-
// // "SDL2_mixer",
15-
// // "SDL2_net",
16-
// // "SDL2_ttf",
17-
// "main"
18-
// };
19-
// }
20-
}
9+
}

examples/sdl2/build.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub fn build(b: *std.Build) void {
77
const optimize = b.standardOptimizeOption(.{});
88
const android_targets = android.standardTargets(b, root_target);
99

10+
const crash_on_exception = b.option(bool, "crash-on-exception", "if true then we'll use the activity from androidCrashTest folder") orelse false;
11+
1012
var root_target_single = [_]std.Build.ResolvedTarget{root_target};
1113
const targets: []std.Build.ResolvedTarget = if (android_targets.len == 0)
1214
root_target_single[0..]
@@ -40,7 +42,16 @@ pub fn build(b: *std.Build) void {
4042
apk.addResourceDirectory(b.path("android/res"));
4143

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

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

0 commit comments

Comments
 (0)