Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK Native method not found error

I am trying to build android application using native code so i want to test if ndk runs successfully.When i try to run my first hello world project log cat says,

01-21 23:30:06.780: E/AndroidRuntime(939): FATAL EXCEPTION: main
01-21 23:30:06.780: E/AndroidRuntime(939): java.lang.UnsatisfiedLinkError: 
Native method not found: com.example.ndktesting.MainActivity.invokeNativeFunction:()Ljava/lang/String;

I checked some stackoverflow answers but could not find my answer.Here is my code for java and c.I am using android ndk r8d version.

//ndktest.c

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

extern "C"{
    JNIEXPORT jstring JNICALL   Java_com_example_ndktesting_ndktest_MainActivity_invokeNativeFunction(JNIEnv* env, jobject  thiz)
};

JNIEXPORT jstring JNICALL   Java_com_example_ndktesting_ndktest_MainActivity_invokeNativeFunction(JNIEnv* env, jobject  thiz){
    return (*env)->NewStringUTF(env, "Hello from native code!");
}

Here is my MainActivity java code:

package com.example.ndktesting;

public class MainActivity extends Activity {    
    //declare the native code function - must match ndktest.c
    private native String invokeNativeFunction();

    public native String  unimplementedinvokeNativeFunction();

    // load the library - name matches jni/Android.mk 
    static {
        System.loadLibrary("ndktest");
    }

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

        // this is where we call the native code
        String hello = invokeNativeFunction();

        new AlertDialog.Builder(this).setMessage(hello).show();
    }
}

Android make file code:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_MODULE    := ndktest
LOCAL_SRC_FILES := ndktest.c

include $(BUILD_SHARED_LIBRARY)
like image 801
Kuru Kshetran Avatar asked Jan 21 '13 18:01

Kuru Kshetran


1 Answers

Your package/class names do not match.

JNIEXPORT jstring JNICALL   Java_com_example_ndktesting_ndktest_MainActivity_invokeNativeFunction(JNIEnv* env, jobject  thiz)

Would be a method in the class

com.example.ndktesting.ndktest.MainActivity

However your actual code

package com.example.ndktesting;

public class MainActivity extends Activity 

causes it to look for

com.example.ndktesting.MainActivity.invokeNativeFunction

without the "ndktest"

Once you make the names match it should either work, or expose the next issue.

like image 50
Chris Stratton Avatar answered Sep 19 '22 08:09

Chris Stratton