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

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)
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 に答える