Hi,all:
    I think JNI is supported in 0.9 Beta. Because I wrote a
helloworld's jni application in 0.9 beta,and it seem running
successfully.
    but i encounter a trouble, I can not do any writing operation,
like remove/rename file.
Next is JNI C code:
JNIEXPORT void JNICALL Java_jprint_print(JNIEnv *env, jobject obj)
{
        remove("/data/a.log");
        rename("/data/b.log", "/data/c.log");
        printf("================hello jni===================\r\n");
}

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
        JNIEnv *env;
        JNINativeMethod meth;
        jclass k;
        jint r;

        r = (*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4);
        k = (*env)->FindClass (env, "com.android.helloactivity.jprint");

        meth.name = "print";
        meth.signature = "()V";
        meth.fnPtr = Java_jprint_print;
        r = (*env)->RegisterNatives (env, k, &meth, 1);
        return JNI_VERSION_1_4;
}

JNIEXPORT void JNI_OnUnload(JavaVM *vm, void *reserved)
{
        JNIEnv *env;
        jclass k;
        jint r;
        r = (*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_2);
        k = (*env)->FindClass (env, "com.android.helloactivity.jprint");
        (*env)->UnregisterNatives(env, k);
}

It is very important to call RegisterNatives() to register native
interface. It will show error information "Not Implemention"  if you
do not register it.

Next is Jave Code:
public class jprint
{
public native void print();

static
    {
        try {
            Log.i("JNI", "Trying to load jni .so");
 
System.out.println(System.getProperty("java.library.path"));
            System.load("/data/libjnilibs.so");
        }
        catch (UnsatisfiedLinkError ule) {
            Log.e("JNI", ule.getMessage());

        }
    }
}

Add code to Android OnCreate():
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        jprint p = new jprint();
        p.print();
...
}
It is also very important to call System.load("/data/libjnilibs.so")
in java instead of System.loadLibrary(), because System.loadLibrary()
always load library from /system/lib directory.

because /system/bin is readonly file system. so you can push .so file
to /data.
1.build out .so file using arm-eabi compiler
2.push .so to /data directoy: adb push d:/libjnilibs.so /data.(I use
windows's emulator and tool)
3.run java application, you will see(via adb logcat):

I/JNI     (  524): Trying to load jni .so
I/System.out(  524): /system/lib
D/dalvikvm(  524): Trying to load lib /data/libjnilibs.so 0x433f22d0
D/dalvikvm(  524): Added shared lib /data/libjnilibs.so 0x433f22d0
I/ActivityManager(   50): Displayed activity com.android.helloactivity


No, the native interface print() will be called from Jave application.
But It can not  remove file in JNI. I think it is limited by java's
security management.
JNIEXPORT void JNICALL Java_jprint_print(JNIEnv *env, jobject obj)
{
        remove("/data/a.log");
        rename("/data/b.log", "/data/c.log");
        printf("================hello jni===================\r\n");
}
I do not know how to solve this problem. who can tell me and
suggestion?

On Aug 26, 5:01 am, hackbod <[EMAIL PROTECTED]> wrote:
> Native code is not supported in 1.0.
>
> On Aug 25, 11:15 am, Tyler Ernst <[EMAIL PROTECTED]> wrote:
>
>
>
> > Thanks, that was exactly what we needed.  We realize JNI is not
> > supported, but Java code is just too slow to do the things we need...
>
> > On Aug 25, 2:23 am, Volker Gropp <[EMAIL PROTECTED]> wrote:
>
> > > On Aug 22, 9:30 pm, Tyler Ernst <[EMAIL PROTECTED]> wrote:
>
> > > > Also since the 0.9 beta has been released I have been unable to copy
> > > > the .so to the /system/lib directory of the emulator.  If anyone has
> > > > found a way to do this that would be greatly appreciated.
>
> > > Hi,
>
> > > you have to mount the system rw. To do so run `adb remount` before you
> > > push your .so. And keep in mindJNIwill not be supported, or did you
> > > find a way to deploy your native lib with a .apk?
>
> > > Volker Gropp- Hide quoted text -
>
> - Show quoted text -

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new Android 0.9 SDK beta!
http://android-developers.blogspot.com/2008/08/announcing-beta-release-of-android-sdk.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to