“UnsatisfiedLinkError:找不到本机方法”但正确命名?

时间:2015-02-19 09:54:35

标签: java android android-ndk java-native-interface native-code

无法找到我的原生方法,我无法解决。

java代码:

package org.cocos2dx.cppemptytest;

public class TestJNI {
    static {
        System.loadLibrary("cpp_empty_test");
    }
    public native String moveto();
}

原生代码:

#include "AppDelegate.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
#include <stdlib.h>

#include "cocos2d.h"

#define  LOG_TAG    "main"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

using namespace cocos2d;

AppDelegate *pAppDelegate;

void cocos_android_app_init (JNIEnv* env, jobject thiz) {
    LOGD("cocos_android_app_init");
    pAppDelegate = new AppDelegate();
}

extern "C" {

    JNIEXPORT jstring JNICALL Java_org_cocos2dx_cppemptytest_TestJNI_moveto
         ( JNIEnv* env, jobject thiz){
        return env->NewStringUTF("default");
    }
}
Android.mk中的

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cpp_empty_test

LOCAL_MODULE_FILENAME := libcpp_empty_test

LOCAL_SRC_FILES := main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp

LOCAL_LDLIBS := -llog

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
                    $(LOCAL_PATH)/../../../../extensions \
                    $(LOCAL_PATH)/../../../.. \
                    $(LOCAL_PATH)/../../../../cocos/editor-support

LOCAL_STATIC_LIBRARIES := cocos2dx_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,.)

输出给出了不满意的链接错误,但我的命名是正确的。是否有其他原因导致此错误发生?

E/AndroidRuntime(30310): java.lang.UnsatisfiedLinkError: 
Native method not found: 
org.cocos2dx.cppemptytest.TestJNI.moveto:()Ljava/lang/String;

1 个答案:

答案 0 :(得分:1)

尝试将 Android.mk 更改为:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := libcpp_empty_test
LOCAL_SRC_FILES := main.cpp
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)