problem while executing please let me know where i am going wrong

176 views
Skip to first unread message

rajiv

unread,
Dec 20, 2010, 9:24:05 AM12/20/10
to android-ndk
hello
i am facing this problem while executing please let me know where i
am going wrong
step 1: Eclipse->new project
step 2:modified java file
package com.androiddeveloper.DefaultNdkverification;

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

public class ndk extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("---DEMO---", "Return value from native lib: " +
getString());
}
public native String getString();
static
{
System.loadLibrary("native_lib_name");
}
}
step3: created jni folder inside that created
ndkverification.c

#include<string.h>
#include<jni.h>

JNIEXPORT jstring JNICALL Java_ndkverification_getString(JNIEnv * env,
jobject obj)
{
return (*env)->NewStringUTF(env, "Complex string calculated in
native code");
}
step4:
inside jni one more file of Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := native_lib_name
LOCAL_SRC_FILES := ndkverification.c

include $(BUILD_SHARED_LIBRARY)

step5:
inside jni one more file of Application.mk
APP_STL := stlport_static


from terminal checked the .c file

XYZ@A1MD04778:~/workspace/DefaultNdkverification/jni$ /home/XYZ/
Desktop/NDK/ndk-build
Compile thumb : native_lib_name <= ndkverification.c
SharedLibrary : libnative_lib_name.so
Install : libnative_lib_name.so => libs/armeabi/
libnative_lib_name.so
XYZ@A1MD04778:~/workspace/DefaultNdkverification/jni$


final if i execute through eclipse but the emulator is displaying
application has stopped unexpectedly

alan

unread,
Dec 20, 2010, 12:12:50 PM12/20/10
to android-ndk
your c function should be called:
Java_com_androiddeveloper_DefaultNdkverification_ndk_getString
if you look in the ddms window you will see more detailed error
messages ("unsatisfied link error" in this case)

Mike Edenfield

unread,
Dec 20, 2010, 5:28:08 PM12/20/10
to andro...@googlegroups.com, rajiv
On 12/20/2010 9:24 AM, rajiv wrote:
> package com.androiddeveloper.DefaultNdkverification;

> public class ndk extends Activity
> {

> public native String getString();
> static
> {
> System.loadLibrary("native_lib_name");
> }
> }

> JNIEXPORT jstring JNICALL Java_ndkverification_getString(JNIEnv * env,
> jobject obj)

You're function name doesn't match what JNI expects. To find the native
implementation of a "native" Java function, JNI has two options:

Implicit Matches: Done by name; the name of your C function has to match
the *full* method signature, which includes the package and class names
exactly as they are in Java. So your function definition would have to be:

---
JNIEXPORT jstring JNICALL
Java_com_androiddeveloper_DefaultNdkverification_ndk_getString(JNIEnv
*env, jobject self)
---

Explicit Matches: Done by using the RegisterNatives method of JNIEnv.
This is a *much better option* for many reasons, including being more
reliable, more efficient, and eliminating the ugly function names. To
do this, you would do something like this:

---
#define JAVA_CLASS "com/androiddeveloper/DefaultNdkverification/ndk";

static jstring JNICALL JNI_getString(JNIEnv *env, jobject self);
static JNINativeMethod methods[] = {
{ "getString", "()Ljava/lang/String;", (void *)&JNI_getString}
}

jint JNI_OnLoad(JavaDM *vm, void *reserved)
{
JNIEnv *env;
if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_4) != JNI_OK)
return -1;

jclass cls = (*env)->FindClass(env, JAVA_CLASS);
(*env)->RegisterNatives(env, cls, methods, sizeof(methods) /
sizeof(methods[0]));

return JNI_VERSION_1_4;
}
---

The second method looks like its more work, but it is ultimately a
better option. And once you have it written once, you can pretty much
copy/paste the JNI_OnLoad implementation into any JNI library you want,
and just change the methods array and JAVA_CLASS to suit.

--Mike

Reply all
Reply to author
Forward
0 new messages