Skip to content

Commit

Permalink
update ZigAndroidTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
zenith391 committed Dec 30, 2022
1 parent 3f8269f commit c5c5a24
Show file tree
Hide file tree
Showing 15 changed files with 1,485 additions and 49 deletions.
1 change: 1 addition & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
zig-cache
*.apk
*.keystore
.vscode
.build_config
zig-out
*.apk.idsig
7 changes: 7 additions & 0 deletions android/LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2020 Felix "xq" Queißner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 7 additions & 2 deletions android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ There are convenience options with `zig build push` (installs the app on a conne
Install the [`sdkmanager`](https://developer.android.com/studio/command-line/sdkmanager) and invoke the following command line:

```
sdkmanager --install "platforms;android-21" # Min version: Android 5
sdkmanager --install "build-tools;33.0.0"
# Android Platforms for your target Android version
# Min version: Android 5
sdkmanager --install "platforms;android-21"
# you can install other versions as well
# remember to set it like `zig build -Dandroid=android99`
sdkmanager --install "build-tools;33.0.1"
sdkmanager --install "ndk;25.1.8937393"
zig build keystore install run
```
Expand Down
105 changes: 62 additions & 43 deletions android/Sdk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ folders: UserConfig,

versions: ToolchainVersions,

launch_using: ADBLaunchMethod = .monkey,

pub const ADBLaunchMethod = enum {
monkey,
am,
};

/// Initializes the android SDK.
/// It requires some input on which versions of the tool chains should be used
pub fn init(b: *Builder, user_config: ?UserConfig, toolchains: ToolchainVersions) *Sdk {
Expand All @@ -55,7 +62,13 @@ pub fn init(b: *Builder, user_config: ?UserConfig, toolchains: ToolchainVersions

const zipalign = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "build-tools", toolchains.build_tools_version, "zipalign" ++ exe }) catch unreachable;
const aapt = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "build-tools", toolchains.build_tools_version, "aapt" ++ exe }) catch unreachable;
const adb = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "platform-tools", "adb" ++ exe }) catch unreachable;
const adb = blk1: {
const adb_sdk = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "platform-tools", "adb" ++ exe }) catch unreachable;
if (!auto_detect.fileExists(adb_sdk)) {
break :blk1 auto_detect.findProgramPath(b.allocator, "adb") orelse @panic("No adb found");
}
break :blk1 adb_sdk;
};
const apksigner = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.android_sdk_root, "build-tools", toolchains.build_tools_version, "apksigner" ++ exe }) catch unreachable;
const keytool = std.fs.path.join(b.allocator, &[_][]const u8{ actual_user_config.java_home, "bin", "keytool" ++ exe }) catch unreachable;

Expand Down Expand Up @@ -96,7 +109,7 @@ pub fn init(b: *Builder, user_config: ?UserConfig, toolchains: ToolchainVersions
}

pub const ToolchainVersions = struct {
build_tools_version: []const u8 = "33.0.0",
build_tools_version: []const u8 = "33.0.1",
ndk_version: []const u8 = "25.1.8937393",
};

Expand Down Expand Up @@ -194,8 +207,6 @@ pub const AppConfig = struct {
},

libraries: []const []const u8 = &app_libs,

packages: []const std.build.Pkg = &.{},
};

/// One of the legal targets android can be built for.
Expand Down Expand Up @@ -448,9 +459,6 @@ pub fn createApp(
\\
, .{perm}) catch unreachable;
}
writer.print(
\\ <uses-sdk android:minSdkVersion="{d}" />
, .{ @enumToInt(app_config.target_version) }) catch unreachable;

if (app_config.fullscreen) {
writer.writeAll(
Expand Down Expand Up @@ -748,9 +756,6 @@ pub fn compileAppLibrary(
for (app_config.libraries) |lib| {
exe.linkSystemLibraryName(lib);
}
for (app_config.packages) |package| {
exe.addPackage(package);
}

exe.setBuildMode(mode);

Expand Down Expand Up @@ -924,14 +929,25 @@ pub fn installApp(sdk: Sdk, apk_file: std.build.FileSource) *Step {
}

pub fn startApp(sdk: Sdk, package_name: []const u8) *Step {
const step = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.adb,
"shell",
"am",
"start",
"-n",
sdk.b.fmt("{s}/android.app.NativeActivity", .{package_name}),
});
const command: []const []const u8 = switch (sdk.launch_using) {
.am => &.{
sdk.system_tools.adb,
"shell",
"am",
"start",
"-n",
sdk.b.fmt("{s}/android.app.NativeActivity", .{package_name}),
},
.monkey => &.{
sdk.system_tools.adb,
"shell",
"monkey",
"-p",
package_name,
"1",
},
};
const step = sdk.b.addSystemCommand(command);
return &step.step;
}

Expand All @@ -946,28 +962,33 @@ pub const KeyConfig = struct {
/// A build step that initializes a new key store from the given configuration.
/// `android_config.key_store` must be non-`null` as it is used to initialize the key store.
pub fn initKeystore(sdk: Sdk, key_store: KeyStore, key_config: KeyConfig) *Step {
const step = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.keytool,
"-genkey",
"-v",
"-keystore",
key_store.file,
"-alias",
key_store.alias,
"-keyalg",
@tagName(key_config.key_algorithm),
"-keysize",
sdk.b.fmt("{d}", .{key_config.key_size}),
"-validity",
sdk.b.fmt("{d}", .{key_config.validity}),
"-storepass",
key_store.password,
"-keypass",
key_store.password,
"-dname",
key_config.distinguished_name,
});
return &step.step;
if (auto_detect.fileExists(key_store.file)) {
std.log.warn("keystore already exists: {s}", .{key_store.file});
return sdk.b.step("init_keystore_noop", "Do nothing, since key exists");
} else {
const step = sdk.b.addSystemCommand(&[_][]const u8{
sdk.system_tools.keytool,
"-genkey",
"-v",
"-keystore",
key_store.file,
"-alias",
key_store.alias,
"-keyalg",
@tagName(key_config.key_algorithm),
"-keysize",
sdk.b.fmt("{d}", .{key_config.key_size}),
"-validity",
sdk.b.fmt("{d}", .{key_config.validity}),
"-storepass",
key_store.password,
"-keypass",
key_store.password,
"-dname",
key_config.distinguished_name,
});
return &step.step;
}
}

const Builder = std.build.Builder;
Expand Down Expand Up @@ -1008,9 +1029,7 @@ const zig_targets = struct {
};
};

const app_libs = [_][]const u8{
"GLESv2", "EGL", "android", "log", "aaudio"
};
const app_libs = [_][]const u8{ "GLESv2", "EGL", "android", "log", "aaudio" };

const BuildOptionStep = struct {
const Self = @This();
Expand Down
13 changes: 11 additions & 2 deletions android/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn build(b: *std.build.Builder) !void {
// This is a set of resources. It should at least contain a "mipmap/icon.png" resource that
// will provide the application icon.
.resources = &[_]Sdk.Resource{
.{ .path = "mipmap/icon.png", .content = .{ .path = "example/icon.png" } },
.{ .path = "mipmap/icon.png", .content = .{ .path = "examples/icon.png" } },
},

.aaudio = aaudio,
Expand All @@ -66,9 +66,18 @@ pub fn build(b: *std.build.Builder) !void {
.libraries = libraries.items,
};

// Replace by your app's main file.
// Here this is some code to choose the example to run
const ExampleType = enum { egl, textview };
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",
};

const app = sdk.createApp(
"app-template.apk",
"example/main.zig",
src,
config,
mode,
.{
Expand Down
11 changes: 10 additions & 1 deletion android/build/auto-detect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig
return config;
}

fn findProgramPath(allocator: std.mem.Allocator, program: []const u8) ?[]const u8 {
pub fn findProgramPath(allocator: std.mem.Allocator, program: []const u8) ?[]const u8 {
const args: []const []const u8 = if (builtin.os.tag == .windows)
&[_][]const u8{ "where", program }
else
Expand Down Expand Up @@ -493,3 +493,12 @@ fn findProblemWithJdk(b: *Builder, path: []const u8) ?[]const u8 {
fn pathConcat(b: *Builder, left: []const u8, right: []const u8) []const u8 {
return std.fs.path.join(b.allocator, &[_][]const u8{ left, right }) catch unreachable;
}

pub fn fileExists(path: []const u8) bool {
std.fs.cwd().access(path, .{}) catch |err| {
if (err == error.FileNotFound) return false;
std.log.debug("Cannot access {s}, {s}", .{ path, @errorName(err) });
return false;
};
return true;
}
37 changes: 37 additions & 0 deletions android/devshell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ pkgs }:

with pkgs;

# Configure your development environment.
#
# Documentation: https://github.com/numtide/devshell
devshell.mkShell {
name = "android-project";
motd = ''
Entered the Android app development environment.
'';
env = [
{
name = "ANDROID_HOME";
value = "${android-sdk}/share/android-sdk";
}
{
name = "ANDROID_SDK_ROOT";
value = "${android-sdk}/share/android-sdk";
}
{
name = "ANDROID_NDK_ROOT";
value = "${android-sdk}/share/android-sdk/ndk";
}
{
name = "JAVA_HOME";
value = jdk11.home;
}
];
packages = [
android-sdk
gradle
jdk11
zig
];
}
Binary file added android/examples/egl/logo.stl
Binary file not shown.
Loading

0 comments on commit c5c5a24

Please sign in to comment.