Cocos2d-x:如何将使用 XCode 开发的 Cocos2d-x 项目移植到 Android(通过 Eclipse)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9192058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:15:45  来源:igfitidea点击:

Cocos2d-x: How to port a Cocos2d-x project developed with XCode to Android (via Eclipse)?

androideclipsexcodecocos2d-iphonecocos2d-x

提问by Regis_AG

I have developed a Cocos2d-X app with XCode which works perfectly when I launch it from XCode on an iOS device.

我开发了一个带有 XCode 的 Cocos2d-X 应用程序,当我在 iOS 设备上从 XCode 启动它时,它可以完美运行。

Now, I want to port it to Android. What I understand is that I have to find a way to import my "XCode project" to Eclipse in order to be able to compile it and launch it on an Android device.

现在,我想将其移植到 Android。我的理解是,我必须找到一种方法将我的“XCode 项目”导入 Eclipse,以便能够编译它并在 Android 设备上启动它。

I am currently trying to convert this project to an hybrid iOS/Android project following this tutorial: http://gameit.ro/2012/01/creating-an-iphone-and-android-cocos2d-x-hybrid-project-updated/#comment-640

我目前正在尝试按照本教程将此项目转换为混合 iOS/Android 项目:http: //gameit.ro/2012/01/creating-an-iphone-and-android-cocos2d-x-hybrid-project-updated /#comment-640

Nevertheless, I don't see any of my cpp classes (cocos2d-x C++ classes) appearing in Eclipse when I open this hybrid project and I get the following errors when trying to compile it with build_native.sh:

尽管如此,当我打开这个混合项目时,我没有看到我的任何 cpp 类(cocos2d-x C++ 类)出现在 Eclipse 中,并且在尝试使用 build_native.sh 编译它时出现以下错误:

Compile++ thumb  : game_logic <= AppDelegate.cpp
jni/../../Classes/AppDelegate.cpp:14:23: error: IntroMenu.h: No such file or directory
jni/../../Classes/AppDelegate.cpp:15:23: error: GameLayer.h: No such file or directory
jni/../../Classes/AppDelegate.cpp:16:26: error: ScoreManager.h: No such file or directory
jni/../../Classes/AppDelegate.cpp: In member function 'virtual bool AppDelegate::applicationDidFinishLaunching()':
jni/../../Classes/AppDelegate.cpp:99: error: invalid use of incomplete type 'struct ScoreManager'
jni/../../Classes/AppDelegate.h:17: error: forward declaration of 'struct ScoreManager'
jni/../../Classes/AppDelegate.cpp:101: error: invalid use of incomplete type 'struct ScoreManager'
jni/../../Classes/AppDelegate.h:17: error: forward declaration of 'struct ScoreManager'
jni/../../Classes/AppDelegate.cpp:118: error: 'GameLayer' has not been declared
jni/../../Classes/AppDelegate.cpp:120: error: 'IntroMenu' has not been declared
make: *** [obj/local/armeabi/objs-debug/game_logic/AppDelegate.o] Error 1
macbook-de-regis-andre-2:android regisandre$ ./build_native.sh

Do I have to import some files to Eclispe? Do I have to modify some android.mk files? Something else?

我是否必须将一些文件导入 Eclispe?我需要修改一些 android.mk 文件吗?还有什么?

Anybody can help me or write a tutorial on this topic? Thanks !!

任何人都可以帮助我或编写有关此主题的教程吗?谢谢 !!

回答by clawoo

From the looks of it you need to add your custom created files to Classes/Android.mk in the LOCAL_SRC_FILES section like this:

从它的外观来看,您需要将自定义创建的文件添加到 LOCAL_SRC_FILES 部分的 Classes/Android.mk 中,如下所示:

LOCAL_SRC_FILES := AppDelegate.cpp \
                   HelloWorldScene.cpp \
                   IntroMenu.cpp \
                   GameLayer.cpp \
                   ScoreManager.cpp

You need to do this in order to let the android build file know about the new files which need to be included in the build process.

您需要这样做是为了让 android 构建文件知道需要包含在构建过​​程中的新文件。

You will need to do this, afaik, for each new source file that you add to the project.

对于添加到项目中的每个新源文件,您都需要执行此操作。

回答by manicaesar

@clawoo is right, but you do not have to include every file you add to the project. Instead, you can do the following and forget about it ;)

@clawoo 是对的,但您不必包含添加到项目中的每个文件。相反,您可以执行以下操作并忘记它;)

In order to not need to update the file every time new source file is added to the project, you can use the following script (found here: http://www.cocos2d-x.org/boards/6/topics/5321)

为了不需要每次向项目添加新源文件时都更新文件,您可以使用以下脚本(可在此处找到:http: //www.cocos2d-x.org/boards/6/topics/5321

dirs := $(shell find $(LOCAL_PATH) -type d)

cppfilestemp1 := $(shell find $(LOCAL_PATH) -type d)
cppfilestemp2 := $(shell find $(cppfilestemp1) -name *.cpp)
cppfilestemp3 := $(sort $(cppfilestemp2))
cppfiles := $(subst $(LOCAL_PATH)/,,$(cppfilestemp3))

LOCAL_SRC_FILES := \
           $(cppfiles)

Please remember, that if you have the files somewhere else, e.g.:

请记住,如果您在其他地方有这些文件,例如:

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

you can do the following:

您可以执行以下操作:

cppfilestemp1 := $(shell find $(LOCAL_PATH)/../../../Classes/ -type d)

and

LOCAL_SRC_FILES := main.cpp
LOCAL_SRC_FILES += $(cppfiles)

In my case it worked.

就我而言,它奏效了。

HINT:

暗示:

If you have problems with compiler complaining about: 'No rule to make target /.../', I suggest to delete in Eclipse contents of obj/local/armeabi/objs-debug/game_shared folder. Then, rerun build_native.sh and refresh (F5) contents of obj folder.

如果编译器有问题抱怨:'No rule to make target /.../',我建议删除 Eclipse 中 obj/local/armeabi/objs-debug/game_shared 文件夹的内容。然后,重新运行 build_native.sh 并刷新 (F5) obj 文件夹的内容。