标签:cocos2dx jni
<span style="font-family: Arial, Helvetica, sans-serif;">步骤一:导入JniHelper.h头文件。</span>
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "JniHelper.h" #endif;此时会遇到JniHelper.h头文件无法找到的问题,需要添加附加包含目录即可解决问题。具体操作步骤如下:右击项目---->c/c++---->常规---->附加包含目录---->添加JniHelper.h所在的路径即可,eg:E:\安卓开发工作空间\cocos2d-x-2.2\cocos2dx\platform\android\jni。
<span style="font-size:18px;">void HelloWorld::fun() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo methodInfo; bool isHave = JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/hellocpp/HelloCpp","showAd","()V"); if(isHave) { methodInfo.env->CallStaticVoidMethod(methodInfo.classID,methodInfo.methodID); } #endif }</span>一定要加上平台判断语句。否则会出现以下bug。(具体如何调用,请参见相关文档)
<span style="font-size:18px;">错误 2 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: static bool __cdecl cocos2d::JniHelper::getStaticMethodInfo(struct cocos2d::JniMethodInfo_ &,char const *,char const *,char const *)" (__imp_? getStaticMethodInfo@JniHelper@cocos2d@@SA_NAAUJniMethodInfo_@2@PBD11@Z),该符号在函数 "public: void __thiscall HelloWorld::fun (void)" (?fun@HelloWorld@@QAEXXZ) 中被引用 E:\安卓开发工作空间\cocos2d-x-2.2\projects\jni2\proj.win32\HelloWorldScene.obj HelloCpp</span>
<span style="font-size:18px;">#pragma once #include<android/log.h> #define LOG_TAG "debug log" #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args) #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args) #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args) </span>
<span style="font-size:18px;">"E:\\迅雷下载\\android-ndk-r9-windows-x86\\android-ndk-r9\\ndk-build.cmd" Android NDK: WARNING:E:\安卓开发工作空间\cocos2d-x-2.2\/cocos2dx/Android.mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries "Compile++ thumb : cocos2dcpp_shared <= HelloWorldScene.cpp jni/../../Classes/HelloWorldScene.cpp:3:23: fatal error: JniHelper.h: No such file or directory compilation terminated. make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o] Error 1</span>
<span style="font-size:18px;">LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := cocos2dcpp_shared LOCAL_MODULE_FILENAME := libcocos2dcpp LOCAL_SRC_FILES := hellocpp/main.cpp ../../Classes/AppDelegate.cpp ../../Classes/HelloWorldScene.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes E:\安卓开发工作空间\cocos2d-x-2.2\cocos2dx\platform\android\jni LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl) $(call import-module,CocosDenshion/android) $(call import-module,extensions) $(call import-module,external/Box2D) $(call import-module,external/chipmunk)</span>
<span style="font-size:18px;">package org.cocos2dx.hellocpp; import org.cocos2dx.lib.Cocos2dxActivity; import org.cocos2dx.lib.Cocos2dxGLSurfaceView; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import com.manage.LA; public class HelloCpp extends Cocos2dxActivity { /** * 保存当前Activity实例, 静态变量 */ private static Context mActivity = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = this; LA.initSDK(mActivity, "1005020"); Log.i("HelloCpp", "onCreate"); } public Cocos2dxGLSurfaceView onCreateView() { Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this); // HelloCpp should create stencil buffer glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); return glSurfaceView; } /** * 显示广告 */ public static void showAd() { mHandler.sendEmptyMessage(0); Log.i("HelloCpp", "showAd"); } public static Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case 0: LA.showAD1(mActivity); break; } } }; static { System.loadLibrary("cocos2dcpp"); } } </span>
11.Cocos2dx2.2下使用JNI技术调用jar包里面的一些方法遇到的一些问题及解决方案。,布布扣,bubuko.com
11.Cocos2dx2.2下使用JNI技术调用jar包里面的一些方法遇到的一些问题及解决方案。
标签:cocos2dx jni
原文地址:http://blog.csdn.net/huang_cai_yuan/article/details/38303569