0

I try run an Android application using ndk, i can build the library, but when i run give a exception.

follow the codes and error for help the solution

i use c code, instead cpp

primitive.c

#include <jni.h>
#include <android/log.h>

JNIEXPORT jboolean JNICALL Java_cookbook_chapter2_PassingPrimitiveActivity_passBooleanReturnBoolean(JNIEnv *pEnv, jobject pObj, jboolean pBooleanP){
__android_log_print(ANDROID_LOG_INFO, "native", "%d in %d bytes", pBooleanP, sizeof(jboolean));
return (!pBooleanP);}

Acitivity class: PassingPrimitiveActivity.java

public class PassingPrimitiveActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_passing_primitive);

    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append("boolean: ").append(passBooleanReturnBoolean(false))");;
    TextView tv = (TextView) findViewById(R.id.display_res);
    tv.setText(strBuilder.toString());

}

private native boolean passBooleanReturnBoolean(boolean p);
static {
    System.loadLibrary("PassingPrimitive");
} }

logcat file, with error. Show that don't have implementation, but in c code, have it

06-11 17:08:30.652 29603 29603 E AndroidRuntime: FATAL EXCEPTION: main

06-11 17:08:30.652 29603 29603 E AndroidRuntime: java.lang.UnsatisfiedLinkError: Native method not found: cookbook.chapter2.passingprimitive.PassingPrimitiveActivity.passBooleanReturnBoolean:(Z)Z

06-11 17:08:30.652 29603 29603 E AndroidRuntime:    at cookbook.chapter2.passingprimitive.PassingPrimitiveActivity.passBooleanReturnBoolean(Native Method)

06-11 17:08:30.652 29603 29603 E AndroidRuntime:    at cookbook.chapter2.passingprimitive.PassingPrimitiveActivity.onCreate(PassingPrimitiveActivity.java:15)

06-11 17:08:30.652 29603 29603 E AndroidRuntime:    at android.app.Activity.performCreate(Activity.java:5184)

my Android.mk seens like it:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := PassingPrimitive
LOCAL_SRC_FILES := primitive.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

Everything compiles fine, creates a .so file and copies to the libs folder under my project, but when I run, logcat show UnsatisfiedLinkError and the app crash

What i can do for solve it?

gmendes
  • 117
  • 1
  • 1
  • 17
  • Do you see your native library being loaded in logcat? Does your application have a libPassingPrimitive.so under libs/armeabi-v7a? It might be helpful if you post your Android.mk and Application.mk also. – JonnyBoy Jun 11 '14 at 23:31

2 Answers2

0

Try generating a proper C header file from your compiled java code, like this:

javah -jni -classpath bin/classes com.whatever.PassingPrimitiveActivity

then include that header file in your C code. The function you define in C should match the one in the header file. The mapping is not always what you assume.

chrisdowney
  • 1,528
  • 1
  • 11
  • 21
  • when i did it, show `Error: cannot access android.app.Activity class file for android.app.Activity not found` – gmendes Jun 12 '14 at 13:31
  • 1
    You need to also provide the android-X.jar in the classpath. See http://stackoverflow.com/a/10577513/892714 – JonnyBoy Jun 12 '14 at 17:29
  • Yes; you need to include the android jar file in your classpath, if your java code uses any android classes, otherwise the javah tool won't know where to find the android class definitions. – chrisdowney Jun 12 '14 at 23:52
0

Your logcat suggests that the native function name should be

Java_cookbook_chapter2_passingprimitive_PassingPrimitiveActivity_passBooleanReturnBoolean
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307