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

#6

Xmm, so is it working now that you can compile and debug in Android studio c++ code for android? or you trying cmake stuff?


#7

I’m using ndkBuild. Yes - I can debug. Now (see topic above) I finally got it working with precompiled libs. Compilation is then much faster and I can still debug.
One import thing to get debugging working:
for releasing app you only need:
abiFilters “armeabi”
as adding more abis are increasing apk size and compilation time.
But, in my case, I own Nexus 6P with arm64-v8a so I have to add it to abiFilters in order to get debugging working.
I’m saying about c++ debugging, because java debugging will work anyway.


#8

Any how-to step by step for newbies? :slight_smile:

I’ve just created a new project using:

cocos new ProjectName -p projectname.com.name -l cpp -t binary

Configured it for build with prebuilt libs for iOS in Xcode. I even have it with source code, because I added path’s for cocos2d sources for header search path option.It compiles with prebuilt libs in no time and at the same time I have debug with source code of cocos2d. Cool.

Now, what should I do to set up it for Android Studio? There even no project folder created for it…


aRRGH! Android studio ... linker error
#9

Make sure you have cocos2d-x 3.14 and NDK r13b properly installed and paths configured.

cocos -v
cocos2d-x-3.14
Cocos Console 2.2

  1. make precompiled libs:

cocos gen-libs -p android --ap android-10
if needed:
cocos gen-libs -p ios
cocos gen-libs -p mac

  1. create new project and don’t use -t binary:

cocos new ProjectName -l cpp

  1. remove cocos2d directory (you don’t need that):

cd ProjectName
rm -rf cocos2d

  1. update gradle version in build.gradle file (top directory):

classpath ‘com.android.tools.build:gradle:2.2.3’

  1. change libcocos2dx subproject path in settings.gradle (top directory):

project(’:libcocos2dx’).projectDir = new File(’/YOUR_PATH/cocos2d-x-3.14/cocos/platform/android/libcocos2dx’)

  1. edit project.properties file (app directory):

target=android-10

  1. update build.gradle file (app directory):

    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 = "/YOUR_PATH/cocos2d-x-3.14"
         }
    
     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

  2. change gradle wrapper version in gradle/wrapper/gradle-wrapper.properties like this:

distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip

  1. In app/jni/Android.mk file remove these:

$(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)

Now open proj.android-studio in Android Studio and enjoy.
If you want to debug c++ make sure you’ve added to abiFilters architecture, which your device is working on (for example arm64-v8a is needed for Nexus 6P). To find .cpp files expand ProjectName -> cpp -> Classes.
I’ve also attached example blank project, which I’ve created to write this post (and it’s working).

ProjectName.zip (6.0 MB)


#10

Thanks a lot for this. It just saved me a few hours. Also, I had to remove this because the cocos compile returned some weird error that the NDK is not set ¯_(ツ)_/¯. Thanks again!


#11

Ok, it wasn’t working as expected. Whole cocos library was compiled instead. One small change is required:
$(call import-module,./prebuilt-mk)
instead of
$(call import-module,.)
in Android.mk file.
New test project included.
When you open this project in android studio you’ll see the difference. There’s no more bunch of libraries in ProjectName/cpp, just MyGame and cpufeatures (dunno why but it’s here).
I’m also switching from armeabi to armeabi-v7a, because non armeabi-v7a are very old and I don’t support them anyway (too old android version for libraries like google play services and facebook).
Now app is compiling for about 18 seconds (about 3mins before) in my case :slight_smile:
Also I can debug on arm64-v8a devices with just armeabi-v7a (don’t have to add arm64-v8a in build.gradle which will increase apk size and compilation time).
ProjectName.zip (5.8 MB)


Debug C++ code from Android Studio
Develop cocos2dx projects on Android Studio
Android Studio Setup
PreBuilt Libraries, Android Studio & NDK_MODULE_PATH
Debugging in Android Studio?
Cocos2d-x Android Studio NDK build C++ DEBUG - WORKS!
Android Studio support
#12

I just get an error “Error: Configuration with name ‘default’ not found.” help :slight_smile:


#13

Did you use zip from the last post? Make sure you use latest android ndk and your android.mk file has all necessary .cpp files listed.


#14

Should we download latest android NDK standalone or we should use one that is downloaded by SDK manager?


#15

Both should work, but I’m using the one which is downloaded do SDK manager.


#16

if i use SDK manager downloaded NDK, should i provide NDK path in bash_profile? and from where i will get the path to this NDK?


#17

Yes, you should put something like this: export NDK_ROOT=/your_path_to_sdk/ndk-bundle in .bash_profile


#18

For me NDK bundle and ANDROID_NDK_HOME are both set to “C:\dev\android-ndk-r13b-windows-x86_64\android-ndk-r13b” and compile works but on Nexux7 debug doesn’t.


#19

Debugging depends on ABI your device have. For example in my Nexus 6P it’s arm64-v8, but it works also with armeabi-v7a. However I won’t be able to debug while using armeabi only. Nexus 7 should work with armeabi-v7a.


#20

it gives error while syncing gradle

Error:(22, 0) Could not find method targets() for arguments [MyGame] on object of type com.android.build.gradle.internal.dsl.NdkBuildOptions.


#21

Strange, did you ran test project I posted above? What version of NDK, Android Studio are you using? Did you download cocos2d-x with that pull request merged? Did you build precompiled libs?


#22

android studio 2.2.3
NDK 13.1.334577 (using android studio SDK manager)
Yes I cloned from Git then download-deps, submodule update and build precompiled libs

Your project gives error in setting.gradle


#23

This is a gradle script error and I have to say it’s very strange.


#24

I just back to this. Just ported my game to C++ and so I have some stable build for iOS. Now I want to build it for android. But now I’m getting “The android command is no longer available.” while trying to execute command: “cocos gen-libs -p android”.


#25

That has nothing to do with gen-libs. Check out these issues: https://github.com/cocos2d/cocos2d-x/issues/17424 and https://github.com/cocos2d/cocos2d-console/issues/404