Passing objects from Java to native

1,427 views
Skip to first unread message

George

unread,
Oct 15, 2009, 2:13:08 PM10/15/09
to android-ndk
The samples in the NDK show how to pass simple parameters from the
Java layer to native. With the NDK, can someone give me an example of
passing, say, a Rect from Java to native?

Currently, the NDK eliminated the need to register the native function
on JNI_Load by following a naming convention for the functions.
Example: com.example.SanAngeles.DemoRenderer.nativeRender became
Java_com_example_SanAngeles_DemoRenderer_nativeRender. Does the NDK
provide any such shortcuts even when passing objects?

Also, can anyone point me to any examples using the NDK with C++ - I
am particularly interested in the lifecycle management of the native
objects created. Example: Let's say I want to create a mirror Rect
object in native for the Rect Java object and off-load all the
functionality to native C++ object. When do I create the native Rect
object, when do I destroy it?

Thanks,
George

Jack Palevich

unread,
Oct 15, 2009, 2:48:23 PM10/15/09
to andro...@googlegroups.com
I think the Android team's JNI experts are pretty busy right now, so it may be some time before you get an answer from them.

In the meantime, the good news is that these are all JNI questions. They aren't really specific to Android or to the NDK. And so with a little luck you should be able to figure out the answer yourself by conducting web searches and by reading JNI documentation.

A good place to start might be:


(I'd love to help more, but so far I've only had to pass primitive types and arrays of primitive types between Java and native code.)

Doug Schaefer

unread,
Oct 15, 2009, 3:00:07 PM10/15/09
to andro...@googlegroups.com
There's also a really good book called Essential JNI. 10 year later it's still a really good reference.

http://www.amazon.com/Essential-Jni-Java-Native-Interface/dp/0136798950

It covers things like passing objects (jobject) back and forth to native. And how to manage the lifecycle.

fadden

unread,
Oct 15, 2009, 3:28:53 PM10/15/09
to android-ndk
On Oct 15, 11:13 am, George <bipi...@gmail.com> wrote:
> The samples in the NDK show how to pass simple parameters from the
> Java layer to native. With the NDK, can someone give me an example of
> passing, say, a Rect from Java to native?

The parameter type will be "jobject". Pass it like anything else.
Some specialization is provided for class objects, arrays, and
primitive arrays.

> Currently, the NDK eliminated the need to register the native function
> on JNI_Load by following a naming convention for the functions.
> Example: com.example.SanAngeles.DemoRenderer.nativeRender became
> Java_com_example_SanAngeles_DemoRenderer_nativeRender. Does the NDK
> provide any such shortcuts even when passing objects?

I'm not entirely sure what you're asking. An unambiguous method
doesn't include the parameter types in the signature name, so the arg
types don't matter. If the method is overloaded you may have to
include the "mangled" signature; see:

http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp615

> Also, can anyone point me to any examples using the NDK with C++ - I
> am particularly interested in the lifecycle management of the native
> objects created. Example: Let's say I want to create a mirror Rect
> object in native for the Rect Java object and off-load all the
> functionality to native C++ object. When do I create the native Rect
> object, when do I destroy it?

I'm not sure what this means and why it applies particularly to C++.
If you can't find your answer in the various references, please
describe what you're after in more detail.

Some links:

http://java.sun.com/docs/books/jni/html/jniTOC.html
http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html
http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=docs/jni-tips.html;hb=HEAD

George

unread,
Oct 16, 2009, 1:45:24 PM10/16/09
to android-ndk
Thanks guys. I should have been more specific. Will look through the
references and be back with specific questions.

George

On Oct 15, 12:28 pm, fadden <fad...@android.com> wrote:
> On Oct 15, 11:13 am, George <bipi...@gmail.com> wrote:
>
> > The samples in the NDK show how to pass simple parameters from the
> > Java layer to native. With the NDK, can someone give me an example of
> > passing, say, a Rect from Java to native?
>
> The parameter type will be "jobject".  Pass it like anything else.
> Some specialization is provided for class objects, arrays, and
> primitive arrays.
>
> > Currently, the NDK eliminated the need to register the native function
> > on JNI_Load by following a naming convention for the functions.
> > Example: com.example.SanAngeles.DemoRenderer.nativeRender became
> > Java_com_example_SanAngeles_DemoRenderer_nativeRender. Does the NDK
> > provide any such shortcuts even when passing objects?
>
> I'm not entirely sure what you're asking.  An unambiguous method
> doesn't include the parameter types in the signature name, so the arg
> types don't matter.  If the method is overloaded you may have to
> include the "mangled" signature; see:
>
>  http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/design.ht...
>
> > Also, can anyone point me to any examples using the NDK with C++ - I
> > am particularly interested in the lifecycle management of the native
> > objects created. Example: Let's say I want to create a mirror Rect
> > object in native for the Rect Java object and off-load all the
> > functionality to native C++ object. When do I create the native Rect
> > object, when do I destroy it?
>
> I'm not sure what this means and why it applies particularly to C++.
> If you can't find your answer in the various references, please
> describe what you're after in more detail.
>
> Some links:
>
>  http://java.sun.com/docs/books/jni/html/jniTOC.html
>  http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html
>  http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=d...
Reply all
Reply to author
Forward
0 new messages