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

No I think this is not related to fmod, I mean when u copy pasting there could be some issue with Extra space or some special symbol on end of line. Also if u have different folder structure- you could wrongly modified androi.mk inside fmod.

Iā€™ve checked paths, all correct. Ok, I just built it when placed at cocos/external.

So, itā€™s works when FMOD in extra:
$(call import-module,external/FMOD/android)

and not works when FMOD in Classes

$(LOCAL_PATH)/../../../Classes/FMOD/android)

I would really want to place it in Classesā€¦ Looks like path for Android.mk wrong when we trying to locate it from project local pathā€¦

So it means you Local-0path vsriable little different interpereted by android.mk, so its merging that line with previous lines. I think there was some command call add path or import path which need to be placed before that local path include to reset directory before looking for android files. Now from mobile cant check exact names of how it called. Just some android.mk versions from cocos there was this comand

Maybe calling FMOD before can change somethingā€¦ well at least error now more informative:

$(call import-module,$(LOCAL_PATH)/../../../Classes/FMOD/android)
$(call import-module,./prebuilt-mk)

Result:

Build command failed.
Android NDK: /Volumes/MacData/GitProjects/ProjectName/proj.android-studio/app/jni/Android.mk: 
Cannot find module with tag 
'/Volumes/MacData/GitProjects/ProjectName/proj.android-studio/app/jni/../../../Classes/FMOD/android' in import path

Ok, Iā€™ve compiled it.

LOCAL_STATIC_LIBRARIES := cocos2dx_static
LOCAL_SHARED_LIBRARIES := fmod

include $(BUILD_SHARED_LIBRARY)

$(call import-add-path, $(LOCAL_PATH)/../../../Classes/FMOD)
$(call import-module,./android)

$(call import-module,./prebuilt-mk)

With help of this https://stackoverflow.com/questions/26662264/cannot-find-module-with-tag-in-import-path-android-ndk

However, when Iā€™m editing AppActivity.java itā€™s says that import org.fmod.*; canā€™t resolve symbol fmodā€¦

There is jar file for fmod, in my screenshot it was just in android folder, you should place this into lib folder or configure in gradle to load it that jar

Itā€™s already there.

Can you please tell me how to do this?

If u have that jar just drop into proj.android-studio/app/lib folder cocos by default taking it from there.

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!