我早就忘记了c ++以及如何正确使用它,但现在我必须处理一个类而且我有点卡住了。 所以我使用cocos2dx为ios编写游戏,但是我使用的是Lua绑定而不是c ++。 但是......尽管如此,我还是需要编写一些使用JNI的东西...... c / c ++我们来了。
我有这样的头文件和cpp文件:
DeviceInfoAndroid.h:
#include <jni.h>
#include "../libs/cocos2dx/platform/android/jni/JniHelper.h"
extern "C" {
const char* getAndroidDeviceId();
}
DeviceInfoAndroid.cpp:
#ifndef _DEVICE_INFO_ANDROID_
#define _DEVICE_INFO_ANDROID_
#include "DeviceInfoAndroid.h"
#include <string>
extern "C" {
const char *getAndroidDeviceId() {
JniMethodInfo t;
const char* retVal;
if (JniHelper::getStaticMethodInfo(t
, "org/cocos2dx/lib/Cocos2dxActivity" //org/cocos2dx/lib/Cocos2dxActivity
, "getIMEI"
, "()Ljava/lang/String;")) {
jstring jstr;
jstr = (jstring);
t.env->CallStaticObjectMethod(t.classID, t.methodID);
char *cstr;
cstr = (char *) t.env->GetStringUTFChars(jstr, 0);
std::string ret(cstr);
t.env->ReleaseStringUTFChars(jstr, cstr);
t.env->DeleteLocalRef(jstr);
retVal = ret.c_str();
}
return retVal;
}
}
#endif
我知道所有这些#ifndef垃圾都不需要,因为我只在一个地方和一个地方使用它,所以不会发生循环依赖,但我还是添加了它[当我删除时它不会改变它]。
所以.. JniHelper.h文件的路径是正确的。它看起来像这个顺便说一句:
JniHelper.h
#ifndef __ANDROID_JNI_HELPER_H__
#define __ANDROID_JNI_HELPER_H__
#include <jni.h>
#include <string>
#include "CCPlatformMacros.h"
namespace cocos2d {
typedef struct JniMethodInfo_
{
JNIEnv * env;
jclass classID;
jmethodID methodID;
} JniMethodInfo;
class CC_DLL JniHelper
{
public:
static JavaVM* getJavaVM();
static void setJavaVM(JavaVM *javaVM);
static jclass getClassID(const char *className, JNIEnv *env=0);
static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode);
static bool getMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode);
static std::string jstring2string(jstring str);
private:
static JavaVM *m_psJavaVM;
};
}
#endif // __ANDROID_JNI_HELPER_H__
好的..现在..我使用makefile来构建它[它是更大的构建过程的一部分,但是对于这个特定的文件它看起来像这样(它与.h和.cpp位于同一目录中)我粘贴在这里的文件]。
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic
LOCAL_SRC_FILES := \
AppDelegate.cpp \
DeviceInfoAndroid.cpp \
DeviceInfo.cpp \
../libs/lua/cocos2dx_support/LuaEngineImpl.cpp \
../libs/lua/cocos2dx_support/Cocos2dxLuaLoader.cpp \
../libs/lua/cocos2dx_support/LuaCocos2d.cpp \
../libs/lua/cocos2dx_support/LuaEngine.cpp \
../libs/lua/CocosDenshion_support/LuaSimpleAudioEngine.cpp \
../libs/lua/exts/luallthread/llthreads.c \
../libs/lua/exts/luasocket/auxiliar.c \
../libs/lua/exts/luasocket/buffer.c \
../libs/lua/exts/luasocket/except.c \
../libs/lua/exts/luasocket/inet.c \
../libs/lua/exts/luasocket/io.c \
../libs/lua/exts/luasocket/luasocket.c \
../libs/lua/exts/luasocket/mime.c \
../libs/lua/exts/luasocket/options.c \
../libs/lua/exts/luasocket/select.c \
../libs/lua/exts/luasocket/tcp.c \
../libs/lua/exts/luasocket/timeout.c \
../libs/lua/exts/luasocket/udp.c \
../libs/lua/exts/luasocket/unix.c \
../libs/lua/exts/luasocket/usocket.c \
../libs/lua/exts/lualoadexts.c \
../libs/lua/exts/luasocketscripts.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../libs/cocos2dx \
$(LOCAL_PATH)/../libs/cocos2dx/platform \
$(LOCAL_PATH)/../libs/cocos2dx/include \
$(LOCAL_PATH)/../libs/cocos2dx/lua_support \
$(LOCAL_PATH)/../libs/CocosDenshion/include \
$(LOCAL_PATH)/../libs/lua/lua \
$(LOCAL_PATH)/../libs/lua/tolua \
$(LOCAL_PATH)/../libs/lua/cocos2dx_support \
$(LOCAL_PATH)/../libs/lua/CocosDenshion_support \
$(LOCAL_PATH)/../libs/lua/exts \
$(LOCAL_PATH)/../libs/lua/exts/luallthread \
$(LOCAL_PATH)/../libs/lua/exts/luasocket \
$(LOCAL_PATH)/../libs/cocos2x/platform/android/jni \
LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../Android/libs/$(TARGET_ARCH_ABI)) \
-L$(call host-path, $(LOCAL_PATH)/../libs/cocos2dx/platform/third_party/android/libraries/$(TARGET_ARCH_ABI)) -lcurl \
-lcocos2d \
-lcocosdenshion \
-llua
include $(BUILD_SHARED_LIBRARY)
正如您所看到的,我已将JniHelper.h文件的路径添加到LOCAL_C_INCLUDES。 我已经尝试删除路径并只使用“JniHelper.h”但它会抛出一个错误,说该文件不存在。
我在构建期间遇到的错误是:
Compile++ thumb : game_logic <= DeviceInfoAndroid.cpp
jni/../../Classes/DeviceInfoAndroid.cpp: In function 'const char* getAndroidDeviceId()':
jni/../../Classes/DeviceInfoAndroid.cpp:10: error: 'JniMethodInfo' was not declared in this scope
jni/../../Classes/DeviceInfoAndroid.cpp:10: error: expected ';' before 't'
jni/../../Classes/DeviceInfoAndroid.cpp:12: error: 'JniHelper' has not been declared
jni/../../Classes/DeviceInfoAndroid.cpp:12: error: 't' was not declared in this scope
jni/../../Classes/DeviceInfoAndroid.cpp:17: error: expected primary-expression before ')' token
make: *** [obj/local/armeabi/objs-debug/game_logic/DeviceInfoAndroid.o] Error 1
任何提示? 我做错了什么? 我知道为了用C ++编写,我需要学习很多东西,但正如我所提到的......我不打算用C ++编写,我需要为我所拥有的每种设备类型创建几行代码在我的游戏中支持。我已经完成了Objective-c和c ++混合以获得iOS设备ID。
编辑: 删除我的一些评论时,我设法删除了包含DeviceInfoAndroid.cpp。我已经纠正了上面的代码。对不起。
答案 0 :(得分:9)
编辑完成后,这是我更新的答案:
JniMethodInfo存在于命名空间cocos2d中,因此您应该将其引用为:
cocos2d::JniMethodInfo t;
JniHelper同样如此:
if (cocos2d::JniHelper::getStaticMethodInfo
等
在编辑前回答:(不再相关,但保存在此处供参考)
您需要将包含从头文件移动到cpp文件。 DeviceInfoAndroid.h不会自动包含在DeviceInfoAndroid.cpp中。您必须手动包含它。