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

@piotrros, read the error again. Got rid of this error by commenting out this line in jni/Android.mk

#$(call import-module,.)

now I have some issues with finding coco2d-x header files which I should be able to fix…

You should have this:

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

not

$(call import-module,.)

If it still won’t work show your Android.mk file.

@piotrros, still no luck. As you said my NDK_MODULE_PATH is not taking effect and I am still unable to build your ProjectName temple project.

I have tried changing the build.gradle file NDK_MODULE_PATH section every way. Single v double quotes. $COCOS_X_ROOT v local cocospath variable v hard coded paths. ‘:’ v ‘;’ path separators. Commenting out the line in the build.gradle file

//arguments "NDK_MODULE_PATH=$cocospath:…

shows no effect and I get the same error?? Sorry to ask again - Any idea? I will ask the community in a separate topic.

My jni/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyGame_shared
LOCAL_MODULE_FILENAME := libMyGame
LOCAL_SRC_FILES := hellocpp/main.cpp
…/…/…/Classes/AppDelegate.cpp
…/…/…/Classes/HelloWorldScene.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/…/Classes
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
#$(call import-module,.)
$(call import-module,./prebuilt-mk)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END

My build.gradle:

apply plugin: ‘com.android.application’

android {
compileSdkVersion 23
buildToolsVersion “23.0.3”

defaultConfig {
    applicationId "org.cocos2dx.ProjectName"
    minSdkVersion 10
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    ext {
  cocospath='/Users/NF/SDK/cocos2d-x-3.14.1'
    }
   externalNativeBuild {
            ndkBuild {
                targets "MyGame"
                arguments "NDK_MODULE_PATH=$cocospath:$cocospath/cocos:$cocospath/external:$cocospath/cocos/prebuilt-mk:$cocospath/extensions"
                arguments "-j" + Runtime.runtime.availableProcessors()
                abiFilters "armeabi"
            }
//          available abiFilters (for debugging):
//          abiFilters "x86", "armeabi", "armeabi-v7a", "arm64-v8a"
    }
}
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

Hi there again :slight_smile:
I’m currently trying to compile my project with FMOD. I’m really don’t understand how to use it with android… on iOS all simple as usually, integrated it in no time… Damn android, really crap ideology of everything from design to development process. Anyway…

I’ve found this http://www.fmod.org/documentation/#content/generated/platform_android/basics.html
There are intrusions:

Android.mk

LOCAL_PATH := $(call my-dir)

#
# FMOD Shared Library
# 
include $(CLEAR_VARS)

LOCAL_MODULE            := fmod
LOCAL_SRC_FILES         := ../../lowlevel/lib/$(TARGET_ARCH_ABI)/libfmodL.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../lowlevel/inc

include $(PREBUILT_SHARED_LIBRARY)

But I don’t understand how to add it into current Android.mk file. Can someone please help me with this? @piotrros @energyy

So what I’m usualy doing for Andoroid:

I’m placing fmod files to following dir: cocos2d/external/fmod

Using following folders structure:

Android.mk inside fmod/android:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := fmod
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfmod.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include
include $(PREBUILT_SHARED_LIBRARY)



include $(CLEAR_VARS)

LOCAL_MODULE := fmodstudio
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfmodstudio.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include
include $(PREBUILT_SHARED_LIBRARY)

Inside JNI - Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

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

LOCAL_MODULE := MyGame_shared

LOCAL_MODULE_FILENAME := libMyGame

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../../Classes/AppDelegate.cpp \
                   ../../../Classes/HelloWorldScene.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END


LOCAL_STATIC_LIBRARIES := cocos2dx_static

LOCAL_SHARED_LIBRARIES := fmod
LOCAL_SHARED_LIBRARIES += fmodstudio

# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)

$(call import-module,.)
$(call import-module,external/fmod/prebuilt/android)


# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END

AppActivity.java:

package org.cocos2dx.cpp;

//FMOD
import org.fmod.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

import org.cocos2dx.lib.Cocos2dxActivity;

public class AppActivity extends Cocos2dxActivity {


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        org.fmod.FMOD.init(this);

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        org.fmod.FMOD.close();
    }

    static {


        // Try logging libraries...
        // Try debug libraries...
        try { System.loadLibrary("fmodD");
            System.loadLibrary("fmodstudioD"); }
        catch (UnsatisfiedLinkError e) { }
        // Try logging libraries...
        try { System.loadLibrary("fmodL");
            System.loadLibrary("fmodstudioL"); }
        catch (UnsatisfiedLinkError e) { }
        // Try release libraries...
        try { System.loadLibrary("fmod");
            System.loadLibrary("fmodstudio"); }
        catch (UnsatisfiedLinkError e) { }

        //System.loadLibrary("stlport_shared");
        System.loadLibrary("cocos2dcpp");
    }

}

This was done for older cocos in new name of: cocos2dcpp was changed to MyGame.

While iOS integration I just placed FMOD into /Classes/FMOD, so it’s not in cocos2d/external/fmod… and cocos2d-x is not a place where I want to store it. I want to save it in my project.

My cocos2d-x folder for all projects located at /Volumes/MacData/Cocos2d-x
Current project located at: /Volumes/MacData/GitProjects/ProjectName

Structure of FMOD is: /Volumes/MacData/GitProjects/ProjectName/Classes/FMOD
here is what inside FMOD folder:

  • /iOS/

  • /Mac/

  • /android/Android.mk
    /android/api/lowlevel/inc/ - header filles here
    /android/api/lowlevel/lib/ - libs files here

I tried to edit Android.mk at JNI folder as per your recommendation but with my folders paths and here it is:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

$(call import-add-path,/Volumes/MacData/Cocos2d-x)
$(call import-add-path,/Volumes/MacData/Cocos2d-x/cocos)
$(call import-add-path,/Volumes/MacData/Cocos2d-x/cocos/prebuilt-mk)
$(call import-add-path,/Volumes/MacData/Cocos2d-x/external)
$(call import-add-path,$(LOCAL_PATH)/../../../Classes/FMOD/android/api/lowlevel/inc)

LOCAL_MODULE := MyGame_shared

LOCAL_MODULE_FILENAME := libMyGame

LOCAL_SRC_FILES := hellocpp/main.cpp \ 
                   ../../../Classes/AppDelegate.cpp \
                   ../../../Classes/HelloWorldScene.cpp \                     
                   ../../../Classes/AudioManager.cpp


LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END


LOCAL_STATIC_LIBRARIES := cocos2dx_static

LOCAL_SHARED_LIBRARIES := fmod

# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)


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

# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END

But it gives me always:

Build command failed.
 ......
 Android NDK: /Volumes/MacData/Cocos2d-x/external/flatbuffers/Android.mk:
 Cannot find module with tag 
'/Volumes/MacData/Cocos2d-x/external/flatbuffers/../../../Classes/FMOD/android' 
 in import path Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?    

I’m using only fmod, not needed fmodstudio.

I think it is bad place for Fmod as u including into android or ios build other platfrom files.

That compile error ussualy comes when Android,mk format is incorrect, like space or something added or wrong $(call import-add-path) also its not clear what in your Android.mk for FMDO where libs files located…

I’ve deleted iOS and Mac inside FMOD folder, but same error, so I believe that problem is in something different.

I’ve just added new lines from your file and got that errors…
Hmm, but $(call import-add-path,$(LOCAL_PATH)/…/…/…/cocos2d/cocos/audio/include)

cocos2d/cocos/audio/include ? Should I add some there? Or it’s not related to FMOD?


UPD:
My Android.mk for FMOD:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := fmod
LOCAL_SRC_FILES := ./api/lowlevel/lib/$(TARGET_ARCH_ABI)/libfmod.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/api/lowlevel/inc
include $(PREBUILT_SHARED_LIBRARY)

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.