Hello all,
I have been struggling with this problem for several days now, and I
was hoping maybe somebody here has had a similar experience and could
shed some light on what I'm doing wrong. First, some background: I'm
running the OMAP ldp1-configured version of kernel 2.6.27, on the OMAP
Zoom 3430 development kit, with a recent Android build. I have a
piece of custom hardware attached to the i2c bus, but the only way I
can control it is through native code that can communicate with the
kernel and facilitate bus-level transactions. I'm pretty sure this is
the right group to post in, but if not please let me know.
Generally, my method of implementation is as follows: 1) generate JNI
source and header files, and compile into a shared library 2) create
an Android application that loads said library and executes one or
more of it's functions 3) put the library into /system/lib/ and the
application file into /data/app/ in the Android filesystem (using SD-
card mount), fire up the device and run the application.
Unfortunately, every time I do this the application force-closes
almost immediately, and logcat shows the following:
D/dalvikvm( 900): Trying to load lib /system/lib/libJNIHelloWorld.so
0x436b6a00
D/dalvikvm( 900): Added shared lib /system/lib/libJNIHelloWorld.so
0x436b6a00
D/dalvikvm( 900): No JNI_OnLoad found in /system/lib/
libJNIHelloWorld.so 0x436b6a00
D/dalvikvm( 900): +++ not scanning '/system/lib/libwebcore.so' for
'print' (wrong CL)
D/dalvikvm( 900): +++ not scanning '/system/lib/libmedia_jni.so' for
'print' (wrong CL)
W/dalvikvm( 900): No implementation found for native Lcom/Hello_JNI/
android/HelloWorld;.print ()I
D/AndroidRuntime( 900): Shutting down VM
W/dalvikvm( 900): threadid=3: thread exiting with uncaught exception
(group=0x4000fe68)
E/AndroidRuntime( 900): Uncaught handler: thread main exiting due to
uncaught exception
E/AndroidRuntime( 900): java.lang.UnsatisfiedLinkError: print
E/AndroidRuntime( 900): at com.Hello_JNI.android.HelloWorld.print
(Native Method)
E/AndroidRuntime( 900): at com.Hello_JNI.android.HelloWorld.main
(HelloWorld.java:8)
E/AndroidRuntime( 900): at
com.Hello_JNI.android.Hello_JNI.onCreate(Hello_JNI.java:18)
E/AndroidRuntime( 900): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1123)
E/AndroidRuntime( 900): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2119)
E/AndroidRuntime( 900): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2172)
E/AndroidRuntime( 900): at android.app.ActivityThread.access$1800
(ActivityThread.java:112)
E/AndroidRuntime( 900): at android.app.ActivityThread
$H.handleMessage(ActivityThread.java:1586)
E/AndroidRuntime( 900): at android.os.Handler.dispatchMessage
(Handler.java:99)
E/AndroidRuntime( 900): at android.os.Looper.loop(Looper.java:
123)
E/AndroidRuntime( 900): at android.app.ActivityThread.main
(ActivityThread.java:3790)
E/AndroidRuntime( 900): at java.lang.reflect.Method.invokeNative
(Native Method)
E/AndroidRuntime( 900): at java.lang.reflect.Method.invoke
(Method.java:521)
E/AndroidRuntime( 900): at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:745)
E/AndroidRuntime( 900): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:503)
E/AndroidRuntime( 900): at dalvik.system.NativeStart.main(Native
Method)
I/Process ( 754): Sending signal. PID: 900 SIG: 3
I/dalvikvm( 900): threadid=7: reacting to signal 3
I/dalvikvm( 900): Wrote stack trace to '/data/anr/traces.txt'
>From this, it looks like the library is recognized and loaded
successfully. JNI_OnLoad and wrong CL messages don't seem to do
anything (writing a JNI_OnLoad function into the library gets rid of
the message, but doesn't fix anything otherwise; wrong CL messages
will always be there if I'm not mistaken, as they merely indicate that
certain libraries will not be searched for the function in question).
The first real warning is: No implementation found for native Lcom/
Hello_JNI/android/HelloWorld;.print ()I - and this is where the
process apparently breaks down. I have been searching for answers ad
nauseum, but there must be something I'm missing here. Here is the
code I am using (simple Hello World implementation that really just
returns an integer from the native function to the application and
prints it using the Java interface).
<HelloJNI.java - main Android routine>
package com.Hello_JNI.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.Hello_JNI.android.HelloWorld;
public class Hello_JNI extends Activity {
private TextView xLabel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
int ret = 0;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
xLabel = (TextView) findViewById(R.id.x_label);
ret = HelloWorld.main();
xLabel.setText(String.format("Return: %d", ret));
}
}
<HelloWorld.java - helper class that declares native function(s) and
loads native library>
package com.Hello_JNI.android;
public class HelloWorld {
private native int print();
public static int main(){
int ret;
HelloWorld hello = new HelloWorld();
ret = hello.print();
return ret;
}
static{
System.loadLibrary("JNIHelloWorld");
}
}
<HelloWorld.java - not included in Android project - this is the class
used to generate the JNI header>
class HelloWorld {
private native int print();
public static void main(){
int ret = 0;
HelloWorld hello = new HelloWorld();
ret = hello.print();
}
static{
System.loadLibrary("JNIHelloWorld");
}
}
<HelloWorld.h - generated JNI header>
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_HelloWorld_print
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
<HelloWorld.c - JNI source file>
#include "jni.h"
#include "HelloWorld.h"
JNIEXPORT jint JNICALL Java_HelloWorld_print (JNIEnv * env, jobject
obj)
{
int i, j;
i = 2;
j = 3;
return (i + j);
}
The JNI shared library is compiled using the instructions here:
http://android-dls.com/wiki/index.php?title=Compiling_for_Android
and the following command:
agcc -shared HelloWorld.c -o libJNIHelloWorld.so
Note that in order to get this to compile I had to put jni.h and
jni_md.h into the directory where I was compiling the library.
I have come across many instances of people with similar problems, but
so far the solution has eluded me. I would greatly appreciate any
information or direction here; I'm thinking there may be a problem
with that linker script I'm using or maybe an issue with my Java
installation or $PATH variables (causing me to have to move those jni
headers into the compile directory), but there are so many potential
obstacles with this project that I could have missed something
simple. Please let me know if you can help.
Thank you,
Chris H.
--~--~---------~--~----~------------~-------~--~----~
unsubscribe: [email protected]
website: http://groups.google.com/group/android-porting
-~----------~----~----~----~------~----~------~--~---