1

我开始学习NDK。我已经下载了最近的 NDK 工具和 eclipse 插件。我遵循了基本教程并尝试运行。我在没有 eclipse 插件(手动调用ndk-build)的情况下让它工作,但使用插件我得到了这个异常:

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

这是我的代码:

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;
    }

}

和:

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();
    }
}

cp:

#include <jni.h>

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

安卓.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := NativeLibrary
LOCAL_SRC_FILES := NativeLibrary.cpp

include $(BUILD_SHARED_LIBRARY)
4

1 回答 1

0

在 CPP 文件中,extern "C"{}在函数周围添加:

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

}
于 2013-02-18T21:03:27.527 回答