user3136235
user3136235

Reputation: 103

AndroidRuntime: java.lang.UnsatisfiedLinkError: Native method not found: com.example.compute.UseNDK.AddNumbers:(II)I

I have a small JNI file with a native function that add the two variable and print the result.But when i run this program the main activity is run and take input from the user but when i press the add button it will give this error AndroidRuntime: java.lang.UnsatisfiedLinkError: Native method not found: com.example.compute.UseNDK.AddNumbers:(II)I plz help me to solve this probblem im new in this field..

The MainActivity.java

package com.example.compute;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import android.util.Log; /*import log class*/


public class MainActivity extends Activity implements OnClickListener
{

//   static 
//    {
//      System.loadLibrary("add");
//    }
//   
//   private native int AddNumbers(int value1,int value2);
//@Override


    private static final String TAG = MainActivity.class.getName();

    private Button btnCalculate;
    private EditText editResult;
    private EditText editV1;
    private EditText editV2;
    private TextView result;    

    UseNDK ntv = new UseNDK();

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

            btnCalculate = (Button)findViewById(R.id.btnAdd);
            // editResult = (EditText)findViewById(R.id.textResult);

            editV1 = (EditText)findViewById(R.id.editV1);
            editV2 = (EditText)findViewById(R.id.editV2);
            result = (TextView)findViewById(R.id.textResult);

            btnCalculate.setOnClickListener(this);

        }

            public void onClick(View v)
            {

                // TODO Auto-generated method stub
                if(v.getId() == R.id.btnAdd)
                {

                    int v1 , v2 , res = 0;

                    v1 = Integer.parseInt(editV1.getText().toString());
                    v2 = Integer.parseInt(editV2.getText().toString());

                    res = ntv.AddNumbers(v1, v2);

                    result.setText(new Integer(res).toString());
                }

            }
    @Override

        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

}

The UseNDK.java

package com.example.compute;

import android.util.Log;

public class UseNDK 
{
    static 
    {
        try
        {
            System.loadLibrary("add");
        }

        catch (UnsatisfiedLinkError e) 
        {
            System.err.println("public native code library failed to load.\n" + e);
            Log.e("Tag",e.getMessage());

        }

    }

    public native int AddNumbers(int value1 , int value2);

}

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)

LOCAL_MODULE    := add
LOCAL_LDLIBS := -llog 
LOCAL_SRC_FILES := add.cpp\
                   add.h\

include $(BUILD_SHARED_LIBRARY)

add.cpp

#include <string.h>
#include <jni.h>
#include <stdlib.h>
#include <stdio.h>
#include <add.h>
#include <android/log.h>

JNIEXPORT jint JNICALL Java_com_example_compute_MainActivity_AddNumbers(JNIEnv* env, jobject obj, jint v1, jint v2)
{

     __android_log_print(ANDROID_LOG_ERROR, "MyTag", "The value is %d", 1+1);

     //return -1;
     return (V1 + V2);
}

add.h

JNIEXPORT jint Java_com_example_compute_MainActivity_AddNumbers(JNIEnv *env, jobject obj, jint , jint );

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/editV2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_marginTop="24dp"
        android:ems="10" />



    <EditText
        android:id="@+id/editV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editV2"
        android:layout_below="@+id/editV2"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editV1"
        android:layout_marginTop="32dp"
        android:text="" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="63dp"
        android:text="Add" />

</RelativeLayout>

LogCat Errors

 D/dalvikvm(1237): Added shared lib /data/app-lib/com.example.compute-1/libadd.so 0xb3d45580
 D/dalvikvm(1237): No JNI_OnLoad found in /data/app-lib/com.example.compute-1/libadd.so 0xb3d45580, skipping init
D/gralloc_goldfish(1237): Emulator without GPU emulation detected.
D/dalvikvm(1282): Trying to load lib /data/app-lib/com.example.compute-2/libadd.so 0xb3d46408
Added shared lib /data/app-lib/com.example.compute-2/libadd.so 0xb3d46408
D/dalvikvm(1282): No JNI_OnLoad found in /data/app-lib/com.example.compute-2/libadd.so 0xb3d46408, skipping init
D/gralloc_goldfish(1282): Emulator without GPU emulation detected.
W/dalvikvm(1282): No implementation found for native Lcom/example/compute/UseNDK;.AddNumbers:(II)I
 D/AndroidRuntime(1282): Shutting down VM
W/dalvikvm(1282): threadid=1: thread exiting with uncaught exception (group=0xb3a94ba8)
E/AndroidRuntime(1282): FATAL EXCEPTION: main
E/AndroidRuntime(1282): Process: com.example.compute, PID: 1282
E/AndroidRuntime(1282): java.lang.UnsatisfiedLinkError: Native method not found: com.example.compute.UseNDK.AddNumbers:(II)I
E/AndroidRuntime(1282): at com.example.compute.UseNDK.AddNumbers(Native Method)
E/AndroidRuntime(1282): at com.example.compute.MainActivity.onClick(MainActivity.java:63)
 E/AndroidRuntime(1282): at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(1282): at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(1282): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(1282): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(1282): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1282): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1282): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1282): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1282): at om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1282): at dalvik.system.NativeStart.main(Native Method)
I/Process(1282): Sending signal. PID: 1282 SIG: 9

Upvotes: 2

Views: 5872

Answers (2)

DSS
DSS

Reputation: 7259

May be this is a late answer, but you could wrap the cpp method inside the extern "c" block like this:

extern "c" {
JNIEXPORT jint JNICALL Java_com_example_compute_MainActivity_AddNumbers(JNIEnv* env, 

jobject obj, jint v1, jint v2)
{
 __android_log_print(ANDROID_LOG_ERROR, "MyTag", "The value is %d", 1+1);
 return (V1 + V2);
}
}

Together with the response by @John Visosky

And then build the application.

Reference: 1

Upvotes: 2

John Visosky
John Visosky

Reputation: 235

In add.cpp, you declare:

Java_com_example_compute_MainActivity_AddNumbers

but the method is actually in UseNDK, so it should be:

Java_com_example_compute_UseNDK_AddNumbers

Upvotes: 0

Related Questions