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

proposal: x/mobile/cmd/gomobile: define a build tag to distinguish gomobile-bind and gomobile-build #24490

Closed
hajimehoshi opened this issue Mar 22, 2018 · 7 comments
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile Proposal
Milestone

Comments

@hajimehoshi
Copy link
Member

hajimehoshi commented Mar 22, 2018

I'm trying to build a library that can run both on gomobile-bind and gomobile-build. Importing "golang.org/x/mobile/app" must be present for gomobile-build, while it must not be present for gomobile-bind. Thus, I'd need to separate some compiled files: some for gomobile-bind and the others for gomobile-build. If there'd be a build tag to distinguish them, it'd be very useful.

@gopherbot gopherbot added this to the Unreleased milestone Mar 22, 2018
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label Mar 22, 2018
@bcmills bcmills changed the title x/mobile/cmd/gomobile: proposal: a build tag to distinguish gomobile-bind and gomobile-build proposal: x/mobile/cmd/gomobile: define a build tag to distinguish gomobile-bind and gomobile-build Mar 22, 2018
@bradfitz
Copy link
Contributor

bradfitz commented Apr 2, 2018

@eliasnaur, thoughts?

@eliasnaur
Copy link
Contributor

Perhaps build tags are the answer, but I'd like to know a bit more about your use case first.

What is the purpose of a library that is both bindable and buildable? Bind is for producing a Go library to use from native code, while build is for producing a complete program ready to run. In regular Go, you would have the library part in some package A (gomobile bind), and then have a different package main that imports and uses A (gomobile build).

Then, why can't you import golang.org/x/mobile/app in bind mode?

@hajimehoshi
Copy link
Member Author

What is the purpose of a library that is both bindable and buildable? Bind is for producing a Go library to use from native code, while build is for producing a complete program ready to run. In regular Go, you would have the library part in some package A (gomobile bind), and then have a different package main that imports and uses A (gomobile build).

In my use case, I have a game lib containing a main loop for desktop usages, and a package for gomobile-bind that uses the game lib. From the mobile, the game lib doesn't activate the mail loop. Now I wanted to make the game lib accept gomobile-build, and encounters the problem that gomobile-build require golang.org/x/mobile/app and gomobile-bind not.

Well, splitting the game lib into two parts would work: one includes the main loop and the other not. It's possible, but that would require a big code change.

Then, why can't you import golang.org/x/mobile/app in bind mode?

I thought this caused a runtime error, but let me double-check.

@hajimehoshi
Copy link
Member Author

hajimehoshi commented Apr 3, 2018

Then, why can't you import golang.org/x/mobile/app in bind mode?

I thought this caused a runtime error, but let me double-check.

  1. Fix my game lib (Ebiten: revision 2f4a51b373f2f06756ef08abaa7ccb185388be8a) to import golang.org/x/mobile/app
diff --git a/internal/ui/mainloop_gomobilebuild.go b/internal/ui/mainloop_gomobilebuild.go
index afeba8fc..1de50304 100644
--- a/internal/ui/mainloop_gomobilebuild.go
+++ b/internal/ui/mainloop_gomobilebuild.go
@@ -13,7 +13,7 @@
 // limitations under the License.
 
 // +build android ios
-// +build gomobilebuild
+//// +build gomobilebuild
 
 package ui
 
diff --git a/internal/ui/mainloop_notgomobilebuild.go b/internal/ui/mainloop_notgomobilebuild.go
index e258f013..2e9e9d55 100644
--- a/internal/ui/mainloop_notgomobilebuild.go
+++ b/internal/ui/mainloop_notgomobilebuild.go
@@ -14,6 +14,7 @@
 
 // +build android ios
 // +build !gomobilebuild
+// +build ignore
 
 package ui
  1. Build my game (go-inovation)
gomobile bind -target android -javapkg com.hajimehoshi.goinovation -o ./mobile/android/inovation/inovation.aar github.com/hajimehoshi/go-inovation/mobile
  1. Open AndroidStudio project at go-inovation and run, then the app crashed with this message:
04-03 20:07:44.937 29956-29956/com.hajimehoshi.goinovation A/Go: cannot find org/golang/app/GoNativeActivity
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: GetMethodID received NULL jclass
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]     in call to GetMethodID
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]     from java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.String)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470] "main" prio=5 tid=1 Runnable
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   | group="main" sCount=0 dsCount=0 obj=0x75c45fa8 self=0x7f74495a00
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   | sysTid=29956 nice=-10 cgrp=default sched=0/0 handle=0x7f78307a98
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   | state=R schedstat=( 391617039 2359639 194 ) utm=32 stm=7 core=7 HZ=100
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   | stack=0x7fcb5ca000-0x7fcb5cc000 stackSize=8MB
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   | held mutexes= "mutator lock"(shared held)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #00 pc 000000000047af34  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+220)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #01 pc 000000000047af30  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+216)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #02 pc 000000000044f460  /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+472)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #03 pc 00000000002eeacc  /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1128)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #04 pc 00000000002ef1b4  /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+120)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #05 pc 00000000001024f4  /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+156)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #06 pc 0000000000101994  /system/lib64/libart.so (_ZN3art11ScopedCheck13CheckInstanceERNS_18ScopedObjectAccessENS0_12InstanceKindEP8_jobjectb+556)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #07 pc 00000000000ffa3c  /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE+1120)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #08 pc 0000000000104204  /system/lib64/libart.so (_ZN3art8CheckJNI19GetMethodIDInternalEPKcP7_JNIEnvP7_jclassS2_S2_b+660)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   native: #09 pc 0000000000368204  /data/app/com.hajimehoshi.goinovation-1/lib/arm64/libgojni.so (JNI_OnLoad+224)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.Runtime.nativeLoad(Native method)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.Runtime.doLoad(Runtime.java:1072)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   - locked <0x08ba542f> (a java.lang.Runtime)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.Runtime.loadLibrary0(Runtime.java:987)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   - locked <0x08ba542f> (a java.lang.Runtime)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.System.loadLibrary(System.java:1562)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at go.LoadJNI.<clinit>(LoadJNI.java:23)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.Class.classForName!(Native method)
04-03 20:07:44.956 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.Class.forName(Class.java:400)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.Class.forName(Class.java:326)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at go.Seq.<clinit>(Seq.java:28)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at go.Seq.touch(Seq.java:45)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at com.hajimehoshi.goinovation.mobile.Mobile.<clinit>(Mobile.java:11)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at com.hajimehoshi.goinovation.mobile.Mobile.isRunning(Native method)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at com.hajimehoshi.goinovation.EbitenGLSurfaceView.onLayout(EbitenGLSurfaceView.java:94)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.View.layout(View.java:17641)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.View.layout(View.java:17641)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewGroup.layout(ViewGroup.java:5575)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.View.layout(View.java:17641)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewGroup.layout(ViewGroup.java:5575)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.View.layout(View.java:17641)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewGroup.layout(ViewGroup.java:5575)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.View.layout(View.java:17641)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewGroup.layout(ViewGroup.java:5575)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.View.layout(View.java:17641)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewGroup.layout(ViewGroup.java:5575)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at com.android.internal.policy.DecorView.onLayout(DecorView.java:727)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.View.layout(View.java:17641)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewGroup.layout(ViewGroup.java:5575)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6344)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.Choreographer.doCallbacks(Choreographer.java:686)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.Choreographer.doFrame(Choreographer.java:621)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.os.Handler.handleCallback(Handler.java:751)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.os.Handler.dispatchMessage(Handler.java:95)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.os.Looper.loop(Looper.java:154)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at android.app.ActivityThread.main(ActivityThread.java:6121)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at java.lang.reflect.Method.invoke!(Native method)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
04-03 20:07:44.957 29956-29956/com.hajimehoshi.goinovation A/art: art/runtime/java_vm_ext.cc:470]

@gopherbot
Copy link

Change https://golang.org/cl/104395 mentions this issue: app: support importing the package for bind programs

@eliasnaur
Copy link
Contributor

Does http://golang.org/cl/104395 help?

And since I sometimes miss the obvious: have you tried the -tags flag? It should work for both gomobile bind and gomobile build.

@hajimehoshi
Copy link
Member Author

hajimehoshi commented Apr 3, 2018

Does http://golang.org/cl/104395 help?

Ah yes, if that CL avoid unnecessary loading Java class for gomobile-bind, I think this would solve the problem, and I'd not need the build tag to distinguish bind and build.

And since I sometimes miss the obvious: have you tried the -tags flag? It should work for both gomobile bind and gomobile build.

Yes, -tags works both on build and bind.

@golang golang locked and limited conversation to collaborators Apr 5, 2019
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 10, 2021
Delay loading of the getRune method ID to the first GoNativeActivity
is created. That way, importing golang.org/x/mobile/app will not
crash, even for gomobile bind programs that don't include (or use)
GoNativeActivity.

Fixes golang/go#24490

Change-Id: I4bf90e067700451f7c026e53165b6614366d7a94
Reviewed-on: https://go-review.googlesource.com/104395
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 11, 2021
Delay loading of the getRune method ID to the first GoNativeActivity
is created. That way, importing golang.org/x/mobile/app will not
crash, even for gomobile bind programs that don't include (or use)
GoNativeActivity.

Fixes golang/go#24490

Change-Id: I4bf90e067700451f7c026e53165b6614366d7a94
Reviewed-on: https://go-review.googlesource.com/104395
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile Proposal
Projects
None yet
Development

No branches or pull requests

4 participants