Miha
2011-06-13 10:15:56 UTC
Hi!
I'm developing an application which will use a lot of existing C++
libraries; I am therefore using the NDK. So far I've just written a
simple demo for myself testing the JNI stuff and callbacks. This
mailing list has been very helpful so far, but since I'm new to
android development and haven't touched C/C++ in 10+ years, I still
have lots to (re)learn.
The library I will be porting (and using) uses threads and is
asynchronous, which means I will be getting various events I should be
handling. I know that I must execute UI-modifying stuff on the UI
thread and so far, I've used Activity.runOnUiThread method on the main
activity instance for that purpose.
On the native side, I get a reference to a class in the JNI_OnLoad and
obtain the jmethodID of the callback methods which I invoke from the
native side-of-things. I cache the reference to the main class using
env->NewGlobalRef(clazz).
When I invoke my first method (from java to native), I store the
instance reference as a global variable in native code (again, I use
env->NewGlobalRef(instance)) and I use that global reference in the
thread procedure to invoke methods on it (callback methods).
The thread uses AttachCurrentThread to get a reference to JNIEnv and
then invokes the methods on the java object instance (the cached
reference).
Now, for some questions:
1) I did not find a way to get the instance of the main activity other
than by calling a native method from the activity. Is this OK? Or is
there an API call I'm missing?
2) Since I'm caching the reference to an _instance_ (the MainActivity
actually), I'm wondering if JNI_OnLoad will be called when this
instance is recreated? (my testing shows that if I exit the app and
then reenter it, the references are still valid and native library
still works as expected)
3) I'm creating strings via env->NewStringUTF and passing them on to
Java world. Who will free them? GC?
4) When should (MUST?) I call DeleteGlobalRef? Is there an
JNI_OnUnload equivalent to JNI_OnLoad available?
5) static functions don't work: I'm getting: No implementation found
for native ... error message. I declared the function static and also
the declaration in corresponding Java file. If I remove static,
everything works. The reason I wanted to use static is beacuse of a
JNI tip that "You should declare the methods "static" so the names
don't take up space in the symbol table on the device." I am not
explicitly registering functions though... (maybe autodiscovery
doesn't find static methods?)
6) Given that much of the codebase I'm integrating is in c++, does it
make sense to develop activities in the NDK as well? Perhaps that
would be easier because I wouldn't have to call java methods all the
time to update the UI?
7) Probably a silly question, but I'm having hard time figuring out
the whole C/C++ API that is available (I have yet to setup eclipse for
C++ dev). What are you guys using as a reference to a C/C++ API that
is available on Android? Currently, I'm between grepping the NDK
header files and googling for method signatures... Is there such a
thing as a compelte Bionic reference?
Thanks,
Miha.
I'm developing an application which will use a lot of existing C++
libraries; I am therefore using the NDK. So far I've just written a
simple demo for myself testing the JNI stuff and callbacks. This
mailing list has been very helpful so far, but since I'm new to
android development and haven't touched C/C++ in 10+ years, I still
have lots to (re)learn.
The library I will be porting (and using) uses threads and is
asynchronous, which means I will be getting various events I should be
handling. I know that I must execute UI-modifying stuff on the UI
thread and so far, I've used Activity.runOnUiThread method on the main
activity instance for that purpose.
On the native side, I get a reference to a class in the JNI_OnLoad and
obtain the jmethodID of the callback methods which I invoke from the
native side-of-things. I cache the reference to the main class using
env->NewGlobalRef(clazz).
When I invoke my first method (from java to native), I store the
instance reference as a global variable in native code (again, I use
env->NewGlobalRef(instance)) and I use that global reference in the
thread procedure to invoke methods on it (callback methods).
The thread uses AttachCurrentThread to get a reference to JNIEnv and
then invokes the methods on the java object instance (the cached
reference).
Now, for some questions:
1) I did not find a way to get the instance of the main activity other
than by calling a native method from the activity. Is this OK? Or is
there an API call I'm missing?
2) Since I'm caching the reference to an _instance_ (the MainActivity
actually), I'm wondering if JNI_OnLoad will be called when this
instance is recreated? (my testing shows that if I exit the app and
then reenter it, the references are still valid and native library
still works as expected)
3) I'm creating strings via env->NewStringUTF and passing them on to
Java world. Who will free them? GC?
4) When should (MUST?) I call DeleteGlobalRef? Is there an
JNI_OnUnload equivalent to JNI_OnLoad available?
5) static functions don't work: I'm getting: No implementation found
for native ... error message. I declared the function static and also
the declaration in corresponding Java file. If I remove static,
everything works. The reason I wanted to use static is beacuse of a
JNI tip that "You should declare the methods "static" so the names
don't take up space in the symbol table on the device." I am not
explicitly registering functions though... (maybe autodiscovery
doesn't find static methods?)
6) Given that much of the codebase I'm integrating is in c++, does it
make sense to develop activities in the NDK as well? Perhaps that
would be easier because I wouldn't have to call java methods all the
time to update the UI?
7) Probably a silly question, but I'm having hard time figuring out
the whole C/C++ API that is available (I have yet to setup eclipse for
C++ dev). What are you guys using as a reference to a C/C++ API that
is available on Android? Currently, I'm between grepping the NDK
header files and googling for method signatures... Is there such a
thing as a compelte Bionic reference?
Thanks,
Miha.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-***@googlegroups.com.
To unsubscribe from this group, send email to android-ndk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-***@googlegroups.com.
To unsubscribe from this group, send email to android-ndk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.