Cocos2d-x Android Studio NDK build C++ DEBUG - WORKS!

Hi,

I was trying to setup Android studio project to work natively with C++ and seems other guys instructions are correct and it’s really works.

So far for Hello World project to try out you need to replace. build.gradle inside app folder with this code:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "org.cocos2dx.hellocpp"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        externalNativeBuild {
            ndkBuild {
                targets 'MyGame'
                arguments 'NDK_MODULE_PATH=../../../..:../../../../cocos:../../../../external'
                arguments '-j' + Runtime.runtime.availableProcessors()
                abiFilters 'armeabi-v7a'
        }
        }

        ndk {
            abiFilters 'armeabi-v7a'

        }
    }



    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        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

After try to run from Android Studio - to make project compile - after that configure

Run->Edit configuration - select your project -> go to tab debugger -> set debuger type hybrid and select proper project symbol directory, it will be under this path

proj.android-studio/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a

also dont forget if you compiling armeabi-v7a to add it to Application.mk in JNI file

Have a try and let me know if it worked or you need more detailed instructions if some step not working properly.

10 Likes

Yes, it does work.
Here’s my version with precompiled libs support: How to link prebuilt Cocos2D X C++ library to Android Studio project?

1 Like

This is great news

nice! well done!

I found it here and tried it immediately.
I get the following error, but does anyone know the cause?

make: *** [D:/cocos2dProjects/MyGame/proj.android-studio/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/libcocos2dxinternal.a] Error
87

    at com.android.build.gradle.tasks.ExternalNativeBuildTaskUtils.executeBuildProcessAndLogError(ExternalNativeBuildTaskUtils.java:233)
    at com.android.build.gradle.tasks.ExternalNativeBuildTask.executeProcessBatch(ExternalNativeBuildTask.java:231)
    at com.android.build.gradle.tasks.ExternalNativeBuildTask.build(ExternalNativeBuildTask.java:163)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)

I am very sorry for the sentence using the Google translation because it is Japanese and I am not good at English.
Thank you for your consideration.

Is that a full stack trace?

gradlew assembleDebug --stacktrace

↑This way, is it wrong?

I output the log in text, but I copied only the last one and pasted it because it is long.

I am sorry that I am unfamiliar with English.
Thank you.

You can add --debug as well. If output log is too long you should post it on pastebin for example.

It works!!! It helps me a lot!! I can debug now. Thanks a lot!

Warning:(499) warning: ignoring old commands for target `/Users/admin/Desktop/HelloCpp/proj.android-studio/app/build/intermediates/ndkBuild/release/obj/local/armeabi-v7a/objs/bullet_static/BulletMultiThreaded/SpuLibspe2Support.o’
Error:Execution failed for task ‘:HelloCpp:externalNativeBuildDebug’.

Unexpected native build target MyGame. Valid values are: extension, audioengine, cpufeatures, box2d, cocos2dandroid, spine, network, cocos2dcpp, flatbuffers, ui, recast, cocos2dxinternal, cocos3d, cocosbuilder, bullet, cocostudio, cocosdenshion``

It don’t work. How do I set? thanks!

I think this is issue, you probably using older cocos2d-x version where compiled lib was called “cocos2dcpp” not “MyGame”

Upgrade your cocos;) to 3.13.1+ or change in gradle that line

targets 'MyGame'
to

targets 'cocos2dcpp'

Target name should match with in Android.mk defined (without _shared)

LOCAL_MODULE := cocos2dcpp_shared

1 Like

nice! It works!! Thanks!!!:thumbsup::thumbsup:

@energyy, appreciate your work. 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!

I have it all compiling, running. Have set the debug type to “Native” and set the symbols directory and yet when I press “debug” it does not hit any breakpoints at all…

Is really frustrating please help … here is my setup:

So I have done: edit configurations -> select app “HelloCpp” – Debug tab - > Native -> symbols directory:

  "C:\dev\firebase\cocos2dx-cpp-sample\sample_project\proj.android-studio\app\.externalNativeBuild\ndkBuild\debug\armeabi-v7a"

In build.gradle for the app I have:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

defaultConfig {
    applicationId "org.cocos2dx.hellocpp"
    minSdkVersion 21
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"

    externalNativeBuild {
        ndkBuild {
            arguments '-j' + Runtime.runtime.availableProcessors()
        }
    }

    ndk {
        abiFilters 'x86','armeabi-v7a'
    }
}

externalNativeBuild {
    ndkBuild {
        path 'jni/Android.mk'
    }
}

sourceSets.main {
    java.srcDir "src"
    res.srcDir "res"
    jniLibs.srcDir "libs"
    manifest.srcFile "AndroidManifest.xml"
    assets.srcDir "assets"
}

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')
    compile 'com.google.firebase:firebase-auth:10.2.0'
    compile 'com.google.android.gms:play-services-base:10.2.0'
}

apply plugin: 'com.google.gms.google-services'

task cleanAssets(type: Delete) {
    delete 'assets'
}
task copyAssets(type: Copy) {
    from '../../Resources'
    into 'assets'
}

clean.dependsOn cleanAssets
preBuild.dependsOn copyAssets

I solved this by upgrading to 3.15 and downgrading the NDK to 13.b. Only took most of the day to figure it out… the joys of open source. :thumbsdown:

Break points are not caught. No compilation error, successful, working apk is generated. Any ideas?

have u used 3.15 version?
now it should work more stable.

Unable to create prebuilt libraries usin 3.15.1.

cocos gen-libs -p android --app-abi x86

NDK build mode: release
running: ‘“C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r11c\ndk-build” -C C:\cocos2d-x-3.15.1\tools\simulator\frameworks\runtime-src\proj.android -j4 APP_ABI=“armeabi” NDK_MODULE_PATH=C:\cocos2d-x-3.15.1;C:\cocos2d-x-3.15.1\cocos;C:\cocos2d-x-3.15.1\external;C:\cocos2d-x-3.15.1\cocos\scripting NDK_TOOLCHAIN_VERSION=4.9’

Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 9 in ./AndroidManifest.xml
Android NDK: WARNING: Ignoring unknown import directory: C:\cocos2d-x-3.15.1
Android NDK: C:\cocos2d-x-3.15.1\cocos/ui/Android.mk: Cannot find module with tag ‘extensions’ 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:
make: Entering directory C:/cocos2d-x-3.15.1/tools/simulator/frameworks/runtime-src/proj.android' C:\cocos2d-x-3.15.1\cocos/./Android.mk:329: *** Android NDK: Aborting. . Stop. make: Leaving directoryC:/cocos2d-x-3.15.1/tools/simulator/frameworks/runtime-src/proj.android’
Error running command, return code: 2.
Error running command, return code: 14.

I already use 3.15.1

1 Like

For those who are still having trouble with debugging, setting Run->Edit configuration - select your project -> go to tab debugger -> set debuger type to native works for me.