Hi, While using the NDK, I came across a very peculiar problem...
Let's take case of the simple "hello-jni" sample. It has one method called stringFromJNI which works wonderfully. I add another method called stringFromMyJNI and it doesn't work. I then did some research and found something very peculiar... If I have more than one method, only first method is actually exported in the shared-object. So, I thought about naming it to something like "registerNatives" and ensure that it is called. And it does get called. But still unable to invoke any method. The code is given below... Thanks in advance! -- Happy Hacking, Gaurav Vaish www.mastergaurav.com ----------------------------------------------------- JNI Code ----------------------------------------------------- jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ); jstring Java_com_example_hellojni_HelloJni_stringFromJNIx( JNIEnv* env, jobject thiz ); //* static JNINativeMethod methods[] = { { "stringFromJNI", "()Ljava/lang/String;", Java_com_example_hellojni_HelloJni_stringFromJNI}, { "stringFromJNIx", "()Ljava/lang/String;", Java_com_example_hellojni_HelloJni_stringFromJNIx}, }; //*/ JNIEXPORT void JNICALL Java_com_example_hellojni_HelloJni_registerNatives (JNIEnv *env, jclass clazz) { (*env)->RegisterNatives(env, clazz, methods, 2); } JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "iHello!"); } JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNIx( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "iHello X!"); } ----------------------------------------------------- Java Code ----------------------------------------------------- public class HelloJni extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText(stringFromJNI() + "\n" + stringFromJNIx()); setContentView(tv); } public static native void registerNatives(); public native String stringFromJNI(); public native String stringFromJNIx(); public native String unimplementedStringFromJNI(); static { extractAndLoadLibrary("hello-jni"); } private static void extractAndLoadLibrary(String basename) { String filename = "lib" + basename + ".so"; String pkg = "com.example.hellojni"; String sourceFile = "/data/app/" + pkg + ".apk"; String targetFolder = "/data/data/" + pkg + "/"; String libraryFile = targetFolder + filename; File f = new File(libraryFile); if(f.exists() && f.isFile()) { // System.out.println("File found! All IZ well"); System.out.println("File " + libraryFile + " deleted? " + f.delete()); return; } try { ZipFile file = new ZipFile(sourceFile); ZipEntry entry = file.getEntry("assets/" + filename); InputStream input = file.getInputStream(entry); System.out.println("Got the input stream..."); OutputStream output = new FileOutputStream(libraryFile); System.out.println("Got the OUTput stream..."); byte[] buf = new byte[1024]; int read; read = input.read(buf, 0, 1024); while(read > 0) { output.write(buf, 0, read); read = input.read(buf, 0, 1024); } output.close(); input.close(); System.load(libraryFile); registerNatives(); } catch(IOException e) { e.printStackTrace(); } } } -- 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 android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en