When developing with gomobile for Android. How can I include assets (html, js, images) inside the apk file? I have a web server running inside the apk which works inside the apk as well as with go run, but when packaged inside the apk on the phone the service cannot find it's assets/static files.
Asked
Active
Viewed 747 times
1 Answers
4
You must put your assets within a directory called 'assets'.
See this example:
https://github.com/udhos/fugo/tree/master/demo/invader/assets
Then you can use the package golang.org/x/mobile/asset to load the desired asset:
import "golang.org/x/mobile/asset"
f, err := asset.Open("image.png")
Sample code taken from https://github.com/udhos/fugo/blob/master/demo/invader/main.go:
func loadFull(name string) ([]byte, error) {
f, errOpen := asset.Open(name)
if errOpen != nil {
return nil, errOpen
}
defer f.Close()
buf, errRead := ioutil.ReadAll(f)
if errRead != nil {
return nil, errRead
}
log.Printf("loaded: %s (%d bytes)", name, len(buf))
return buf, nil
}

Everton
- 12,589
- 9
- 47
- 59
-
1Thank you so much, will try that. I guess then that I'm out of luck using the built-in static file middleware that Echo (my web server that will run on the Android device) uses, but I can write my own static file serving middleware whith this info. ;) – tirithen Mar 22 '18 at 11:15
-
@Everton thanks, it works for me as well. By chance do you know how we can read files in nested directories as well? like if the file is in "images/image.png" – Ashkanxy Jul 14 '21 at 22:18