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

I am using Cocos2D 3.13.1.

Same here. Iā€™m very interested too in using prebuilt libs with Android Studio.

So, I just created project:

cocos new TestLibs -p com.test.libs -l cpp -t binary

And there is no proj.android-studio but only proj.android folder. How properly import it into AS?
Try to summon @piotrros here - are you using AS with latest cocos2d-x and prebuilt libs?

Nah, but Iā€™m still trying:

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