feature. See also
. The project being documented here (as the example) is the Zig library itself.
system.detectAndroidApiLevel
fn detectAndroidApiLevel(io: Io) !u32
File
Code
fn detectAndroidApiLevel(io: Io) !u32 {
comptime if (builtin.os.tag != .linux) unreachable;
var child = try std.process.spawn(io, .{
.argv = &.{
"/system/bin/getprop",
"ro.build.version.sdk",
},
.stdin = .ignore,
.stdout = .pipe,
.stderr = .ignore,
});
errdefer child.kill(io);
// Currently API levels are two-digit numbers, but we want to make sure we never read a partial value.
var stdout_buf: [92 + 1]u8 = undefined;
var reader = child.stdout.?.readerStreaming(io, &.{});
const n = try reader.interface.readSliceShort(&stdout_buf);
const api_level = std.fmt.parseInt(u32, stdout_buf[0 .. n - 1], 10) catch |e| {
std.log.err(
"Could not parse API level, unexpected getprop output '{s}' ({s})",
.{ stdout_buf[0 .. n - 1], @errorName(e) },
);
return error.ApiLevelQueryFailed;
};
switch (try child.wait(io)) {
.exited => |code| if (code != 0) {
std.log.err("getprop terminated abnormally with exit code: {d}", .{code});
return error.ApiLevelQueryFailed;
},
.signal => |sig| {
std.log.err("getprop terminated abnormally with signal: {t}", .{sig});
return error.ApiLevelQueryFailed;
},
.stopped => |sig| {
std.log.err("getprop stopped abnormally with signal: {t}", .{sig});
return error.ApiLevelQueryFailed;
},
.unknown => {
std.log.err("getprop terminated abnormally", .{});
return error.ApiLevelQueryFailed;
},
}
return api_level;
}