Skip to content
Open
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
2 changes: 0 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const std = @import("std");
const androidbuild = @import("src/androidbuild/androidbuild.zig");

// Expose Android build functionality for use in your build.zig

// TODO: rename tools.zig to Sdk.zig
pub const Sdk = @import("src/androidbuild/tools.zig");
pub const Apk = @import("src/androidbuild/apk.zig");
Expand Down
1 change: 1 addition & 0 deletions examples/minimal/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn build(b: *std.Build) void {
apk.setKeyStore(key_store_file);
apk.setAndroidManifest(b.path("android/AndroidManifest.xml"));
apk.addResourceDirectory(b.path("android/res"));
apk.addAssetsDirectory(b.path("android/assets"));

// Add Java files
// - If you have 'android:hasCode="false"' in your AndroidManifest.xml then no Java files are required
Expand Down
4 changes: 0 additions & 4 deletions examples/minimal/src/android-bind.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// TODO(jae): 2024-09-19
// Consider just making this import from native C libraries.
// For now just wanted a basic example that compiles and runs on an Android emulator

const __builtin_va_list = extern struct {
padding: u32,
};
Expand Down
58 changes: 2 additions & 56 deletions examples/sdl2/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@
android:versionName="1.0"
android:installLocation="auto"
package="com.zig.sdl2">
<!--
Based on:
https://github.com/libsdl-org/SDL/blob/release-2.30.7/android-project/app/src/main/AndroidManifest.xml
-->

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="35" />

<!-- OpenGL ES 2.0 -->
<uses-feature android:glEsVersion="0x00020000" />

<!-- Touchscreen support -->
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />

<!-- Game controller support -->
<uses-feature
android:name="android.hardware.bluetooth"
android:required="false" />
Expand All @@ -29,50 +18,19 @@
<uses-feature
android:name="android.hardware.usb.host"
android:required="false" />

<!-- External mouse input events -->
<uses-feature
android:name="android.hardware.type.pc"
android:required="false" />

<!-- Audio recording support -->
<!-- if you want to capture audio, uncomment this. -->
<!-- <uses-feature
android:name="android.hardware.microphone"
android:required="false" /> -->

<!-- Allow downloading to the external storage on Android 5.1 and older -->
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="22" /> -->

<!-- Allow access to Bluetooth devices -->
<!-- Currently this is just for Steam Controller support and requires setting SDL_HINT_JOYSTICK_HIDAPI_STEAM -->
<!-- <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> -->
<!-- <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> -->

<!-- Allow access to the vibrator -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.VIBRATE" />

<!-- if you want to capture audio, uncomment this. -->
<!-- <uses-permission android:name="android.permission.RECORD_AUDIO" /> -->

<!-- Create a Java class extending SDLActivity and place it in a
directory under app/src/main/java matching the package, e.g. app/src/main/java/com/gamemaker/game/MyGame.java

then replace "SDLActivity" with the name of your class (e.g. "MyGame")
in the XML below.

An example Java class can be found in README-android.md
-->
<application android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:hardwareAccelerated="true" >

<!-- Example of setting SDL hints from AndroidManifest.xml:
<meta-data android:name="SDL_ENV.SDL_ACCELEROMETER_AS_JOYSTICK" android:value="0"/>
-->

<activity android:name="ZigSDLActivity"
android:label="@string/app_name"
android:alwaysRetainTaskState="true"
Expand All @@ -85,18 +43,6 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Let Android know that we can handle some USB devices and should receive this event -->
<!-- <intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter> -->
<!-- Drop file event -->
<!--
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
-->
</activity>
</application>

Expand Down
4 changes: 0 additions & 4 deletions src/android/android.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");

// TODO(jae): 2024-10-03
// Consider exposing this in the future
// pub const builtin = android_builtin;

const android_builtin = struct {
const ab = @import("android_builtin");

Expand Down
24 changes: 16 additions & 8 deletions src/androidbuild/apk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ android_manifest: ?LazyPath,
artifacts: std.ArrayListUnmanaged(*Step.Compile),
java_files: std.ArrayListUnmanaged(LazyPath),
resources: std.ArrayListUnmanaged(Resource),
assets: std.ArrayListUnmanaged(LazyPath),

pub const Options = struct {
/// ie. "35.0.0"
Expand Down Expand Up @@ -92,6 +93,7 @@ pub fn create(sdk: *Sdk, options: Options) *Apk {
.artifacts = .empty,
.java_files = .empty,
.resources = .empty,
.assets = .empty,
};
return apk;
}
Expand All @@ -116,6 +118,13 @@ pub fn addResourceDirectory(apk: *Apk, dir: LazyPath) void {
}) catch @panic("OOM");
}

/// Set the directory of your Android assets folder.
/// - assets/MyImage.png
pub fn addAssetsDirectory(apk: *Apk, dir: LazyPath) void {
const b = apk.b;
apk.assets.append(b.allocator, dir) catch @panic("OOM");
}

/// Add artifact to the Android build, this should be a shared library (*.so)
/// that targets x86, x86_64, aarch64, etc
pub fn addArtifact(apk: *Apk, compile: *std.Build.Step.Compile) void {
Expand Down Expand Up @@ -344,14 +353,13 @@ fn doInstallApk(apk: *Apk) std.mem.Allocator.Error!*Step.InstallFile {
aapt2link.addArg("-o");
const resources_apk_file = aapt2link.addOutputFileArg("resources.apk");

// TODO(jae): 2024-09-17
// Add support for asset directories
// Additional directory
// aapt.step.dependOn(&resource_write_files.step);
// for (app_config.asset_directories) |dir| {
// make_unsigned_apk.addArg("-A"); // additional directory in which to find raw asset files
// make_unsigned_apk.addArg(sdk.b.pathFromRoot(dir));
// }
for (apk.assets.items) |dir| {
aapt2link.addArg("-A"); // additional directory in which to find raw asset files
const val = dir.src_path.sub_path;

aapt2link.addArg(val);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this to be:

aapt2link.addArg(val);
aapt2link.addFileInput(dir);

This will ensure if the file added is changed that the Zig compiler is aware of the change and we will re-run the aapt2link step.

From the Zig build system itself:

/// Adds an additional input files that, when modified, indicates that this Run
/// step should be re-executed.
/// If the Run step is determined to have side-effects, the Run step is always
/// executed when it appears in the build graph, regardless of whether this
/// file has been modified.
pub fn addFileInput(self: *Run, file_input: std.Build.LazyPath) void {
    file_input.addStepDependencies(&self.step);
    self.file_inputs.append(self.step.owner.allocator, file_input.dupe(self.step.owner)) catch @panic("OOM");
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like addFileInput is expecting only a file. There are compile issues with this method. I did not immediately see a directory implementation

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I mean the ideal thing would be adding each asset directly by walking the directory recursively.
It's something I've also had in the backlog for "addResourceDirectory".

Ideally just something that walks the tree and and at least does aapt2link.addFileInput(file) would be good.

//aapt2link.addFileInput(dir);
}

// Add resource files
for (apk.resources.items) |resource| {
Expand Down
Loading