How to link prebuilt Cocos2D X C++ library to Android Studio project?

android studio 2.2.3
NDK 13.1.334577 (using android studio SDK manager)
Yes I cloned from Git then download-deps, submodule update and build precompiled libs

Your project gives error in setting.gradle

This is a gradle script error and I have to say it’s very strange.

I just back to this. Just ported my game to C++ and so I have some stable build for iOS. Now I want to build it for android. But now I’m getting “The android command is no longer available.” while trying to execute command: “cocos gen-libs -p android”.

That has nothing to do with gen-libs. Check out these issues: https://github.com/cocos2d/cocos2d-x/issues/17424 and https://github.com/cocos2d/cocos2d-console/issues/404

1 Like

i have posted a Q&A about exactly your problem : http://stackoverflow.com/questions/42552341/why-cocos-compile-p-android-gives-the-android-command-is-no-longer-available/42552342#42552342

you have updated SDK tools, you need to revert back to previous version until this issue is resolved.

Ok, I’ve downgraded to 25.2.3 and able to build libs:

cocos gen-libs -p android --ap android-16 --app-abi armeabi-v7a

Then I’ve downloaded project from this post: How to link prebuilt Cocos2D X C++ library to Android Studio project? - #11 by piotrros

Fixed paths for my cocos2d-x in several files and got this:

Error while executing '/Volumes/MacData/Android/SDK/ndk-bundle/ndk-build' with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/jni/Android.mk NDK_APPLICATION_MK=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/jni/Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=0 APP_PLATFORM=android-9 NDK_OUT=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/release/obj NDK_LIBS_OUT=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/release/lib NDK_MODULE_PATH=/Volumes/MacData/Cocos2d-x:/Volumes/MacData/Cocos2d-x/cocos:/Volumes/MacData/Cocos2d-x/external:/Volumes/MacData/Cocos2d-x/cocos/prebuilt-mk -j4 APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
Android NDK: /Volumes/MacData/Cocos2d-x/cocos/editor-support/spine/prebuilt-mk/Android.mk: Cannot find module with tag 'editor-support/spritebuilder/prebuilt-mk' in import path    
Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?    
Android NDK: The following directories were searched:    
Android NDK:         
Build command failed.
Error while executing '/Volumes/MacData/Android/SDK/ndk-bundle/ndk-build' with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/jni/Android.mk NDK_APPLICATION_MK=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/jni/Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=1 APP_PLATFORM=android-9 NDK_OUT=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=/Users/KAMIKAZE/Desktop/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/debug/lib NDK_MODULE_PATH=/Volumes/MacData/Cocos2d-x:/Volumes/MacData/Cocos2d-x/cocos:/Volumes/MacData/Cocos2d-x/external:/Volumes/MacData/Cocos2d-x/cocos/prebuilt-mk -j4 APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
Android NDK: /Volumes/MacData/Cocos2d-x/cocos/editor-support/spine/prebuilt-mk/Android.mk: Cannot find module with tag 'editor-support/spritebuilder/prebuilt-mk' in import path    
Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?    
Android NDK: The following directories were searched:    
Android NDK:         
/Volumes/MacData/Cocos2d-x/cocos/prebuilt-mk/Android.mk

My config:

Also, very strange thing. I downloaded your project and edited path’s for my location of cocos2d-x.
Next, when I opened it in AS I got an error:

And when I clicked “Details…”:

Why is that? Where it finds you account path’s? I can’t find it… to fix.

Bash profile:

# Add environment variable COCOS_CONSOLE_ROOT for cocos2d-x
export COCOS_CONSOLE_ROOT=/Volumes/MacData/Cocos2d-x/tools/cocos2d-console/bin
export PATH=$COCOS_CONSOLE_ROOT:$PATH

# Add environment variable COCOS_X_ROOT for cocos2d-x
export COCOS_X_ROOT=/Volumes/MacData
export PATH=$COCOS_X_ROOT:$PATH

# Add environment variable COCOS_TEMPLATES_ROOT for cocos2d-x
export COCOS_TEMPLATES_ROOT=/Volumes/MacData/Cocos2d-x/templates
export PATH=$COCOS_TEMPLATES_ROOT:$PATH

# Add environment variable NDK_ROOT for cocos2d-x
export NDK_ROOT=/Volumes/MacData/Android/SDK/ndk-bundle
export PATH=$NDK_ROOT:$PATH

# Add environment variable ANT_ROOT for cocos2d-x
export ANT_ROOT=/usr/local/Cellar/ant/1.9.7/bin
export PATH=$ANT_ROOT:$PATH

# Add environment variable ANDROID_SDK_ROOT for cocos2d-x
export ANDROID_SDK_ROOT=/Volumes/MacData/Android/SDK
export PATH=$ANDROID_SDK_ROOT:$PATH
export PATH=$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$PATH

Open settings.gradle file and fix libcocos2dx path. It’s definitely “my” path :slight_smile:

By the way “pierdoly” means “unnecessary things” in English :stuck_out_tongue:

That path was fixed after downloading, as long as other. So, it’s like something else… hmm maybe because I’m using http://github.com/newnon/cocos2d-x

It’s can’t find /prebuilt-mk/ but this folder should be or not?

Where did you get “spritebuilder” in editor-support directory? I don’t have such a directory. Isn’t it connected with SpriteBuilder-X? Try fresh and clean cocos2d-x copy. Most probably you haven’t added spritebuilder in Android.mk.

Yes for SBX. https://github.com/newnon/cocos2d-x/tree/v3.13.1/cocos/editor-support/spritebuilder
I can’t imagine cocos2d-x without it. But SBX can’t work with official repo, there are too many changes in newnon/cocos2d-x for support of SBX, which can’t be easy merged… and I don’t think cocos2d-x team really want or interested in it.

Later I will try just to compile with official repo, if it works will see, what I need to fix in my project from newnon/cocos2d-x.

Open cocos/prebuilt-mk/Android.mk and add missing spritebuilder module there.

I’ve tried that finally and everything works. My project structure is identical to @piotrros one except for

targets “MyGame”
arguments “NDK_MODULE_PATH=$cocospath:$cocospath/cocos:$cocospath/external:$cocospath/cocos/prebuilt-mk:$cocospath/extensions”
arguments “-j” + Runtime.runtime.availableProcessors()

which my gradle doesn’t like; but that’s no problem because i’ve left all the

$(call import-add-path,${COCOS2DX_ROOT})
$(call import-add-path,${COCOS2DX_ROOT}/external)
$(call import-add-path,${COCOS2DX_ROOT}/cocos)
$(call import-add-path,${COCOS2DX_ROOT}/cocos/audio/include)

so it works the same.
Debug is working, both on emulator and devices.
Only thing I’m still missing is complete project structure. I mean the include dirs, cocos/…/*.h
Those aren’t shown in android studio, even if the debugger finds them. Nothing particularly important, anyway, just for the sake of curiosity…

This solution uses prebuilt libraries so you won’t be able to see files in cocos directory.

Hmm, I have already there:

$(call import-module, editor-support/spine/prebuilt-mk)
$(call import-module, editor-support/spritebuilder/prebuilt-mk)

But what I’ve found , than in folder: ../Cocos2d-x/cocos/editor-support/cocosbuilder/ - there is “prebuilt-mk”, but not in ../Cocos2d-x/cocos/editor-support/spritebuilder folder. Anyway libs was build ok for android and iOS. How prebuilt-mk folder generated? For some reason it’s not generated for spritebuilder, but ok for cocosbuilder folder…
Ok, temporary I just copied it from cocosbuilder and modified it accordingly to fit spritebuilder names.

Now, I’ve problem with:
Error:(1508, 25) error: no member named 'stoi' in namespace 'std'
How this can be? For iOS again all compiles ok.

Also additional question: in Application.mk I’ve found line APP_ABI := armeabi, but it’s should be armeabi-v7a? Isn’t it? Because I’m compiling for it using:
cocos gen-libs -p android --ap android-16 --app-abi armeabi-v7a - is that right by the way?

Sure it builds fine for iOS, because .mk files are for Android. You should recreate “prebuilt-mk” for spritebuilder. I’m not sure how to do this (these .mk can be pretty hard to make). Anyway, I had the similar problem with latest audio engine change - it was working, but I couldn’t make prebuilt libs. The author of this pull request, @dumganhar, may be able to help you, as he fixed that problem.

As for the error no member named 'stoi' in namespace 'std': is it in spritebuilder itself or somewhere else?

APP_ABI may be armeabi-v7a, armeabi it’s just the default setting. armeabi is backward compatible so I don’t think it’d be any issue. You won’t be able to debug precompiled lib anyway. Also if you specify it in gen-libs it should override that value.

Yea, I just copied it from cocosbuilder and fixed names inside.

This in source code of my game. And I “fixed” this by replacing with atoi. Don’t know why compiler for android can’t find stoi

In gen-libs yes, but I’m talking about Application.mk for my game. I just saw that armeabi like old, and better to use armeabi-v7a there?

And finally I’ve made it. Lol after 5 days of trying. Thanks for help. Ohh, this android environment is so crap… really want go back and develop games only for iOS, where everything is just works. But anyway, I need my game on both platforms…

Yes, android has a problem with some std built-in functions like stoi.

Application.mk for game, well, just as I said, I think it’s overrided be gradle.

Cocos uses armeabi as default, but I personally use armeabi-v7a. armeabi devices are very old, for sure below android 4.0, which I don’t support anyway (cuz google services and facebook). armeabi-v7a give the ease of debugging on all my devices.

I see, and so how to change cocos to configure it to use armeabi-v7a? Or this is it:
cocos gen-libs -p android --ap android-16 --app-abi armeabi-v7a ?

This is it.

1 Like

After a whole day of experimenting, I managed to get this working in Windows. Don’t know if this is still helpful to you but here’s the tweaks I had to make to piottros’ steps above (also I precompiled the libs using --app-abi armeabi:x86 to support both arm and x86):

Step 5:
Make sure you use a relative path to the libcocos2dx directory in settings.gradle e.g.
../../../../frameworks/cocos2d-x-3.14.1/cocos/platform/android/libcocos2dx

Step 6:
colon : characters such as in C:\ break the NDK_MODULE_PATH parameter on Windows, so you need to change them for semicolons ; if you’re using an absolute path for the “cocospath” string. Those lines in my build.gradle file look like this:
cocospath = "C:/users/dev/code/frameworks/cocos2d-x-3.14.1"
arguments "NDK_MODULE_PATH=$cocospath;$cocospath/cocos;$cocospath/external;$cocospath/cocos/prebuilt-mk;$cocospath/extensions"

It looks as though the cocos2d-x dev team are already fixing this path issue for the next release though :slight_smile:

Thanks to @piotrros for posting the steps!