Skip to content

new example native activity #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 17 additions & 0 deletions examples/native-activity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# NativeActivity

Ported from https://github.com/android/ndk-samples/tree/main/native-activity .

This example demonstrates the use of native_app_glue and android_main.

## std.log override

```zig
_ = c.__android_log_write(priority, "ZIG", &buf.buffer);
```

You can display the logs filtered by "ZIG" in color by following the steps below.

```
$ adb logcat -s "ZIG" -v color
```
24 changes: 24 additions & 0 deletions examples/native-activity/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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.native_activity">

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:hasCode="false"
tools:targetApi="31">
<activity
android:name="android.app.NativeActivity"
android:exported="true"
android:label="@string/app_name">
<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/native-activity/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 NativeActivity</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.native_activity</string>
</resources>
69 changes: 69 additions & 0 deletions examples/native-activity/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const std = @import("std");
const builtin = @import("builtin");
const android = @import("android");

pub fn build(b: *std.Build) void {

// const target = b.standardTargetOptions(.{});
const target = b.resolveTargetQuery(.{
.cpu_arch = .aarch64,
.os_tag = .linux,
.abi = .android,
});
const optimize = b.standardOptimizeOption(.{});

const android_sdk = android.Sdk.create(b, .{});
const apk = android_sdk.createApk(.{
.api_level = .android15,
.build_tools_version = "35.0.1",
.ndk_version = "29.0.13113456",
});
const key_store_file = android_sdk.createKeyStore(.example);
apk.setKeyStore(key_store_file);
apk.setAndroidManifest(b.path("android/AndroidManifest.xml"));
apk.addResourceDirectory(b.path("android/res"));

const exe = b.addSharedLibrary(.{
.name = "native-activity",
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/main.zig"),
.link_libc = true,
});
b.installArtifact(exe);

exe.addIncludePath(.{ .cwd_relative = b.fmt("{s}/sources/android/native_app_glue", .{apk.ndk.path}) });
exe.addCSourceFile(.{
.file = .{ .cwd_relative = b.fmt("{s}/sources/android/native_app_glue/android_native_app_glue.c", .{apk.ndk.path}) },
});
exe.addCSourceFile(.{
.file = b.path("src/helper.cpp"),
});
// exe.target_link_options(${TARGET_NAME} PUBLIC -u ANativeActivity_onCreate)
const libs = [_][]const u8{
"android",
"EGL",
"GLESv1_CM",
"log",
};
for (libs) |lib| {
exe.linkSystemLibrary(lib);
}

const android_dep = b.dependency("android", .{
.optimize = optimize,
.target = target,
});
exe.root_module.addImport("android", android_dep.module("android"));

apk.addArtifact(exe);

const installed_apk = apk.addInstallApk();
b.getInstallStep().dependOn(&installed_apk.step);

const run_step = b.step("run", "Install and run the application on an Android device");
const adb_install = android_sdk.addAdbInstall(installed_apk.source);
const adb_start = android_sdk.addAdbStart("com.zig.native_activity/android.app.NativeActivity");
adb_start.step.dependOn(&adb_install.step);
run_step.dependOn(&adb_start.step);
}
14 changes: 14 additions & 0 deletions examples/native-activity/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.{
.name = .native_activity,
.version = "0.0.0",
.fingerprint = 0xe31e7fb5499884f3, // Changing this has security and trust implications.
.minimum_zig_version = "0.14.1",
.dependencies = .{
.android = .{ .path = "../.." },
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
10 changes: 10 additions & 0 deletions examples/native-activity/src/c.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub usingnamespace @cImport({
Copy link
Owner

Choose a reason for hiding this comment

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

Should avoid pub usingnamespace as it's proposed to be removed for legitimate reasons.
CTranslateModule should be using for this kind of thing instead.

Related issue:
ziglang/zig#20663

Copy link
Author

Choose a reason for hiding this comment

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

I see 👀

@cInclude("EGL/egl.h");
@cInclude("GLES/gl.h");
@cInclude("android/choreographer.h");
@cInclude("android/log.h");
@cInclude("android/sensor.h");
@cInclude("android/set_abort_message.h");
@cInclude("android_native_app_glue.h");
// #include <jni.h>
});
15 changes: 15 additions & 0 deletions examples/native-activity/src/helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <android_native_app_glue.h>
#include <android/sensor.h>

extern "C" {

void call_souce_process(android_app *state, android_poll_source *s) {
s->process(state, s);
Copy link
Author

Choose a reason for hiding this comment

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

can not call c++ member function from zig.

}

const float* get_acceleration(const ASensorEvent *event)
{
return &event->acceleration.x;
Copy link
Author

Choose a reason for hiding this comment

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

can not use anonymous union ?

typedef struct ASensorEvent {
    int32_t version; /* sizeof(struct ASensorEvent) */
    int32_t sensor;
    int32_t type;
    int32_t reserved0;
    int64_t timestamp;
    union {
        float           data[16];
        ASensorVector   vector;
        ASensorVector   acceleration;
        ASensorVector   magnetic;
        float           temperature;
        float           distance;
        float           light;
        float           pressure;
    };
    int32_t reserved1[4];
} ASensorEvent;

}

}
Loading