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

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

#61

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.


#62

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…


#63

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


#64

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

#65

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…


#66

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


#67

It’s already there.

Can you please tell me how to do this?


#68

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


#69

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


#70

The release note of v3.15 mentioned it.


#71

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


#72

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.


#73

So how build libs? With all latest?


#74

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.


#75

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


#76

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


#77

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


#78

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


#79

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.


#80

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!