Parsing assets from src/main/assets folder with gomobile

381 Views Asked by At

I have a gomobile native Android app which embedded into webview. This setup works nice, I even managed to use the gorilla mux router, but I wanted to use pongo2 templating engine also and I run into a problem when I try to parse my template html files.

I have the following line in my ./app/build.gradle file:

android {
  sourceSets {
    main {
      assets.srcDirs = ['assets']
    }
  }
}

I placed my html file here, which I try to parse on the index route: ./app/src/main/assets/views/index.html.

func index(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Content-Type", "text/html; charset=utf-8")
  //res, err := http.Get("https://google.com")
  res, err := http.Get("file:///android_asset/views/index.html")
  if err != nil {
    fmt.Fprintf(w, "could not execute: %v\n", err)
  }
  text, err := ioutil.ReadAll(res.Body)
  res.Body.Close()
  if err != nil {
    fmt.Fprintf(w, "could not execute: %v\n", err)
  }
  fmt.Fprintf(w, "%s", text)
}

This setup without the pongo2 rendering, only for the simpler example. Parsing the html file from google works and displayed in the app when I open it. But when I try to parse file:///android_asset/views/index.html file, I'm getting the following error:

enter image description here

EDIT:

I discovered asset.Open in godoc, so I modified my code:

func index(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/html; charset=utf-8")

    a, err := asset.Open("views/index.html")
    if err != nil {
        fmt.Fprintf(w, "could not execute: %v\n", err)
    }
    defer a.Close()
    fmt.Fprintf(w, "File contents: %s\n", a)
}

I still manually copy my template files to ./app/src/main/assets/views/index.html folder, but the end result is the same.

EDIT2:

I realised the problem was when I copied my template files into ./app/src/main/assets/views folder, there wasn't any file in the ./app/src/main/assets folder (just only the views subfolder), hence these assets didn't copied into the apk beause of that. So now I copy my files directly in the assets folder and my index.html location is ./app/src/main/assets/index.html. This way my templates do gets copied into the apk... Now I'm getting the following screen. I think I should decode somehow the text:

enter image description here

Solution for embedding a single html file:

I had to use ioutil.ReadAll, because asset.File is implemented as the following interface:

type File interface {
    io.ReadSeeker
    io.Closer
}

So my final code to parsing html files:

func index(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/html; charset=utf-8")

    a, err := asset.Open("index.html")
    if err != nil {
        fmt.Fprintf(w, "could not execute: %v\n", err)
    }
    defer a.Close()

    b, err := ioutil.ReadAll(a)
    if err != nil {
        fmt.Fprintf(w, "could not execute: %v\n", err)
    }
    fmt.Fprintf(w, "File contents: %s\n", b)
}

Which resulting:

enter image description here

However, this still not solving why referencing files with file:///android_asset/views/index.html not working, which is needed for static files like css/js, so the question remains open. Maybe serving the assets from the java code?

0

There are 0 best solutions below