JNI interop for dart

257 views
Skip to first unread message

Astitva Raj

unread,
Apr 4, 2022, 9:47:46 AM4/4/22
to dart-gsoc
Hello Mentors,
I am Astitva. I am interested in working on JNI interop for dart in GSOC 2022 and I am working on good sample project. I am able to call a Java method from dart but I am not able to call a dart method from java. Can you please guide me how I can call dart method from java.
Thanks
Astitva

Daco Harkes

unread,
Apr 4, 2022, 9:57:37 AM4/4/22
to Astitva Raj, dart-gsoc
Hi Astitva,

Nice to meet you!

You should be able to call a Dart method from C, and call that C method from Java.

The Dart method you can give to C as a Pointer with Pointer.fromFunction.

To call a C function from Java, you need to declare it external in Java and give it the right name in C.
You can look up tutorials on how to do this [1,2].


Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 


--
You received this message because you are subscribed to the Google Groups "dart-gsoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dart-gsoc+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dart-gsoc/3104d91d-a8ec-4e31-9a19-fa4b6cf846f1n%40googlegroups.com.

Astitva Raj

unread,
Apr 4, 2022, 10:02:18 AM4/4/22
to dart-gsoc

Thankyou sir for information.

Astitva Raj

unread,
Apr 4, 2022, 1:01:23 PM4/4/22
to dart-gsoc
Hello sir,
Thank you for your guidance because of you I have successfully developed flutter application for sample project. But it is quite simple. Can you please tell me that should I develop more complex project or simple one is sufficient? 
In this project dart calls the JAVA function and then JAVA Function calls the dart function to calculate the double of value of counter.
I have attached the screenshot of application.
WhatsApp Image 2022-04-04 at 9.55.09 PM.jpeg
This is how application works to calculate result.
Untitled.jpg
Thanks
Astitva

Daco Harkes

unread,
Apr 5, 2022, 3:43:05 AM4/5/22
to Astitva Raj, dart-gsoc, Liam Appelbe
Hi Astitva,

Cool!

You can try more complicated examples:
- Sharing some native memory between Dart and Java without copying? (with Pointer in Dart and PrimitiveArray/ExternalArrayPointer/... in Java).
- Exposing a Java object in Dart so that you have a Dart API in which one can call methods.

And then the next step would be to think about how you could automatically generate that code from a Java API.

Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 

Mahesh Hegde

unread,
Apr 6, 2022, 7:02:22 AM4/6/22
to dart-gsoc
Hi

> PrimitiveArray/ExternalArrayPointer/... in Java

As far as google could help, ExternalArrayPointer is from JNIWrapper library, which seems proprietary. Did you have anything else in mind?

I was able to share native memory using ByteBuffer though. That must be what JNIWrapper builds on, because so far as I know any of the array APIs may copy when passing natively allocated memory.

Cheers
Mahesh

Daco Harkes

unread,
Apr 6, 2022, 7:06:24 AM4/6/22
to Mahesh Hegde, dart-gsoc
Hi,

ByteBuffer sounds good. I'm not so familiar with what to use on the Java side. As long as we can share bytes without copying we're good! :-)

Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 

Message has been deleted

Daco Harkes

unread,
Apr 8, 2022, 4:41:28 AM4/8/22
to Mahesh Hegde, dart-gsoc
Hi Mahesh,

Only lists that are backed by native memory can be shared from Dart to C/Java.
There are only two such types of list:
1. Pointer from dart:ffi
2. "external" TypedData (external means memory external to Dart), which can be constructed with Pointer.asTypedList.

> Is there any way to get reference to a dart List<T>'s underlying buffer?
No, List<int> is most likely a list in Dart memory, so it cannot be shared with C/Java. The garbage collector might move the list at any time and the representation is not compatible with C.

> Does dart make any guarantees about int representation?
The representation in Dart of integers cannot be shared with C/Java."Smi"s, or small integers, are bit shifted 1 bit to the left, and "Mint"s or medium size integers are boxed in an object.

Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 


On Fri, Apr 8, 2022 at 10:03 AM Mahesh Hegde <net.ma...@gmail.com> wrote:
Hi Daco,

Take a look at gsoc_jni2 repo I shared with you, particularly lib/Toast.dart Toast#calcByteSum, this seems to work with typed lists like UintList.

I have a question:
* Is there any way to get reference to a dart List<T>'s underlying buffer? I thought it is not possible because ints are technically object in dart. Does dart make any guarantees about int representation?
Reply all
Reply to author
Forward
0 new messages