Ok I have been trying and trying to get a simple JNI program running.

This is my source:
Run.java:

package corey.nativetest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Run extends Activity {

    private native int print();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        ((TextView)findViewById(R.id.text)).setText(print());
    }
    static {
        try {
            Log.i("JNI", "Trying to load libRun.so");
            System.loadLibrary("/data/lib/libRun.so");
        }
        catch (UnsatisfiedLinkError ule) {
            Log.e("JNI", "WARNING: Could not load libRun.so");
        }
    }
}

Run.c:
#include <jni.h>
#include <stdio.h>
#include "Run.h"


JNIEXPORT jint JNICALL Java_Run_print(JNIEnv * x, jobject y)
{
    return (jint)42;
}

static JNINativeMethod sMethods[] = {
     /* name, signature, funcPtr */
    {"print", "()I", (void*)Java_Run_print}
};


jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    static const char* const kClassName = "corey/nativetest/Run";
    JNIEnv* env;
    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK)
        return -1;

    /* get class with (*env)->FindClass */
    /* register methods with (*env)->RegisterNatives */

    jclass clazz;

    /* look up the class */
    clazz = (*env)->FindClass(env, kClassName);
    if (clazz == NULL) {
        return -1;
    }

    (*env)->RegisterNatives(env, clazz, sMethods, 1);

    return JNI_VERSION_1_4;
}

int main(int argc, char **argv) {
    return 0;
}

and my .h is really just the "javah -jni" prebuilt one.

I compiled it using the instructions here:
http://davanum.wordpress.com/2007/12/09/android-invoke-jni-based-methods-bridging-cc-and-java/
http://honeypod.blogspot.com/2007/12/shared-library-hello-world-for-android.html

adb push libRun.so /data/lib/


Nothing works, I get

I/jdwp    (  942): received file descriptor 20 from ADB
I/dalvikvm(  942): Debugger thread not active, ignoring DDM send
(t=0x54455354 l
=8)
W/SurfaceFlinger(  582): executeScheduledBroadcasts() skipped, contention on
the
 client. We'll try again later...
D/dalvikvm(  553): GC freed 227 objects / 8600 bytes in 224ms
D/dalvikvm(  553): GC freed 7 objects / 272 bytes in 163ms
D/dalvikvm(  553): GC freed 2 objects / 56 bytes in 130ms
I/JNI     (  942): Trying to load libRun.so
E/JNI     (  942): WARNING: Could not load libRun.so
D/dalvikvm(  942): +++ not scanning '/system/lib/libwebcore.so' for 'print'
(wrong CL)
D/dalvikvm(  942): +++ not scanning '/system/lib/libmedia_jni.so' for
'print' (wrong CL)
W/dalvikvm(  942): No implementation found for native
Lcorey/nativetest/Run;.print ()I
D/AndroidRuntime(  942): Shutting down VM
W/dalvikvm(  942): threadid=3: thread exiting with uncaught exception
(group=0x4000fe70)
E/AndroidRuntime(  942): Uncaught handler: thread main exiting due to
uncaught exception
E/AndroidRuntime(  942): java.lang.UnsatisfiedLinkError: print
E/AndroidRuntime(  942):        at corey.nativetest.Run.print(Native Method)
E/AndroidRuntime(  942):        at corey.nativetest.Run.onStart(Run.java:23)
E/AndroidRuntime(  942):        at
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1205)
E/AndroidRuntime(  942):        at
android.app.Activity.performStart(Activity.java:3490)
E/AndroidRuntime(  942):        at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
E/AndroidRuntime(  942):        at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
E/AndroidRuntime(  942):        at
android.app.ActivityThread.access$1800(ActivityThread.java:112)
E/AndroidRuntime(  942):        at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
E/AndroidRuntime(  942):        at
android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  942):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  942):        at
android.app.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime(  942):        at
java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  942):        at
java.lang.reflect.Method.invoke(Method.java:521)


Obviously my lib isn't loading, but I don't know why. Any ideas?

Thanks
-Corey Ling

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: http://groups.google.com/group/android-porting
-~----------~----~----~----~------~----~------~--~---

Reply via email to