srijeda, 16.01.2008.

c

c


(see also: aapl, c, )


Heres a screen shot

1

Step By Step Instructions below:


Step #1: Compile the project using ant. Some snippets are shown below.

NativeAdd.java class (with Native method)

package c android.util.Log;public class  {    static {        try {            Log.i("JNI", "Trying to load libNativeAdd.so");            System.loadLibrary("NativeAdd");        }        catch (UnsatisfiedLinkError ule) {            Log.e("JNI", "WARNING: Could not load libNativeAdd.so");        }    }    public static native long add(long a, long b);}

Snippet that shows how this class/method is invoked from the main activity

    public void onClick(View view) {        Log.i(LOG_TAG, "onClick");        EditText a = (EditText) findViewById(R.id.a);        EditText b = (EditText) findViewById(R.id.b);        EditText c = (EditText) findViewById(R.id.c);        Log.i(LOG_TAG, "calling native method");        long sum = NativeAdd.add(Long.parseLong(a.getText().toString()),                Long.parseLong(b.getText().toString()));        Log.i(LOG_TAG, "back from native method");        String text = Long.toString(sum);        c.setText("Native add returns = " + text.subSequence(0, c    }

Step #2: Generate header files using java by running the following command.

javah -classpath ../../android.jar;../bin/classes; org.apache.NativeAdd

Heres how the header file (org_apache_CallNative.h) looks like

/* DO NOT EDIT THIS FILE - it is machine generated */#include/* Header for class org_apache_NativeAdd */#ifndef _Included_org_apache_NativeAdd#define _Included_org_apache_NativeAdd#ifdef __cplusplusextern "C" {#endif/* * Class:     org_apache_NativeAdd * Method:    add * Signature: (JJ)J */JNIEXPORT jlong JNICALL Java_org_apache_NativeAdd_add  (JNIEnv *, jclass, jlong, jlong);#ifdef __cplusplus}#endif#endif

Step #3: Code a tiny C file (org_apache_NativeAdd.c) as shown below:

#include "org_apache_NativeAdd.h"JNIEXPORT jlong JNICALL Java_org_apache_NativeAdd_add  (JNIEnv *env, jclass c, jlong a, jlong b){    return a + b;}

Step #4: Please read the following article before you try the next step.
Shared library Hello World! for Android

Step #5: Compile and link the into a shared library.

arm-none-linux-gnueabi-gcc  -I/usr/lib/jvm/java-1.5.0-sun/include -I/usr/lib/jvm/java-1.5.0-sun/include/linux  -fpic -c org_apache_NativeAdd.carm-none-linux-gnueabi-ld -T armelf_linux_eabi.xsc -shared -o libNativeAdd.so org_apache_NativeAdd.o
NOTE: details on armelf_linux_eabi.xsc are in the Step #4.Step #6: copy the library to the emulator. Copy the Application as well.
adb push native/libNativeAdd.so /system/libadb install bin/CallNative.apk

Step #7: Run the app to try some values. Thats it!

Download the sources and Application - CallNative.zip



Popular topics today: macbook air, michigan primary results, pacman jones, norm chow, kylie wyote, michigan exit polls, iphone update, summer phoenix, morrisville nc, randy newman, buynowbe

- 00:46 - Komentari (0) - Isprintaj - #

<< Arhiva >>

Creative Commons License
Ovaj blog je ustupljen pod Creative Commons licencom Imenovanje-Dijeli pod istim uvjetima.