11.Cocos2dx2.2下使用JNI技术调用jar包里面的一些方法遇到的一些问题及解决方案。

<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。
此时可能还会遇到找不到jni.h和jni_md.h的问题,解决方案为在%JAVA_HOME%/include/下找到 jni.h,在%JAVA_HOME%/include/win32/下找到jni_md.h,复制到“Visual Studio目录/VC/include/”下,即可解决问题。


步骤二:编写调用java端方法的代码。
<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>

步骤三:将其移植到Android平台,此时会提示在CCPlatformDefine.h文件中log.h: no such file or directory,即log.h找不到。

解决方案:
1.修改Android.mk文件配置,添加如下语句
 LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
2.修改CCPlatformDefine.h文件
注释掉#include "log.h",添加#include "ALog.h"。
3.新建ALog.h文件,写入以下内容,并将其放置于与CCPlatformDefine.h所在的同一目录下。
<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>

11.Cocos2dx2.2下使用JNI技术调用jar包里面的一些方法遇到的一些问题及解决方案。_第1张图片

步骤四:搞完第三步之后,再编译运行,还会提示如下错误。
<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>

即找不到JniHelper.h这个文件,解决方案如下。

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

就是在LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes\后面追加JniHelper.h文件所在的路径,让编译器编译本地的文件。相关的Android.mk里面的一些基础知识,参见http://www.himigame.com/lua-game/1388.html
如果仍然提示找不到头文件
则在项目的cpp文件中第一步的路径改成下面的
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
#include "platform/android/jni/JniHelper.h"
#endif

步骤五:编写供c++调用的java方法,如果是第三方jar里面的方法的话,建议用Handler去触发,让其处于在UI线程中运行,否则可能出错。
<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>


参考文章:
1.http://www.firedragonpzy.com.cn/index.php/archives/3140



你可能感兴趣的:(jni,cocos2dx)