Skip to content

Commit 04b2eb6

Browse files
add Android SDK auto-detection to Linux (#37)
add Android SDK auto-detection to Linux
1 parent 4caa73e commit 04b2eb6

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/androidbuild/androidbuild.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ const AndroidTargetQuery = struct {
135135
.abi = if (android_target.cpu_arch != .arm) .android else .androideabi,
136136
.cpu_arch = android_target.cpu_arch,
137137
.cpu_features_add = android_target.cpu_features_add,
138+
// TODO(jae): 2025-05-11
139+
// Setup Android API Level for Zig 0.14.0+
140+
// .android_api_level = null,
138141
};
139142
}
140143
};

src/androidbuild/tools.zig

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,81 @@ fn getAndroidSDKPath(allocator: std.mem.Allocator) error{OutOfMemory}![]const u8
585585
defer allocator.free(user);
586586
return try std.fmt.allocPrint(allocator, "/Users/{s}/Library/Android/sdk", .{user});
587587
},
588+
// NOTE(jae: 2025-05-11
589+
// Auto-discovery of Android SDK for Linux
590+
// - /home/AccountName/Android/Sdk
591+
// - /usr/lib/android-sdk
592+
// - /Library/Android/sdk
593+
// - /Users/[USER]/Library/Android/sdk
594+
// Source: https://stackoverflow.com/a/34627928
595+
.linux => {
596+
{
597+
const android_sdk_path = "/usr/lib/android-sdk";
598+
const has_path: bool = pathblk: {
599+
std.fs.accessAbsolute(android_sdk_path, .{}) catch |err| switch (err) {
600+
error.FileNotFound => break :pathblk false, // fallthrough and try next
601+
else => std.debug.panic("{s} has error: {}", .{ android_sdk_path, err }),
602+
};
603+
break :pathblk true;
604+
};
605+
if (has_path) {
606+
return android_sdk_path;
607+
}
608+
}
609+
610+
{
611+
const android_sdk_path = "/Library/Android/sdk";
612+
const has_path: bool = pathblk: {
613+
std.fs.accessAbsolute(android_sdk_path, .{}) catch |err| switch (err) {
614+
error.FileNotFound => break :pathblk false, // fallthrough and try next
615+
else => std.debug.panic("{s} has error: {}", .{ android_sdk_path, err }),
616+
};
617+
break :pathblk true;
618+
};
619+
if (has_path) {
620+
return android_sdk_path;
621+
}
622+
}
623+
624+
// Check user paths
625+
// - /home/AccountName/Android/Sdk
626+
// - /Users/[USER]/Library/Android/sdk
627+
const user = std.process.getEnvVarOwned(allocator, "USER") catch |err| switch (err) {
628+
error.OutOfMemory => return error.OutOfMemory,
629+
error.EnvironmentVariableNotFound => &[0]u8{},
630+
error.InvalidWtf8 => @panic("USER environment variable is invalid UTF-8"),
631+
};
632+
if (user.len > 0) {
633+
{
634+
const android_sdk_path = try std.fmt.allocPrint(allocator, "/Users/{s}/Library/Android/sdk", .{user});
635+
const has_path: bool = pathblk: {
636+
std.fs.accessAbsolute(android_sdk_path, .{}) catch |err| switch (err) {
637+
error.FileNotFound => break :pathblk false, // fallthrough and try next
638+
else => std.debug.panic("{s} has error: {}", .{ android_sdk_path, err }),
639+
};
640+
break :pathblk true;
641+
};
642+
if (has_path) {
643+
return android_sdk_path;
644+
}
645+
}
646+
{
647+
// NOTE(jae): 2025-05-11
648+
// No idea if /AccountName/ maps to $USER but going to assume it does for now.
649+
const android_sdk_path = try std.fmt.allocPrint(allocator, "/home/{s}/Android/Sdk", .{user});
650+
const has_path: bool = pathblk: {
651+
std.fs.accessAbsolute(android_sdk_path, .{}) catch |err| switch (err) {
652+
error.FileNotFound => break :pathblk false, // fallthrough and try next
653+
else => std.debug.panic("{s} has error: {}", .{ android_sdk_path, err }),
654+
};
655+
break :pathblk true;
656+
};
657+
if (has_path) {
658+
return android_sdk_path;
659+
}
660+
}
661+
}
662+
},
588663
else => {},
589664
}
590665
return &[0]u8{};

0 commit comments

Comments
 (0)