New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/mobile: support vendored asset package on Android #26445

Open
derekwbrown opened this Issue Jul 18, 2018 · 15 comments

Comments

Projects
None yet
4 participants
@derekwbrown

derekwbrown commented Jul 18, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version devel +2acae87416 Mon Jul 9 23:04:30 2018 +0000 windows/amd64

go/mobile version is also latest from master

Does this issue reproduce with the latest release?

This is on latest master/built from source (to resolve other gomobile problem).

What operating system and processor architecture are you using (go env)?

This may be the same as #25255

Note my build environment is windows; i have reproduced the problem using a linux
build environment; target environment is android

go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\db\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=d:\dev\go
set GOPROXY=
set GORACE=
set GOROOT=c:\devtools\go1.11.devel
set GOTMPDIR=
set GOTOOLDIR=c:\devtools\go1.11.devel\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\db\AppData\Local\Temp\go-build567116882=/tmp/go-build -gno-record-gcc-switches
set VGOMODROOT=

What did you do?

I have an android app that creates a service. The service is built linking to a library
created with gomobile bind

What did you expect to see?

Everything works

What did you see instead?

When the service starts, it immediately crashes with
I/GoLog (18112): asset: no current JVM
I/Zygote ( 195): Process 18112 exited cleanly (1)

The first thing the service does is attempt to load an asset (which is present in the .apk)

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 18, 2018

Update. Same problem even if I compile my go directly into the app (with the correct gradle settings) rather than build an aar with gomobile bind and then link the aar to the app

@FiloSottile FiloSottile changed the title from asset: no current JVM on android service/gobind to x/mobile: asset: no current JVM on android service/gobind Jul 19, 2018

@gopherbot gopherbot added this to the Unreleased milestone Jul 19, 2018

@FiloSottile

This comment has been minimized.

Member

FiloSottile commented Jul 19, 2018

@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented Jul 19, 2018

Please provide a complete example that demonstrates this issue. I can't reproduce it, and there is even a test for reading assets in gomobile bind programs:

https://github.com/golang/mobile/blob/56e3592fa7a0582e84d783579d2eb8e6114e0312/bind/testdata/testpkg/testpkg.go#L228

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 19, 2018

Sample here. https://github.com/derekwbrown/testrepo/tree/master/android
compiled using gradlew.bat (my test env is windows)
sign it.
push to device
run it.
logcat output I/ActivityManager( 548): Start proc 11569:org.golang.example.android/u0a7 for activity org.golang.example.android/org.golang.example.bind.MainActivity W/linker (11569): Unsupported flags DT_FLAGS_1=0x8 W/linker (11569): libgojni.so: unused DT entry: type 0x6ffffffe arg 0x19c4 W/linker (11569): libgojni.so: unused DT entry: type 0x6fffffff arg 0x1 I/GoLog (11569): asset: no current JVM I/Zygote ( 195): Process 11569 exited cleanly (1) I/ActivityManager( 548): Process org.golang.example.android (pid 11569) has died W/ActivityManager( 548): Force removing ActivityRecord{238c1729 u0 org.golang.example.android/org.golang.example.bind.MainActivity t110}: app died, no saved state

@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented Jul 20, 2018

The asset succeeds in being read on an emulator (android 8.1.0) and my Google Pixel (android 8.1.0). What device are you using? Can you reproduce in an emulator? Is the log saying something relevant before the "no current JVM"?

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 20, 2018

Device is Nexus 7 running android 5.1.1
Was able to reproduce using the emulator; created a Nexus 7 emulator running Android 5.1.1

logs prior to no current JVM below; I had googled and the fact that they're warning level indicates that they're non-fatal?
W/linker ( 4069): Unsupported flags DT_FLAGS_1=0x8
W/linker ( 4069): libgojni.so: unused DT entry: type 0x6ffffffe arg 0x1a14
W/linker ( 4069): libgojni.so: unused DT entry: type 0x6fffffff arg 0x1
I/GoLog ( 4069): asset: no current JVM

@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented Jul 21, 2018

Unfortunately, even with an 5.1.1 emulator, I fail to reproduce the error. Are you using the latest checkout of gomobile (I'm on revision 371a4e8cb797fc2ab3793091822fda6179a73102) and have up to date binaries (go install golang.org/x/mobile/cmd/...)? Also, can you reproduce the error with Go 1.11 beta 2?

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 22, 2018

Did as you asked, same problem. I recompiled go with 1.11 beta 2

go version
go version devel +c814ac44c0 Thu Jul 19 21:30:27 2018 +0000 windows/amd64

then reinstalled and rebuilt gomobile

gomobile version
gomobile version +371a4e8 Thu Jul 19 12:32:16 2018 +0000 (android); androidSDK=c:\users\db\AppData\Local\Android\Sdk\platforms\android-27

Made sure I was also vendoring in the latest gomobile... contents of Gopkg.lock
[[projects]]
branch = "master"
name = "golang.org/x/mobile"
packages = [
"asset",
"internal/mobileinit"
]
revision = "371a4e8cb797fc2ab3793091822fda6179a73102"

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 22, 2018

For what it's worth, my android NDK is android-ndk-r17 for my gomobile init.
I also retried the example, commenting out the reading of the asset, and it works fine (just as a sanity check).

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 22, 2018

OK... so that we're looking at the exact same thing...
I added a dockerfile to the repo.
check out https://github.com/derekwbrown/testrepo/android from master
build the docker image from the docker directory
docker build -t android:fail .
that builds everything... just mount it and copy it
docker run -v /tmp:/mnttmp -t android:fail cp /tmp/app-release-unsigned.apk /mnttmp
The apk will be in your /tmp directory. Sign it and install it.

@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented Jul 22, 2018

Thank you for the docker instructions; I can reproduce the error now. The problem is that there are two versions of the golang.org/x/mobile packages: one in GOPATH/src, and one in the vendor/ directory created by dep. The package generated by gomobile bind uses the one in GOPATH/src, and at runtime, Seq.java supplies the context to that package. On the other hand, your hello.go calls into the vendored golang.org/x/mobile/asset package that hasn't been initialized. If you rm -rf vendor/golang.org/x/mobile, your example will run.

(Edited to correct my mistake in thinking Seq.java called into the vendored version.)

@eliasnaur eliasnaur changed the title from x/mobile: asset: no current JVM on android service/gobind to x/mobile: support vendored asset package on Android Jul 22, 2018

@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented Jul 22, 2018

I'm not sure what the correct fix is, but I changed the issue title to match the problem: multiple versions of the the asset package, which in turn means multiple versions of the internal/mobileinit package, which has global variables for the JVM and application context.

In Android Java API, you're expected to always pass in an android.context.Context, while the Go asset package expects access to assets without any context.

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 22, 2018

so, to clarify, I shouldn't vendor in the x/mobile package?

@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented Jul 22, 2018

That's right. I'm keeping the issue open for a future solution that safely allows vendoring mobile packages for bind programs.

@derekwbrown

This comment has been minimized.

derekwbrown commented Jul 22, 2018

Great. Thank you for all of your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment