Skip to content
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
Open

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

derekwbrown opened this issue Jul 18, 2018 · 15 comments
Labels
mobile Android, iOS, and x/mobile OS-Android
Milestone

Comments

@derekwbrown
Copy link

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
Copy link
Author

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 added OS-Android mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 19, 2018
@FiloSottile FiloSottile changed the title asset: no current JVM on android service/gobind 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
Copy link
Contributor

/cc @eliasnaur

@eliasnaur
Copy link
Contributor

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

@eliasnaur eliasnaur added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 19, 2018
@derekwbrown
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
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 removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Jul 22, 2018
@eliasnaur eliasnaur changed the title x/mobile: asset: no current JVM on android service/gobind x/mobile: support vendored asset package on Android Jul 22, 2018
@eliasnaur
Copy link
Contributor

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
Copy link
Author

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

@eliasnaur
Copy link
Contributor

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

@derekwbrown
Copy link
Author

Great. Thank you for all of your help.

jeesk pushed a commit to jeesk/clipboard that referenced this issue Jun 17, 2022
changkun added a commit to golang-design/clipboard that referenced this issue Oct 30, 2022
See golang/go#26445 and golang/go#33254

Co-authored-by: songqifu <songqifu@linakesi.com>
Co-authored-by: Changkun Ou <hi@changkun.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mobile Android, iOS, and x/mobile OS-Android
Projects
None yet
Development

No branches or pull requests

4 participants