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

#39

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 ?


A preconfigured environment to end all compiling frustration
#40

This is it.


#41

After a whole day of experimenting, I managed to get this working in Windows. Don’t know if this is still helpful to you but here’s the tweaks I had to make to piottros’ steps above (also I precompiled the libs using --app-abi armeabi:x86 to support both arm and x86):

Step 5:
Make sure you use a relative path to the libcocos2dx directory in settings.gradle e.g.
../../../../frameworks/cocos2d-x-3.14.1/cocos/platform/android/libcocos2dx

Step 6:
colon : characters such as in C:\ break the NDK_MODULE_PATH parameter on Windows, so you need to change them for semicolons ; if you’re using an absolute path for the “cocospath” string. Those lines in my build.gradle file look like this:
cocospath = "C:/users/dev/code/frameworks/cocos2d-x-3.14.1"
arguments "NDK_MODULE_PATH=$cocospath;$cocospath/cocos;$cocospath/external;$cocospath/cocos/prebuilt-mk;$cocospath/extensions"

It looks as though the cocos2d-x dev team are already fixing this path issue for the next release though :slight_smile:

Thanks to @piotrros for posting the steps!


#42

@piotrros, thanks for all the info & support. Using these instructions I have a project compiling and debugging from Android Studio.

What I cannot see is the C++ files in my cocos2d-x project. Can you advise me please?

Thanks again!


#43

Did you open proj.android-studio and not the whole project directory? Can you switch Project to Android in the top bar (above libcocos2dx)?


#44

@piotrros, I think the main problem is that I don’t know what I am doing in Android Studio (& also Cocos2d-x and SDK BOX) my first project and I am struggling…:unamused:

Open the project.android-studio and I can build and run but not ‘see’ or ‘debug’ my C++ files, Android view:

Still in project.android-studio and I can build and run, Project view:

I can open the parent cocos2d-x project folder, and ‘see’ my C++ files, but I cannot debug.

Project view:

Android view:

I have tried to create a Run configuration for the ‘parent’ folder but I seem unable to assign a Module. Probably the wrong approach :expressionless:

Any hints really appreciated, I am getting increasingly frustrated! Thanks.

cocos2d-x 3.14.1, ndk r14b, build android-9 <=> android-25, tools 25.0.2

BTW I have been following these discussions:




#45

It depends which android studio version and cocos version u using, debug in cocos by default enabled only in 3.15 branch and for it you need newest android studio


#46

Do you use latest Android Studio? (2.3) Is my template project How to link prebuilt Cocos2D X C++ library to Android Studio project? working for you?


#47

@energyy, thanks for the points made but some people (see @piotrros below) report being able to use AS with cocos 3.14.x. I would like to achieve this, v.much! :slight_smile:


#48

@piotrros, thanks for coming back. What I did:
Downloaded & unzipped your ‘template’ project.
Changed ndk to 13b.
Installed required android sdks and build tools version.
Ran => cocos gen-libs -p android --ap android-10 (ok)

Updated pros.android-studio/settings.gradle
//project(’:libcocos2dx’).projectDir = new File(’/Users/piotr/Documents/pierdoly/cocos2d-x-3.14/cocos/platform/android/libcocos2dx’)
replace with
project(’:libcocos2dx’).projectDir = new File(’/Users/NF/SDK/cocos2d-x-3.14.1/cocos/platform/android/libcocos2dx’)

Updated proj.android-studio/app/build.gradle
//arguments “NDK_MODULE_PATH=$cocospath:$cocospath/cocos:$cocospath/external:$cocospath/cocos/prebuilt-mk:$cocospath/extensions”
replace with
arguments “NDK_MODULE_PATH=$COCOS_X_ROOT:$COCOS_X_ROOT/cocos:$COCOS_X_ROOT/external:$COCOS_X_ROOT/cocos/prebuilt-mk:$COCOS_X_ROOT/extensions”

proj.android-studio NF$ cocos compile -p android -m debug --android-studio

errors with

Android NDK: WARNING: Ignoring unknown import directory: /Users/NF/MobDev/Cocos/ProjectName/cocos2d
Android NDK: WARNING: Ignoring unknown import directory: /Users/NF/MobDev/Cocos/ProjectName/cocos2d/cocos
Android NDK: WARNING: Ignoring unknown import directory: /Users/NF/MobDev/Cocos/ProjectName/cocos2d/external
Android NDK: jni/Android.mk: Cannot find module with tag ‘.’ 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:
jni/Android.mk:26: *** Android NDK: Aborting. . Stop.

and so far I have not been able to fix. Any ideas pleas?


#49

There’s some problem with paths. Are you sure COCOS_X_ROOT is pointing to cocos2d-x directory? Can you use “cocospath” variable like I did?


#50

Actually to make it working with 3.14.x

Have a look on this setup:


#51

Hi @energyy, if you take a look above where my screenshots are you will see I am following this thread (as well as others). Cheers!


#52

@piotrros, ok did that, updated build.gradle, see below, but same error??

    ext {
             //cocospath = "/Users/piotr/Documents/pierdoly/cocos2d-x-3.14"
	cocospath = "/Users/NF/SDK/cocos2d-x-3.14.1"
    }

    externalNativeBuild {

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

#53

@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…


#54

You should have this:

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

not

$(call import-module,.)

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


#55

@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


#56

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


#57

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.


#58

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.