1

I'm starting to learn NDK. I've downloaded the recent NDK tools and eclipse plugin. I've followed a basic tutorial and tried to run. I'm getting it work without the eclipse plugin(Calling manually to ndk-build) but with the plugin I get this excepsion:

02-17 17:49:01.477: E/AndroidRuntime(9746): java.lang.UnsatisfiedLinkError: performOperation

Here is my code:

package com.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class HelloWorld extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hello_world);

    NativeLibrary nativeobject = new NativeLibrary();
    nativeobject.result(this);

    }

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

}

and:

package com.helloworld;


import android.content.Context;
import android.widget.Toast;

public class NativeLibrary {


    /* *
     * performOperation is defined in native library
     */
    public native String performOperation();


    /* *
     * loads the library shared object
     */
    static {
        System.loadLibrary("NativeLibrary");
    }

    /* *
     * Computes the result
     */
    public void result(Context ctx) {
        Toast.makeText(ctx, performOperation(), Toast.LENGTH_SHORT).show();
    }
}

cpp:

#include <jni.h>

JNIEXPORT jstring JNICALL Java_com_helloworld_NativeLibrary_performOperation(
        JNIEnv* env, jobject o) {
    return env -> NewStringUTF("this is NativeLibrary");
}

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := NativeLibrary
LOCAL_SRC_FILES := NativeLibrary.cpp

include $(BUILD_SHARED_LIBRARY)
Ari M
  • 1,386
  • 3
  • 18
  • 33

1 Answers1

0

In the CPP file, add extern "C"{} around the function:

extern "C" {
JNIEXPORT jstring JNICALL Java_com_helloworld_NativeLibrary_performOperation(
        JNIEnv* env, jobject o) {
    return env -> NewStringUTF("this is NativeLibrary");
}

}
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281