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

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

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.

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…

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)

5 Likes

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!

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)

3 Likes

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

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.

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

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

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?

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

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.

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.

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.

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?

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

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

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”.

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

1 Like