-
Notifications
You must be signed in to change notification settings - Fork 7
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub usingnamespace @cImport({ | ||
@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> | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see 👀