gogio: load Android assets from module root

This commit is contained in:
Joe Julian
2026-04-16 20:59:20 -07:00
parent 192acd9d09
commit f71579e799
2 changed files with 46 additions and 3 deletions
+18 -3
View File
@@ -424,7 +424,8 @@ func exeAndroid(tmpDir string, tools *androidTools, bi *buildInfo, extraJars, pe
if err != nil {
return err
}
if err := copyTree(filepath.Join(bi.pkgDir, "android", "res"), resDir); err != nil {
moduleRoot := moduleRootForDir(bi.pkgDir)
if err := copyTree(filepath.Join(moduleRoot, "android", "res"), resDir); err != nil {
return err
}
resZip := filepath.Join(tmpDir, "resources.zip")
@@ -452,8 +453,8 @@ func exeAndroid(tmpDir string, tools *androidTools, bi *buildInfo, extraJars, pe
AppName: appName,
Schemes: bi.schemes,
PackageQueries: bi.packageQueries,
ManifestSnip: readOptionalText(filepath.Join(bi.pkgDir, "android", "manifest_snippets.xml")),
AppSnip: readOptionalText(filepath.Join(bi.pkgDir, "android", "application_snippets.xml")),
ManifestSnip: readOptionalText(filepath.Join(moduleRoot, "android", "manifest_snippets.xml")),
AppSnip: readOptionalText(filepath.Join(moduleRoot, "android", "application_snippets.xml")),
}
manifestBuffer, err := renderAndroidManifest(manifestSrc)
if err != nil {
@@ -839,6 +840,20 @@ func androidExtraJars(dir string) ([]string, error) {
return jars, nil
}
func moduleRootForDir(dir string) string {
current := dir
for {
if _, err := os.Stat(filepath.Join(current, "go.mod")); err == nil {
return current
}
parent := filepath.Dir(current)
if parent == current {
return dir
}
current = parent
}
}
func findNDK(androidHome string) (string, error) {
ndks, err := filepath.Glob(filepath.Join(androidHome, "ndk", "*"))
if err != nil {
+28
View File
@@ -83,6 +83,34 @@ func TestCopyTreeCopiesNestedResources(t *testing.T) {
}
}
func TestModuleRootForDirFindsOwningModule(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeTestFile(t, filepath.Join(root, "go.mod"), "module example.invalid/crew\n")
dir := filepath.Join(root, "cmd", "keepassgo")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("MkdirAll(%q) error = %v", dir, err)
}
if got := moduleRootForDir(dir); got != root {
t.Fatalf("moduleRootForDir(%q) = %q, want %q", dir, got, root)
}
}
func TestModuleRootForDirFallsBackToInputDir(t *testing.T) {
t.Parallel()
dir := filepath.Join(t.TempDir(), "cmd", "keepassgo")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("MkdirAll(%q) error = %v", dir, err)
}
if got := moduleRootForDir(dir); got != dir {
t.Fatalf("moduleRootForDir(%q) = %q, want %q", dir, got, dir)
}
}
func writeTestFile(t *testing.T, path, contents string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {