androidandroid-studiogogomobile

File is not recognized after generating the ".APK" file from Go to Android using gomobile tool


I am working on a project in which i need to generate an APK file form a Golang code base using gomobile tool. the plan is to be able to run it on android tablet. When I install the apk file on the tablet, through the Android Studio I get the following error.

 E/Go: panic: open C:/Users/ash/OneDrive/Desktop/go-workSpace/src/Myproject/config.json: no such file or directory

However, after clicking on the given path in the error message, the Android Studio opens the the file it is complaining to find (config.json)

Here is the source code I have in Go

exDir := "C:/Users/ash/OneDrive/Desktop/go-workSpace/src/Myproject/"    
configFile, err := ioutil.ReadFile(filepath.Join(filepath.Dir(exDir), "config.json")) // Look for the config file in the same directory as the executable
    
Check(err)

func Check(err error) {
    if err != nil {
        fmt.Println("Received generic error, panicking")
        fmt.Println(err)
        panic(err)
    }
}

Any suggestion how to fix the file path?


Solution

  • After reviewing this thread. Here is the official go support page. So, I think I need to

    1. move all the files into a directory called assets.
    2. The assets directory is embedding to the ".APK" can be accessed through Android code base.
    import "golang.org/x/mobile/asset"  
    
    jsonFile, errOpen := asset.Open(config.json)
    if errOpen != nil {
        fmt.Println(errOpen)
    }
    defer jsonFile.Close()
    buf, errRead := ioutil.ReadAll(jsonFile)
    if errRead != nil {
        fmt.Println(errRead)
    }