Cocos 2dx 4.x. Enable C++17 in Android Studio

I am trying to learn Cocos 2dx game engine. I generated a simple project with this command:

cocos new -l cpp -p com.testgame1 -d path_to_dir testgame1

Next, I try to build an android project. Everything is successful. Then I wrote a lot of code that uses C++ standard 14, 17. Example (file main.cpp):

void cocos_android_app_init(JNIEnv* env) {
    std::string vec;
    std::transform(std::begin(vec), std::end(vec), std::begin(vec), [](auto& elem)
                       return elem;
    appDelegate.reset(new AppDelegate());

Here I using auto in lambda function (standart C++ 14).
I enable support for the standard in the usual way for Android Studio in build.gradle:

defaultConfig {
        applicationId "com.testgame1"
        minSdkVersion PROP_MIN_SDK_VERSION
        targetSdkVersion PROP_TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0"

        externalNativeBuild {
            cmake {
                targets 'MyGame'
                cppFlags "-std=c++17 -frtti -fexceptions -fsigned-char"
                arguments "-DCMAKE_FIND_ROOT_PATH=", "-DANDROID_STL=c++_static", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_NEON=TRUE"

        ndk {
            abiFilters = []
            abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String})

But it doesn’t have any effect. On a clean project (no Cocos 2dx game engine) everything works flawlessly.
I am getting an error in Android Studio:

..\..\..\..\jni\hellocpp\main.cpp:42:72: error: 'auto' not allowed in lambda parameter

NDK: 21.4.7075529

How to fix it?

Try this:

In your game project folder, open up CMakeLists.txt, and add the following after the include(CocosBuildSet) statement:


If you want to apply C++17 to the cocos2d engine code as well, then adding this may work:

set_target_properties(cocos2d PROPERTIES
1 Like

Thank you. It also worked for linux.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.