Sei sulla pagina 1di 5

Compiling Cocos2D-X 3.

1x Projects Using Android Studio

Introduction

My name is Ryan Baclit, and I wrote this document after all the excitement of making my game project
work in Android Studio when the android command became a simple warning script file. It was born
out of this line after a new update of the IDE:

The android command is no longer available.


For manual SDK and AVD management, please use
Android Studio.
For command-line tools, use
tools/bin/sdkmanager and tools/bin/avdmanager
I searched a lot on the web, from forums to reference manuals of Android, until I bumped into ABIs
and putting them to use in my project. It took days of research, but it was worth it.

Let me show you my solution that worked for me. Try this on your project if you encounter compiling
problems in the shell with the latest android update.

Disclaimer

I know desperation may result in hasty actions that can be destructive for your Cocos2D-X game
project. Let me make things clear before you try anything in this document.

1. The author of this document will not be responsible for any damages on your projects in any
way. I wrote this document purely as a guide to help, and these steps worked for my project based on
my personal experience. You are still responsible for your project, not the author of this
document. If you have any shred of doubt on continuing with my solution, DO NOT CONTINUE.
Wait for the official Cocos2D-X team to resolve any android-related problem.

2. ALWAYS MAKE BACKUPS OF YOUR GAME PROJECTS. This is the most important thing
before doing any experimenting. IF you messed up your only copy of your Cocos2D-X game source
code, the author of this document will not be responsible for your actions, mistakes or errors.

RCB031320171218V1.0
Compiling Cocos2D-X 3.1x Projects Using Android Studio

Prerequisites

1. You are using Cocos2D-X 3.14 or higher in your game project.


2. You have setup NDK with your project. I used android-ndk-r13b in my project, and pointed my
Android Studio there using the Project Structure window. You can find that by clicking File →
Project Structure. Read more online about this option.
3. You have installed the latest Android Studio and opened your Cocos2D-X proj.android-studio folder
in that IDE.
4. You may have seen problems like Gradle failed to sync, with additional messages like this:

proj.android-
studio/app/jni/../../../cocos2d/external/freetype2/prebuilt/android/A
ndroid.mk:cocos_freetype2_static: LOCAL_SRC_FILES points to a missing
file

proj.android-
studio/app/jni/../../../cocos2d/external/freetype2/prebuilt/android/x
86_64/libfreetype.a exists or that its path is correct

Solution

My solution needs to make changes in the Android.mk, Application.mk, and build.gradle of your
project. If you see things that are too basic or beginner, like putting too much or too little in the code,
you are right. I am not yet and expert at doing Makefiles, gradle build files, and build variables. If you
can give shortcuts, I would gladly appreciate it. Here are the things that I did.

Read the Use the Android Studio UI section of this document:


https://developer.android.com/studio/projects/add-native-code.html

To get the general idea of how to use your Android.mk that Cocos2D-X made for Android Studio
instead of writing a new one. Like I told you, I am still not familiar with CMake or ndk-build, so I
relied on my instincts that lead me to my solution.

This time, do these.

1. Go to the 1:Project pane and find the libcocos2dx folder. Right-click on it and select Link C++
Project with Gradle.
2. Select ndk-build in the Build System dropdown box. This will let you choose an Android.mk file,
and include the associated Application.mk file with it. Click on the ellipsis and find the Android.mk
file located in the proj.android-studio folder of your game project.
3. Click Ok to save your changes. Android Studio will sync for a while to update the 1:Project pane,
and you will see a new section called External Build Files if successful.

RCB031320171218V1.0
Compiling Cocos2D-X 3.1x Projects Using Android Studio

Android.mk
This step assumes that you listed all the source code files in the LOCAL_SRC_FILES. I did this
because I do not know how to use wildcards effectively and still researching about that.

Find the line

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

// More lines of source code, if there are any.

Note that the ellipsis for going to the parent directory involves two levels up. You need to add another
level up to let Android Studio NDK find your code. To do that, add another ../ in front of the ../../ like
this:

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

After you do that in each source code you listed, save the file. You can save that with Android Studio
even if it complains that it failed to sync. Let it complain while you continue to save.

Application.mk
You need to make changes in the APP_ABI depending on the architecture that you are trying to build
your game application with. I am using x86 because that is fast on my emulator that emulates an x86
device. Yours might be different like armeabi or similar. You make the changes in your setup as needed.

Find the line with the keyword

APP_ABI

If there is a line, and it lists your target architecture, you can safely skip this section.

Else,

1. Add this line at the bottom.

RCB031320171218V1.0
Compiling Cocos2D-X 3.1x Projects Using Android Studio

APP_ABI := x86

This will force your NDK to build your app as an x86 app that can run on x86 devices and emulators.

2. Save the file. You can do this in Android Studio or an editor. Before I forget, if you save the
Android.mk and Application.mk using another editor while Android Studio is running, I recommend
clicking Build → Refresh Linked C++ Projects option to make the IDE update itself with your
changes.

I read this option online here

https://developer.android.com/studio/projects/add-native-code.html

and I think it does not hurt to run this command if you make changes using other editors outside of
Android Studio.

build.gradle
Finding the correct build.gradle file may be tricky because there are three of them under the Gradle
Scripts pane. What you need to do to find the correct one is to find the build.gradle file that does not
say Module: libcocos2dx or Project: proj.android-studio. Once you find the correct file, open it to
make changes.

Here we go!

1. Find the section starting with:

defaultConfig {

That section is a compound statement that has a closing brace. Its completed form looks like this:

defaultConfig {

// Many options depending on your project

RCB031320171218V1.0
Compiling Cocos2D-X 3.1x Projects Using Android Studio

2. Add this code before the closing curly brace.

ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'x86'
}

Just like earlier, I am building an x86 app for an x86 device, and I used ‘x86’ in the abiFilters option.
You can add more architectures depending on your needs.

Android Studio will perform a Gradle sync after each change, do a clean, and build. It will take a very
long time, but it will make your project like you did with the old android command line.

If no error comes up, congratulations! You can now build a debug source compile with Android Studio,
then build an APK of your app to be installed in your target device! You still need to manually install
your APK using the adb install command as I have not yet discovered how to do this with clicks in
Android Studio. But with this, you can continue with your Cocos2D-X game project as you did with
the android command tool using the cocos command!

Thank you for reading this document. I hope I have helped you somehow.

You can send feedback to me at my email address:

gamehelphere@gmail.com

I will try to answer if time permits. I also have work and other projects, and it might take some time for
me to reply to you. I hope you understand.

Time for me to continue with my game project and study more Android for future Cocos2D-X Android
projects!

RCB031320171218V1.0

Potrebbero piacerti anche