Is JNI_OnLoad() implementation mandatory in Android JNI

2,172 views
Skip to first unread message

Ranga

unread,
Sep 10, 2009, 4:18:21 AM9/10/09
to android-ndk
Hai,

I have a Android JNI application in that i called a native
function.The file which has native function ,has only one
function.When i execute this Android application i get error messages
in logcat as No JNI_OnLoad implementation is found.But after adding
the implementation of JNI_OnLoad() i donot get the error.


1. So my question is ,Is it mandatory to have JNI_OnLoad and
JNI_OnUnLoad() funation in any native application?

2. While printing some message in native code ,the message is not
being displayed.Why it is not being displayed?


Thanks,
-Siva.

fadden

unread,
Sep 10, 2009, 1:59:51 PM9/10/09
to android-ndk
On Sep 10, 1:18 am, Ranga <pnk...@gmail.com> wrote:
>         I have a Android JNI application in that i called a native
> function.The file which has native function ,has only one
> function.When i execute this Android application i get error messages
> in logcat as No JNI_OnLoad implementation is found.But after adding
> the implementation of JNI_OnLoad() i donot get the error.

Messages labeled "D" or "DEBUG" are for debugging. "I" is INFO, W is
WARNING, E is ERROR.

JNI_OnLoad is not required.

> 2. While printing some message in native code ,the message is not
> being displayed.Why it is not being displayed?

stdin, stdout, and stderr are redirected to /dev/null during Android
framework startup.

Ranga

unread,
Sep 16, 2009, 12:00:50 AM9/16/09
to android-ndk
Thanks for ur replay.

Without having JNI_OnLoad() Android giving UnsatisfiedLinkError
exception.Why is it behaving like that.Am i missing any thing
regararding settings or any thing.

But if i use JNI_OnLoad() and registering native functions in that
one.I am not getting that exception.

even though i run sample NDK projects same result.


Thanks,
-Siva.

Shreesh Ayachit

unread,
Sep 16, 2009, 7:26:54 AM9/16/09
to android-ndk

> Without having JNI_OnLoad() Android giving UnsatisfiedLinkError
> exception.Why is it behaving like that.Am i missing any thing
> regararding settings or any thing.
>
> But if i use JNI_OnLoad() and registering native functions in that
> one.I am not getting that exception.

You dont need JNI_OnLoad() if you are generating the JNI function
declarations using javah.
But if you are using .. say SWIG to generate them or writing by hand
and the function declarations don't follow the following syntax:
Java_<fully qualified class name>_<function name>()

then you need to define a mapping between the Java Signatures and the
function declarations in JNI code through JNI_OnLoad()

And AFAIK, sample NDK projects do run without JNI_OnLoad()


Shreesh


Ranga

unread,
Sep 16, 2009, 8:36:17 AM9/16/09
to android-ndk

Thank you very much.Now it is working perfectly.

Lewis Z.

unread,
Sep 25, 2009, 6:13:31 PM9/25/09
to android-ndk
On Sep 10, 12:59 pm, fadden <fad...@android.com> wrote:
> stdin, stdout, and stderr are redirected to /dev/null during Android
> framework startup.
Can we override this behavior? If so, how?

Tia.

fadden

unread,
Sep 27, 2009, 8:46:33 PM9/27/09
to android-ndk
Where would you like the output to go?

There's a semi-sleazy hack in the VM that you can enable with "setprop
log.redirect-stdio true", followed by stop/start to pick up the
change. This passes an option into the VM that causes it to start a
thread dedicated to copying data from stdout/stderr to the Android log
file. See dalvik/vm/StdioConverter.c in the source tree.

(Egad, it's actually in the documentation at
http://developer.android.com/guide/developing/tools/adb.html .)

Lewis Z.

unread,
Sep 28, 2009, 3:04:45 PM9/28/09
to android-ndk
fadden, thanks a lot. With the setting, now I can see logs from my
device drive.
Reply all
Reply to author
Forward
0 new messages