harshalizee
harshalizee

Reputation: 129

Using jni/ndk in android: UnsatisfiedLinking Error and Force closing Application

I was trying out the Hello World programme on ndk/jni android.

This is my java Code:

package com.example.myndkapp;

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

public class MainActivity extends Activity {
    // load the library - name matches jni/Android.mk
    static {
        Log.d("TAG1","Before Load");
        System.loadLibrary("ndkfoo");
        Log.d("TAG2","After Load");
    }

    // declare the native code function - must match ndkfoo.c
    private native String invokeNativeFunction();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("TAG3","Set Content View Called");
        // this is where we call the native code
        try{
            String hello = invokeNativeFunction();
            Log.d("TAG4","InvokeFunc Called..");
            new AlertDialog.Builder(this).setMessage(hello).show();
        }catch(Exception e){
            e.printStackTrace();
        }


    }
}

The ndkfoo.c Code:

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

jstring Java_com_example_ndkfoo_MAinActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
  return (*env)->NewStringUTF(env, "Hello from native code!");
}

My Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

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

include $(BUILD_SHARED_LIBRARY)

My AndroidMainfest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myndkapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I get the error UnsatisfiedLinking Error and application force closes. Also, I get the no implementation found for native function.

Upvotes: 2

Views: 525

Answers (1)

Jackson Chengalai
Jackson Chengalai

Reputation: 3937

Your package name is not exactly same.change that in ndkfoo.c to

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

  jstring Java_com_example_myndkapp_MainActivity_invokeNativeFunction(JNIEnv* env, jobject      javaThis) {
  return (*env)->NewStringUTF(env, "Hello from native code!");
 }

Upvotes: 1

Related Questions