Skip to content

Commit

Permalink
update to ZigAndroidTemplate#26
Browse files Browse the repository at this point in the history
  • Loading branch information
zenith391 committed Dec 30, 2022
1 parent c5c5a24 commit ce60460
Show file tree
Hide file tree
Showing 11 changed files with 729 additions and 306 deletions.
76 changes: 40 additions & 36 deletions android/Sdk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ pub fn createApp(
sdk: *Sdk,
apk_file: []const u8,
src_file: []const u8,
dex_file_opt: ?[]const u8,
app_config: AppConfig,
mode: std.builtin.Mode,
wanted_targets: AppTargetConfig,
Expand Down Expand Up @@ -460,35 +461,28 @@ pub fn createApp(
, .{perm}) catch unreachable;
}

if (app_config.fullscreen) {
writer.writeAll(
\\ <application android:debuggable="true" android:hasCode="false" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" tools:replace="android:icon,android:theme,android:allowBackup,label" android:icon="@mipmap/icon" >
\\ <activity android:configChanges="keyboardHidden|orientation" android:name="android.app.NativeActivity">
\\ <meta-data android:name="android.app.lib_name" android:value="@string/lib_name"/>
\\ <intent-filter>
\\ <action android:name="android.intent.action.MAIN"/>
\\ <category android:name="android.intent.category.LAUNCHER"/>
\\ </intent-filter>
\\ </activity>
\\ </application>
\\</manifest>
\\
) catch unreachable;
} else {
writer.writeAll(
\\ <application android:debuggable="true" android:hasCode="false" android:label="@string/app_name" tools:replace="android:icon,android:theme,android:allowBackup,label" android:icon="@mipmap/icon">
\\ <activity android:configChanges="keyboardHidden|orientation" android:name="android.app.NativeActivity">
\\ <meta-data android:name="android.app.lib_name" android:value="@string/lib_name"/>
\\ <intent-filter>
\\ <action android:name="android.intent.action.MAIN"/>
\\ <category android:name="android.intent.category.LAUNCHER"/>
\\ </intent-filter>
\\ </activity>
\\ </application>
\\</manifest>
\\
) catch unreachable;
}
const theme = if (app_config.fullscreen)
\\android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
else
\\
;

writer.print(
\\ <application android:debuggable="true" android:hasCode="{[hasCode]}" android:label="@string/app_name" {[theme]s} tools:replace="android:icon,android:theme,android:allowBackup,label" android:icon="@mipmap/icon" >
\\ <activity android:configChanges="keyboardHidden|orientation" android:name="android.app.NativeActivity">
\\ <meta-data android:name="android.app.lib_name" android:value="@string/lib_name"/>
\\ <intent-filter>
\\ <action android:name="android.intent.action.MAIN"/>
\\ <category android:name="android.intent.category.LAUNCHER"/>
\\ </intent-filter>
\\ </activity>
\\ </application>
\\</manifest>
\\
, .{
.hasCode = dex_file_opt != null,
.theme = theme,
}) catch unreachable;

break :blk buf.toOwnedSlice() catch unreachable;
});
Expand Down Expand Up @@ -546,6 +540,8 @@ pub fn createApp(
sdk.b.pathFromRoot(unaligned_apk_file),
"-I", // add an existing package to base include set
root_jar,
"-I",
"classes.dex",
});

make_unsigned_apk.addArg("-M"); // specify full path to AndroidManifest.xml to include in zip
Expand Down Expand Up @@ -576,6 +572,12 @@ pub fn createApp(

const align_step = sdk.alignApk(unaligned_apk_file, apk_file);

if (dex_file_opt) |dex_file| {
const copy_dex_to_zip = CopyToZipStep.create(sdk, unaligned_apk_file, null, std.build.FileSource.relative(dex_file));
copy_dex_to_zip.step.dependOn(&make_unsigned_apk.step); // enforces creation of APK before the execution
align_step.dependOn(&copy_dex_to_zip.step);
}

const sign_step = sdk.signApk(apk_file, key_store);
sign_step.dependOn(align_step);

Expand Down Expand Up @@ -686,16 +688,18 @@ const CreateResourceDirectory = struct {
const CopyToZipStep = struct {
step: Step,
sdk: *Sdk,
target_dir: []const u8,
target_dir: ?[]const u8,
input_file: std.build.FileSource,
apk_file: []const u8,

fn create(sdk: *Sdk, apk_file: []const u8, target_dir: []const u8, input_file: std.build.FileSource) *CopyToZipStep {
std.debug.assert(target_dir[target_dir.len - 1] == '/');
fn create(sdk: *Sdk, apk_file: []const u8, target_dir_opt: ?[]const u8, input_file: std.build.FileSource) *CopyToZipStep {
if (target_dir_opt) |target_dir| {
std.debug.assert(target_dir[target_dir.len - 1] == '/');
}
const self = sdk.b.allocator.create(CopyToZipStep) catch unreachable;
self.* = CopyToZipStep{
.step = Step.init(.custom, "copy to zip", sdk.b.allocator, make),
.target_dir = target_dir,
.target_dir = target_dir_opt,
.input_file = input_file,
.sdk = sdk,
.apk_file = sdk.b.pathFromRoot(apk_file),
Expand All @@ -712,10 +716,10 @@ const CopyToZipStep = struct {

const output_path = self.input_file.getPath(self.sdk.b);

var zip_name = std.mem.concat(self.sdk.b.allocator, u8, &[_][]const u8{
self.target_dir,
var zip_name = if (self.target_dir) |target_dir| std.mem.concat(self.sdk.b.allocator, u8, &[_][]const u8{
target_dir,
std.fs.path.basename(output_path),
}) catch unreachable;
}) catch unreachable else std.fs.path.basename(output_path);

const args = [_][]const u8{
self.sdk.host_tools.zip_add.getOutputSource().getPath(self.sdk.b),
Expand Down
8 changes: 7 additions & 1 deletion android/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,22 @@ pub fn build(b: *std.build.Builder) !void {

// Replace by your app's main file.
// Here this is some code to choose the example to run
const ExampleType = enum { egl, textview };
const ExampleType = enum { egl, textview, invocationhandler };
const example = b.option(ExampleType, "example", "Which example to run") orelse .egl;
const src = switch (example) {
.egl => "examples/egl/main.zig",
.textview => "examples/textview/main.zig",
.invocationhandler => "examples/invocationhandler/main.zig",
};
const dex: ?[:0]const u8 = switch (example) {
.invocationhandler => "classes.dex",
else => null,
};

const app = sdk.createApp(
"app-template.apk",
src,
dex,
config,
mode,
.{
Expand Down
25 changes: 13 additions & 12 deletions android/examples/egl/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const log = android.log;

const EGLContext = android.egl.EGLContext;
const JNI = android.JNI;
const NativeActivity = android.NativeActivity;
const c = android.egl.c;

const app_log = std.log.scoped(.app);
Expand Down Expand Up @@ -200,10 +201,10 @@ pub const AndroidApp = struct {
});

if (event_type == .AKEY_EVENT_ACTION_DOWN) {
var jni = JNI.init(self.activity);
defer jni.deinit();
var native_activity = NativeActivity.init(self.activity);
defer native_activity.deinit();

var codepoint = jni.AndroidGetUnicodeChar(
var codepoint = try native_activity.AndroidGetUnicodeChar(
android.AKeyEvent_getKeyCode(event),
android.AKeyEvent_getMetaState(event),
);
Expand Down Expand Up @@ -252,21 +253,21 @@ pub const AndroidApp = struct {
const event_type = @intToEnum(android.AMotionEventActionType, android.AMotionEvent_getAction(event));

{
var jni = JNI.init(self.activity);
defer jni.deinit();
var native_activity = NativeActivity.init(self.activity);
defer native_activity.deinit();

// Show/Hide keyboard
// _ = jni.AndroidDisplayKeyboard(true);
// _ = native_activity.AndroidDisplayKeyboard(true);

// this allows you to send the app in the background
// const success = jni.AndroidSendToBack(true);
// const success = native_activity.AndroidSendToBack(true);
// _ = success;
// std.log.scoped(.input).debug("SendToBack() = {}\n", .{success});

// This is a demo on how to request permissions:
if (event_type == .AMOTION_EVENT_ACTION_UP) {
if (!JNI.AndroidHasPermissions(&jni, "android.permission.RECORD_AUDIO")) {
JNI.AndroidRequestAppPermissions(&jni, "android.permission.RECORD_AUDIO");
if (!try NativeActivity.AndroidHasPermissions(&native_activity, "android.permission.RECORD_AUDIO")) {
try NativeActivity.AndroidRequestAppPermissions(&native_activity, "android.permission.RECORD_AUDIO");
}
}
}
Expand Down Expand Up @@ -350,11 +351,11 @@ pub const AndroidApp = struct {

fn mainLoop(self: *Self) !void {
// This code somehow crashes yet. Needs more investigations
var jni = JNI.init(self.activity);
defer jni.deinit();
var native_activity = NativeActivity.init(self.activity);
defer native_activity.deinit();

// Must be called from main thread…
_ = jni.AndroidMakeFullscreen();
_ = try native_activity.AndroidMakeFullscreen();

var loop: usize = 0;
app_log.info("mainLoop() started\n", .{});
Expand Down
Loading

0 comments on commit ce60460

Please sign in to comment.