0

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?

Ashkanxy
  • 2,380
  • 2
  • 6
  • 17
  • 1
    Consider this: do you think every single tablet to install the app will have `C:/Users/ash/OneDrive/Desktop/go-workSpace/src/Myproject/config.json` on its filesystem? Probably not... Perhaps you actually want to use an [asset](https://stackoverflow.com/a/49418556/6069012). – Gavin Jul 01 '21 at 18:17
  • @Gavin Thanks for your comment. Yes, this is for testing. I should definitely use some standard utilities to make it general for all the apps. Just hard coded for now to find the root cause of app crashing. – Ashkanxy Jul 01 '21 at 18:35
  • 1
    My point is, your tablet likely does not have that path available (even though *your computer* does), which is why the application crashes but you can find the file. – Gavin Jul 01 '21 at 19:45
  • 1
    Assuming you are turning your **config.json** file into a `map[string]string` or something similar, comment out that file read line and just hardcode the stuff in **config.json** inline and test out your code. That file path is a _Windows_ specific file path. Android is Linux based and there is no `C:` drive. Or you can copy **config.json** onto the tablet in a known location and put that path in the read line – RisingSun Jul 01 '21 at 21:24

1 Answers1

0

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)
}
Ashkanxy
  • 2,380
  • 2
  • 6
  • 17