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

Strange, did you ran test project I posted above? What version of NDK, Android Studio are you using? Did you download cocos2d-x with that pull request merged? Did you build precompiled libs?

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