Vinod Maurya
Vinod Maurya

Reputation: 4167

Native method not found - JNI Android

I am trying to build a demo app using jni (I am new to it). I have Googled and SO about the problem but nothing helped.

Below is the code I am using:

native.c

#include <jni.h>

JNIEXPORT jint JNICALL com_example_MainActivity_helloint(JNIEnv* env, jobject o)
{
    return (jint) 2;
}

MainActivity.java

public class MainActivity extends Activity {

    static {  
        System.loadLibrary("ndk1");  
    }

    public native int helloint();


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

        helloint();  

    }

}

Android.mk

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

The project builds successfully (including ndk-build) but when I run this, app crashes and the logcat shows:

12-09 03:38:49.630: W/dalvikvm(2077): No implementation found for native Lcom/example/MainActivity;.helloint:()I
12-09 03:38:49.630: D/AndroidRuntime(2077): Shutting down VM
12-09 03:38:49.640: W/dalvikvm(2077): threadid=1: thread exiting with uncaught exception (group=0xb1ad5b90)
12-09 03:38:49.650: E/AndroidRuntime(2077): FATAL EXCEPTION: main
12-09 03:38:49.650: E/AndroidRuntime(2077): Process: com.example, PID: 2077
12-09 03:38:49.650: E/AndroidRuntime(2077): java.lang.UnsatisfiedLinkError: Native method not found: com.example.MainActivity.helloint:()I
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.example.MainActivity.helloint(Native Method)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.example.MainActivity.onCreate(MainActivity.java:22)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.Activity.performCreate(Activity.java:5243)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.os.Looper.loop(Looper.java:137)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at java.lang.reflect.Method.invoke(Method.java:515)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at dalvik.system.NativeStart.main(Native Method)

I am not sure what I am doing wrong. Could anyone please point me in the right direction?

Your help is appreciated !!

Upvotes: 0

Views: 1566

Answers (1)

Vinod Maurya
Vinod Maurya

Reputation: 4167

It seems I missed Java_ in function name.

JNIEXPORT jint JNICALL Java_com_example_MainActivity_helloint(JNIEnv* env, jobject o)
{
    return (jint) 2;
}

I am not sure if this was the real problem, but it did the trick!!

Upvotes: 2

Related Questions