xlab/android-go

NativeActivity.AssetManager is nil

FlorianUekermann opened this issue · 5 comments

The AssetManager field of the android.NativeActivity returned by app.NativeActivity.NativeActivity() is nil.

I'm building for API 24 with:
export CGO_CFLAGS="-march=armv7-a"
export GOOS=android
export GOARCH=arm
export GOARM=7

The device is a Pixel XL

xlab commented

Hi, by default all struct wrappers hold reference to the original struct, fields are not copied.
To copy all fields from the underlying C struct, including AssetManager reference for this case, use Deref():

a := app.NativeActivity.NativeActivity()
a.Deref()
// ... access fields

Thanks. I missed the Defer. However I am still having problems with the AssetManager. Maybe I misunderstood the rather sparse NDK docs. As soon as I get the app.onCreate event I do this:

app.Main(func(a app.NativeActivity) {
...
  for {
    select {
    case event := <-lifecycleEvents:
      switch event.Kind {
      case app.OnCreate:
        log.Println("onCreate")
        activity = a.NativeActivity()
        log.Println(activity == nil)
        activity.Deref()
        log.Println(activity.AssetManager == nil)
        var asset = android.AssetManagerOpen(activity.AssetManager, "textures/textures.txt", android.AssetModeStreaming)
        if asset == nil {
          panic("asset not found")
        }
...

I have verified that the apk contains "/assets/textures/textures.txt". The output I get is:

08-15 11:05:13.541  8519  8534 I inact   : onCreate
08-15 11:05:13.541  8519  8534 I inact   : false
08-15 11:05:13.541  8519  8534 I inact   : false
08-15 11:05:13.543  8519     0 E Go      : panic: asset not found
08-15 11:05:13.543  8519     0 E Go      : 
08-15 11:05:13.543  8519     0 E Go      : goroutine 7 [running, locked to thread]:
08-15 11:05:13.543  8519     0 E Go      : inact.Init.func1(0xe9bc7250, 0xe9bc44e8)
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/inact/android.go:61 +0x5b0
08-15 11:05:13.543  8519     0 E Go      : github.com/xlab/android-go/app.Main(0xe9b4ba30)
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/github.com/xlab/android-go/app/app.go:84 +0x38
08-15 11:05:13.543  8519     0 E Go      : inact.Init()
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/inact/android.go:96 +0x78
08-15 11:05:13.543  8519     0 E Go      : main.main()
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/voxelengine/main.go:28 +0x14
08-15 11:05:13.543  8519     0 E Go      : github.com/xlab/android-go/app/internal/callfn.CallFn(0xe9ac1258)
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/github.com/xlab/android-go/app/internal/callfn/callfn_arm.s:10 +0x18
08-15 11:05:13.543  8519     0 E Go      : created by github.com/xlab/android-go/app.callMain
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/github.com/xlab/android-go/app/app.go:50 +0x354

A bit of background: As you can see from the stack trace, this code is in a function (inact.Init()), which is part of an imported package. inact.Init() is the first function called in main() and the first line inside the function is runtime.LockOSThread(). Then loop over incoming lifecycle events until onCreate is received, in which case the code above is called.

Btw. I think it would be great if the app package had something like app.NativeActivity.OpenAssetReader(name string) io.Reader. I am happy to contribute that code, but I need to get it to run first :-).

xlab commented

Safe strings are disabled for this package, that means the strings you pass to binding should be \x00-ended, otherwise you're feeding the whole memory block :)

Try this:

 var asset = android.AssetManagerOpen(activity.AssetManager, "textures/textures.txt\x00", android.AssetModeStreaming)

I think it would be great if the app package had something like

I agree, it could also automatically add \x00 to the path. Thanks.

Since #7 is merged, I'll close this issue.

xlab commented

@MaVo159 I refactored the function to avoid unnecessary Go memory references into C, added mutex for activity-related operations, etc.

Please check with 3a14af7

Thanks for bringing that issue.