Reverse Java bindings with gomobile and fyne.io

228 Views Asked by At

I have a fairly simple application that in theory should use Android Java API to get the location ( inspired by this answer ):

package main

import (
    "Java/android/content/Context"
    "Java/android/location/LocationManager"
    "fyne.io/fyne/app"
    "fyne.io/fyne/widget"
)

func GetLocation() string {
    locationManager := ctx.GetSystemService(Context.LOCATION_SERVICE)
    location := locationManager.GetLastKnownLocation(LocationManager.GPS_PROVIDER)
    lat := location.GetLatitude()
    lng := location.GetLongitude()
    return fmt.Sprintf("%f:%f", lat, lng)
}

func main() {
    a := app.New()
    w := a.NewWindow("Location")

    loc := widget.NewLabel("Location defined " + GetLocation())
    w.SetContent(widget.NewVBox(
        loc,
    ))

    w.ShowAndRun()
}

Yet I can't get it working with the build command as fyne package -os android -appID com.user.fynedemo -icon icon.png - it fails with the error:

fyne package -os android -appID com.jdevelop.fynedemo -icon 1x1-00000000.png
go build -buildmode=c-shared -o /tmp/gomobile-work-028676736/lib/armeabi-v7a/libfynedemo.so fynedemo failed: exit status 1
a.go:3:8: package Java/android/content/Context is not in GOROOT (/usr/lib/go/src/Java/android/content/Context)
a.go:4:8: package Java/android/location/LocationManager is not in GOROOT (/usr/lib/go/src/Java/android/location/LocationManager)

To me it looks like Go->Java bindings are not generated, so I wonder what would be the first step for getting this working?

1

There are 1 best solutions below

0
On

Fyne applications do not expose gomobile Java or iOS bindings unfortunately. You can use CGo to execute any code, but the platform specific SDKs are not bound in to the toolkit.

The reason is to avoid exposing any platform specific APIs. A platform independent location API would be preferable, though Fyne does not have one yet.