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

There were no lib folder, just created it and placed jar, nothing happened… same error. Any ideas?
UPD: fixed, just should be “libs” folder. Game started… no errors but no music… thats question for another thread )


Another question:
I saw that bug with compiling is closed and fixed - Solve the error that when building android project with Android SDK Tools 25.3.x.

With old sdk tools I’ve used this command for android:
cocos gen-libs -p android --ap android-19 --app-abi x86:armeabi-v7a

And today I’ve updated to all latest sdk tools and other. Now this command not works:

Ant (Eclipse project) is not supported from Android SDK Tools 25.3.0.
Error running command, return code: 101.

Which command I should use to build to android? Documentation as usual sucks and old and not updated http://www.cocos2d-x.org/docs/editors_and_tools/cocosCLTool/

I see also @piotrros created same bug https://github.com/cocos2d/cocos2d-console/issues/420
Any solutions? @zhangxm @zhangbin

The release note of v3.15 mentioned it.

Can’t find there info or doc how to build prebuilt for android?

No, it is for the issue you met when compiling for Android. About prebuilt library, it is a feature that developer for cocos studio which is canceled. But i think the command can work since we tested it before releasing, but don’t test the prebuilt libs.

So how build libs? With all latest?

I think the release note mentioned it clearly. The error is caused that new Android SDK version remove some commands. You can copy tools folder from old version of Android SDK. Or you can try cocos gen-libs -p android --android-studio --ap android-19 --app-abi x86:armeabi-v7a to have a try.

1 Like

So as I understand this is one solution… okay, thanks. Without copying tools that command not works.

I did all the steps given by piotrros and I am getting this error:
error: /Users/toshadsalwekar/Downloads/cocos2d-x-3.15/external/flatbuffers/prebuilt-mk/…/…/…/prebuilt/android/armeabi/flatbuffers.a(flathash.o): multiple definition of ‘main’

It seems like there is something wrong in the prebuilt libraries itself, but I can’t figure out what.

Also, I don’t know if there is any way to purge just the prebuilt libraries and rebuild them; maybe that might solve the problem

Can you re-download cocos2d-x 3.15 and start from scratch?

I re-downloaded cocos2dx and now I am getting this error:

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:ProjectName:externalNativeBuildDebug’.

Build command failed.
Error while executing ‘/Users/toshadsalwekar/Library/Android/sdk/ndk-bundle/ndk-build’ with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/Users/toshadsalwekar/code/ProjectName/proj.android-studio/app/jni/Android.mk NDK_APPLICATION_MK=/Users/toshadsalwekar/code/ProjectName/proj.android-studio/app/jni/Application.mk APP_ABI=armeabi NDK_ALL_ABIS=armeabi NDK_DEBUG=1 APP_PLATFORM=android-9 NDK_OUT=/Users/toshadsalwekar/code/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=/Users/toshadsalwekar/code/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/debug/lib NDK_MODULE_PATH=/Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15:/Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/cocos:/Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/external:/Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/cocos/prebuilt-mk:/Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/extensions -j4 /Users/toshadsalwekar/code/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/debug/obj/local/armeabi/libMyGame.so}
[armeabi] Compile++ thumb: MyGame_shared <= main.cpp
[armeabi] Compile++ thumb: MyGame_shared <= AppDelegate.cpp
[armeabi] Compile++ thumb: MyGame_shared <= HelloWorldScene.cpp
[armeabi] SharedLibrary : libMyGame.so
/Users/toshadsalwekar/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin/ld: error: /Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/external/flatbuffers/prebuilt-mk/…/…/…/prebuilt/android/armeabi/flatbuffers.a(flathash.o): multiple definition of ‘main’
/Users/toshadsalwekar/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/bin/ld: /Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/external/flatbuffers/prebuilt-mk/…/…/…/prebuilt/android/armeabi/flatbuffers.a(flatc.o): previous definition here
/Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/external/flatbuffers/prebuilt-mk/…/…/…/prebuilt/android/armeabi/flatbuffers.a(flatc.o):flatc.cpp:generators: error: undefined reference to ‘flatbuffers::GenerateCppGRPC(flatbuffers::Parser const&, std::string const&, std::string const&)’
/Users/toshadsalwekar/Downloads/tools/cocos2d-x-3.15/external/flatbuffers/prebuilt-mk/…/…/…/prebuilt/android/armeabi/flatbuffers.a(flatc.o):flatc.cpp:generators: error: undefined reference to ‘flatbuffers::GenerateGoGRPC(flatbuffers::Parser const&, std::string const&, std::string const&)’
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/toshadsalwekar/code/ProjectName/proj.android-studio/app/build/intermediates/ndkBuild/debug/obj/local/armeabi/libMyGame.so] Error 1

Same problem here, even re-bulited the library with this fix still got the below error.

error: undefined reference to ‘flatbuffers::GenerateCppGRPC(flatbuffers::Parser const&, std::string const&, std::string const&)’
error: undefined reference to ‘flatbuffers::GenerateGoGRPC(flatbuffers::Parser const&, std::string const&, std::string const&)’

Edited:
If comment flathash.cpp instead of flatc.cpp will got the above error, otherwise it can compile properly.

1 Like

here is my way for Windows users … it involves a bit of copying but it Works!!

the copying is just so we can use relative paths instead of absolute paths

this is exactly how i did setup my project along-with prebuilt libs and android studio and its all in details

am using ndk R13b and cocos2d-x 3.15

first i generate prebuilt libs using this command:

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

i used android-17 because thats what i had installed in the android sdk manager

then once finished create a new cocos2d-x project

then copy cocos2d-x-3.15(which contains prebuilt libs) inside the new project folder and remove the cocos2d folder inside it and then rename your newly copied cocos2d-x-3.15 folder to cocos2d

once again we’re doing all this copying so we don’t change a lot once we open up the project in android studio

then open up the project using android studio (wait a bit until the project is done setting up first) and go to

File->project structure and set both sdk and ndk paths

then go to android.mk and replace:

$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/audio/include)

with this

$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/prebuilt-mk)

then in android studio go to build.gradel (Module: yourProjectName) and copy this into it

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion 17 // use whatever platform you have
    buildToolsVersion "26.0.1" //be careful in here to use a version of build Tools that you have

    defaultConfig {
        applicationId "org.cocos2dx.game"
        minSdkVersion 10
        targetSdkVersion 17
        versionCode 1
        versionName "1.0"

        ext {
            //read my explanation on how to change this path
            cocospath = '/Users/Joseph/Documents/MyGameBin/cocos2d'
        }

        externalNativeBuild {
            ndkBuild {
                targets "MyGame"
                arguments "NDK_MODULE_PATH=$cocospath;$cocospath/cocos;$cocospath/external;$cocospath/cocos/prebuilt-mk"
                arguments "-j" + Runtime.runtime.availableProcessors()
                abiFilters "armeabi-v7a"
            }
        }

    }

    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        jniLibs.srcDir "libs"
        manifest.srcFile "AndroidManifest.xml"
        assets.srcDir "assets"
    }

    externalNativeBuild {
        ndkBuild {
            path "jni/Android.mk"
        }
    }

    signingConfigs {

        release {
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                signingConfig signingConfigs.release
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':libcocos2dx')
}

task cleanAssets(type: Delete) {
    delete 'assets'
}
task copyAssets(type: Copy) {
    from '../../Resources'
    into 'assets'
}

clean.dependsOn cleanAssets
preBuild.dependsOn copyAssets

then go to yourProjectName then right click on cocos2d folder then properties then go to security tab then copy the full path

then now go back to build.gradel (Module: yourProjectName) and paste the path in

cocospath ='your full path here'

now your path should look like this

cocospath = '‪DriveName:\\Some\\Path\\yourProjectName\\cocos2d'

replace each double back slash with a single forward slash and also remove the drive name with the colons

here is how mine looks like

cocospath ='/Users/Joseph/Documents/MyGameBin/cocos2d'

then in android studio go to gradle.properties(Project Properties) and change
PROP_APP_ABI=armeabi
to
PROP_APP_ABI=armeabi-v7a

then go to application.mk and change
APP_ABI := armeabi
to
APP_ABI := armeabi-v7a

now you are set so just run the project and your done!

PS: do exactly as i told and nothing else and it will Work!

@Joseph39, I’m actually don’t understand why you are using windows? It’s so easy to install hackintosh or if you have money buy a real Mac. Under macOS development is amazing. Mostly you develop you game for macOS target, testing easily and quickly. Along with this later testing on iOS. Finally when your game ready for 95% you can try to run it on android.

And yea, on macOS I’ve looks like same setup for android for prebuilt libs, works well.

don’t get confused game development on win32 is also a breeze and very fast along with visual studio and when the game is almost done then start testing on android its the same plane as every one

so as you see i have no reason to switch to a mac right now … having said that i did once try hackintosh i think it was version 10. or so and it was very smooth and fast and xcode seemed to be like or even faster than visual studio

but even if you are developing on a mac there is no escaping the fact that you’re gonna have test and profile on android sooner or later and that’s exactly the moment where all hell breaks loose

and let us not forget that as one of the important steps of testing is profiling
so what am really interested about is how people profile performance in android with the surprisingly lack of ndk profilers it seems left out and no one really cares about

i tried android-ndk-profiler for some time but for some one who is used to visual studio one click profiling makes it really hard to get used to android ndk way of profiling … the setup is bit involved and you have to do every thing by the command line which makes profiling even slower and harder

we are almost in 2018 and still dont have good ndk profilers which makes you wonder what a world we are living in.

Yes.

Aha, thats was terrible period for me when I started developing for android target using android studio. Well, it’s cheap low-mid quality product, not premium like Apple, so don’t respect that development all will be flawless and simple as for iOS. You are using cheap product so you must suffer with all tools and instruments around it, sarcasm)… or not? I was, really hard…

However, I don’t understand, iOS should be n1 build target, so it’s no sense to use other OS than macOS, because you can build to iOS only from it.
That’s why after all for 2d game developers targeting to priority market(iOS) and doing some A games it’s only 1 option is - Mac.

Also, you’re using old NSK and tools, so it works… same as I’m, but I want to have an option to build prebuilt libs with all the latest updates…

By the way - it’s not working. Tested with latest build tools and get:
Ant (Eclipse project) is not supported from Android SDK Tools 25.3.0.

Is there a way to generate prebuilt libs fro android with latest updates? What command should I use?

Or maybe I can generate it with studio, how? I just need all that libs there… : /Cocos2d-x/prebuilt/android/armeabi-v7a

I;ve created another topic for that.

You have to use old build tools.

@anon98020523 yep, you should use old build tools as @piotrros said.

https://github.com/cocos2d/cocos2d-x/pull/18470 - any changes after this? Can I use latest tools? and

Will it work?

@slackmoehrle this really needs detailed info http://cocos2d-x.org/docs/en/editors_and_tools/prebuilt_libraries.html

Yes, this is “In progress” as labeled here: https://github.com/cocos2d/cocos2d-x-docs/issues/8

I anticipate getting it done this week.