x/mobile: Samples for reverse binding in gomobile

226 views
Skip to first unread message

Jay Sharma

unread,
Sep 6, 2019, 7:29:33 AM9/6/19
to golang-nuts
Hello All,

I can not see any samples for reverse binding (Calling function from go layer of java layer) in gomobile.

Is anybody tried any samples of reverse binding.

Thank in advance.

Elias Naur

unread,
Sep 6, 2019, 10:10:48 AM9/6/19
to golang-nuts


fredag den 6. september 2019 kl. 13.29.33 UTC+2 skrev Jay Sharma:
Hello All,

I can not see any samples for reverse binding (Calling function from go layer of java layer) in gomobile.



Jay Sharma

unread,
Sep 9, 2019, 6:09:02 AM9/9/19
to golang-nuts
Hello @elias

Thank you for reply.

I have tried the sample with reverse binding:

import (
        "Java/java/lang/System"
)

func JavaCall() {
         t := System.CurrentTimeMillis()
         log.Println("Init", "[Test] Called java function return value is: ", t);
}

Built the above with gomobile and generated the binding.
It is working fine.

Next I want to try with my own classes in java.. I want to define class and call it from go.
Can you please suggest, in that case how to build with gomobile ?

Elias Naur

unread,
Sep 9, 2019, 8:10:25 AM9/9/19
to Jay Sharma, golang-nuts
On Mon Sep 9, 2019 at 3:09 AM Jay Sharma wrote:
>
> *Next I want to try with my own classes in java.. I want to define class
> and call it from go. *
> *Can you please suggest, in that case how to build with gomobile ?*
>

I think you can use the -classpath option to gomobile.

-- elias

Jay Sharma

unread,
Sep 10, 2019, 7:26:00 AM9/10/19
to golang-nuts
Hello @elias,

I tried the following:

1. Created a java class :

package reversebinding;

public class RBinding {
 public static String getStringFromJava() {
        return "Hello from java !!";
    }
}

2. Generated the .class file for this file.

3. Generated the android binding using gomobile tool and used -classpath="Path to my .class file"

4. Binding is generated successfully.

But when I used that generated (.aar) file in my android app and tried to trigger the api it is crashing;

2019-09-10 16:36:54.199 9400-9430/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:134: testFunction [Test]
2019-09-10 16:36:54.199 9400-9430/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:136: [Test] Now going to call a java system function (System.CurrentTimeMillis()).....
2019-09-10 16:36:54.200 9400-9434/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:138: [Test] Called java function return value is:  1568113614199
2019-09-10 16:36:54.200 9400-9434/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:140: [Test] Now going to call my java function.....
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go: panic: runtime error: invalid memory address or nil pointer dereference
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7568010708]
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go: goroutine 17 [running, locked to thread]:
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go: test.testFunction(0x4000000738)
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go:     /home/test/2019/test/test.go:141 +0x190
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go: main.proxytest__testFunction(...)
2019-09-10 16:36:54.203 9400-0/com.sample com.sample E/Go:     /tmp/gomobile-work-071468276/src/gobind/go_testmain.go:199
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go: main._cgoexpwrap_5427a26f9204_proxytest__testFunction(0x8020080280200802)
2019-09-10 16:36:54.203 9400-0/com.sample  E/Go:     _cgo_gotypes.go:606 +0x1c
2019-09-10 16:36:54.204 9400-9423/com.sample  A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 9423 (Thread-2)

Can you please provide you opinion. Let me know if any other information is needed.

Thanks....

Jay Sharma

unread,
Sep 10, 2019, 9:19:56 AM9/10/19
to golang-nuts
Just one more information:

I am calling from my go code like this:
RBinding.GetStringFromJava()

Elias Naur

unread,
Sep 10, 2019, 11:25:47 AM9/10/19
to Jay Sharma, golang-nuts
On Tue Sep 10, 2019 at 4:26 AM Jay Sharma wrote:
> ------=_Part_1258_983331168.1568114760099
> Content-Type: text/plain; charset="UTF-8"
>
> Hello @elias,
>
> I tried the following:
>
> 1. Created a java class :
>
> package reversebinding;
>
> public class RBinding {
> public static String getStringFromJava() {
> return "Hello from java !!";
> }
> }
>
> 2. Generated the .class file for this file.
>
> 3. Generated the android binding using gomobile tool and used
> -classpath="Path to my .class file"
>
> 4. Binding is generated successfully.
>
> *But when I used that generated (.aar) file in my android app and tried to
> trigger the api it is crashing; *
The error is arguably not very helpful. Are you sure there is not another error
before the nil pointer exception? In any case, did you ensure that the .class
is included in the Android apk? An easy check is to try to call your method
from Java before trying from Go.

-- elias

Jay Sharma

unread,
Sep 11, 2019, 6:51:16 AM9/11/19
to golang-nuts
Hi Elias,

The provided logs:
2019-09-10 16:36:54.199 9400-9430/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:134: testFunction [Test]
2019-09-10 16:36:54.199 9400-9430/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:136: [Test] Now going to call a java system function (System.CurrentTimeMillis())..
2019-09-10 16:36:54.200 9400-9434/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:138: [Test] Called java function return value is:  1568113614199

In this you can see I am able to call System.CurrentTimeMillis() and printing in logs.

This is the next line of code where I am trying to call my own class java function from go:
2019-09-10 16:36:54.200 9400-9434/com.sample  E/GoLog: 2019/09/10 11:06:54 test.go:140: [Test] Now going to call my java function....

After this log I am actually calling like this:
RBinding.getStringFromJava() and it is crashing.


I have one doubt when we build using gomobile in that I am giving the classpath to my .class java file.
Will it be included in .aar or I have to include that .class file along with .aar in my android application.

Thanks.

Elias Naur

unread,
Sep 11, 2019, 7:06:20 AM9/11/19
to Jay Sharma, golang-nuts
On Wed Sep 11, 2019 at 3:51 AM Jay Sharma wrote:
>
>
> I have one doubt when we build using gomobile in that I am giving the
> classpath to my .class java file.
> *Will it be included in .aar or I have to include that .class file along
> with .aar in my android application. *
>

The class file is not automatically included, that wasn't clear from my earlier
reply. Please include the class file in your project and try my suggestion to
call your code from Java.

-- elias

Jay Sharma

unread,
Sep 11, 2019, 10:20:07 AM9/11/19
to golang-nuts
Thank you very much Elias : with your suggestion it is working fine.

Just last query:

Currently, I called static method of my java class:

package reversebinding;

public class RBinding {
 
public static String getStringFromJava() {
         
return "Hello from java !!";
     
}
 
}

Now, I want to create object of this class in go and want to call method using that object from go.

I was checking your proposal (https://github.com/golang/go/issues/16876) but could not able to make it. :(
Can you please help me , how to do that?

Thanks.

Elias Naur

unread,
Sep 11, 2019, 12:11:13 PM9/11/19
to Jay Sharma, golang-nuts
On Wed Sep 11, 2019 at 7:20 AM Jay Sharma wrote:
>
>
> *Now, I want to create object of this class in go and want to call method
> using that object from go. *
>
> I was checking your proposal (https://github.com/golang/go/issues/16876)
> but could not able to make it. :(

The proposal suggests calling the generated New method on the object.
Did you try that? If so, what went wrong?

-- elias

Jay Sharma

unread,
Sep 12, 2019, 3:44:21 AM9/12/19
to golang-nuts
Hello Elias,

Following are the lines from proposals:

Creating new Java instances

To create a new instance of a Java class, use the New function defined in the class package. For example:

import (
"Java/java/lang/Object" "Java/java/lang"
)

func
NewObject() lang.Object {
   
return Object.New()
}

How can I modify it for my class ?

I know, It is too much to ask. I got confused.

Thanks for your help.

 

Jay Sharma

unread,
Sep 12, 2019, 5:35:20 AM9/12/19
to golang-nuts
Hello Elias,

Following is my java file:


package reversebinding;

public class RBinding {
 
public static String getStringFromJava() {
       
return "Hello from java !!";
   
}
}




I tried the following in my go file :



import "Java/reversebinding/RBinding"


// creating object like this:

javaObj
:= JavaAPI.New()
javaObj
.GetStringFromJava()

But while building using the gomobile it is giving following error:
JavaObj.GetStringFromJava undefined (type Java.Reversebinding_JavaAPI has no field or method GetStringFromJava)
undefined: val


If I call using class name: it is working fine. but with object it is not working.

Can  you please provide your suggestion ?

Thanks.

Elias Naur

unread,
Sep 12, 2019, 7:01:05 AM9/12/19
to Jay Sharma, golang-nuts
On Thu Sep 12, 2019 at 2:35 AM Jay Sharma wrote:
>
> *Hello Elias,*
>
> *Following is my java file: *
>
>
> package reversebinding;
>
> public class RBinding {
> public static String getStringFromJava() {
> return "Hello from java !!";
> }
> }
>
>
>

Your Java method is declared as "static"; static methods are exported as
regular functions in reverse bindings. Remove "static" and you might have
better luck calling it.

>
> *I tried the following in my go file : *
>
>
>
> import "Java/reversebinding/RBinding"
>
>
> // creating object like this:
>
> javaObj := JavaAPI.New()
> javaObj.GetStringFromJava()
>
> *But while building using the gomobile it is giving following error: *

Jay Sharma

unread,
Sep 12, 2019, 8:23:24 AM9/12/19
to golang-nuts
Hello Elias,

First of all, Thank you very much for help. :)

After I remove the static from function it worked well.

Thanks

psyh...@gmail.com

unread,
Feb 4, 2020, 8:21:32 AM2/4/20
to golang-nuts
Hello, Jay.
Can I ask you some questions about gomobile reverse binding in private messaging?
Reply all
Reply to author
Forward
0 new messages