Switch Android APP_STL default to c++_static

Hey all.

Just in-case you didn’t read this blog post. The Android NDK Team is now recommending people to switch to libc++ Which means soon enough, The default for cocos should be c++_shared orc++_static

Apperently by r17, libc++ will be the default

libc++ is out of beta and is now the preferred STL in the NDK. Starting in r17, libc++ is the default STL for CMake and standalone toolchains. If you manually selected a different STL, we strongly encourage you to move to libc++

I think cocos should switch over to libc++ whe r16 comes out fully. I tested a fresh clone from github and it works with r16beta-1. There is just on thing you have to change in the ndk which they already patched for r16beta-2

Issue and Fix


https://android-review.googlesource.com/#/c/platform/ndk/+/479579/1/sources/android/support/include/stdio.h

Links:


2 Likes

Is libc++ stable now? We changed to use libc++ in some version, but meet some strange issues, and in that time, libc++ release note says it is not stable.

But aren’t it’s a default for Xcode 8 even all time? I know not enough about all this, but just saw.

1

If you make a new, you usually runs fine. My game which I’ve been working on for a while now works on libc++. Only thing is to make sure external sdks like SDKBOX update their libs

I’m not saying to update it now, but the NDK team is saying when they release r16, libc++ will be the preferred and by r17, it will be default

I think cocos2d-x should look into making libc++ the default when r16 releases.

Yea iOS has always used clang and libc++. I don’t understand why it took so long for Android NDK to get off gnustl. It is missing may of the stl functions that people would usually use

I had to implement my own to_string while using gnustl because it didn’t have it :confused:

Yep, we should keep an eye on it.

So NDK 16 released. I just compiled a new cocos project from scratch using the latest GitHub commit and it builds.

I think making the default for the future of cocos ndk 16, c++_static or c++_shared and clang is the best option

2 Likes

But prebuilt libs now working for me. Did you test it also? iOS and macOS works ok, but android has many problem.s

I haven’t tested it, but since generating the pre-built libs means that it will be compiling the same source, I can assume that it should

I’ll try it out and get back to you.

I never tried source code for android, I don’t know how) and it will compile forever every time…
Currently, I can compile prebuilt with NDK 16 with:

APP_STL := gnustl_static

My minimum target is api 19:

cocos gen-libs -p android --ap android-19 --app-abi x86:armeabi-v7a

What do you mean it takes a while?

Once you compile once, it shouldn’t compile cocos2d-x again unless you change something in the engine or change things in the Application.mk file.

Also, the current limitation with cocos gen-libs is that you need to use the old build system ant. This is what I get when I try

Mozart-Ari:~ mozartlouis$ cocos gen-libs -p android --ap android-19 --app-abi x86:armeabi-v7a
Python 2.7.10
running: '/Users/mozartlouis/Code/Libraries/Cocos2d-x/tools/cocos2d-console/bin/cocos compile -s /Users/mozartlouis/Code/Libraries/Cocos2d-x/tools/simulator -p android --ndk-mode release --app-abi x86:armeabi-v7a --ap android-19'

Python 2.7.10
Building mode: debug
Using Eclipse project : /Users/mozartlouis/Code/Libraries/Cocos2d-x/tools/simulator/frameworks/runtime-src/proj.android
Ant (Eclipse project) is not supported from Android SDK Tools 25.3.0.
Error running command, return code: 101.

Try to compile using NDK 16 with

APP_STL := c++_static and use clang for this.

I currently use gradle for build in android studio, which in my opinion is the better option than using cocos commane line tools

Here’s an exampile of my gradle

build.gradle

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
    buildToolsVersion "27.0.1"

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

        externalNativeBuild {
            ndkBuild {
                if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                    // skip the NDK Build step if PROP_NDK_MODE is none
                    targets 'game'
                    arguments 'NDK_TOOLCHAIN_VERSION=clang'
                    arguments 'APP_PLATFORM=android-' + PROP_APP_PLATFORM
                    arguments '-j' + Runtime.runtime.availableProcessors()
                    abiFilters.addAll(PROP_APP_ABI.split(':').collect { it as String })
                }
            }
        }
    }

    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        manifest.srcFile "AndroidManifest.xml"
        assets.srcDir "../../Resources"
    }

    externalNativeBuild {
        ndkBuild {
            if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                // skip the NDK Build step if PROP_NDK_MODE is none
                path "jni/Android.mk"
            }
        }
    }

    signingConfigs {
        dev {
            if (project.hasProperty("STORE_FILE")) {
                storeFile file(STORE_FILE)
                storePassword STORE_PASSWORD
                keyAlias ALIAS_DEV
                keyPassword ALIAS_DEV_PASSWORD
                v2SigningEnabled true
            }
        }

        release {
            if (project.hasProperty("STORE_FILE")) {
                storeFile file(STORE_FILE)
                storePassword STORE_PASSWORD
                keyAlias ALIAS_RELEASE
                keyPassword ALIAS_RELEASE_PASSWORD
                v2SigningEnabled true
            }
        }
    }

    buildTypes {
        debug {
            debuggable true
            jniDebuggable true
            renderscriptDebuggable true
            minifyEnabled false
            shrinkResources false

            if (project.hasProperty("STORE_FILE")) {
                signingConfig signingConfigs.dev
            }

            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=1'
                }
            }
        }

        release {
            debuggable false
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            if (project.hasProperty("STORE_FILE")) {
                signingConfig signingConfigs.release
            }

            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=0'
                }
            }
        }
    }
}
android.applicationVariants.all { variant ->
    // delete previous files first
    delete "${buildDir}/intermediates/assets/${variant.dirName}"

    variant.mergeAssets.doLast {
        copy {
            from "${buildDir}/../../Resources"
            into "${buildDir}/intermediates/assets/${variant.dirName}"
            exclude "**/*.gz"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':libcocos2dx')
}

gradle.properties

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled zprojects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

PROP_COMPILE_SDK_VERSION=19
PROP_APP_PLATFORM=19
PROP_MIN_SDK_VERSION=19
PROP_TARGET_SDK_VERSION=27
PROP_APP_ABI=armeabi-v7a:arm64-v8a:x86

android.library.reference.1=../cocos2d/cocos/platform/android/java/libs/gps/

Seems to be, for example when clean project it recompiles. I just want prebuilt to save that time. And I want tot use one lib folder for all games.

Thats in ideal world… well, I like prebuilt.

My config is here Pre-built libs for android not compiles with NDK 14+

That for using prebuilt libs.

android is very new to me… I can say that I just create prebuilt via console and then built my game via android studio after that and run on emulator.

You need to replace tool folder to older it’s common issue.

UPD:

But with:

modified Cocos2d-x/tools/simulator/frameworks/runtime-src/proj.android/jni/Application.mk
APP_STL := c++_static

I got:

/Volumes/MacData/Cocos2d-x/external/spidermonkey/prebuilt/android/x86/libjs_static.a(jscntxt.o)(.text+0x1178): error: undefined reference to 'vtable for std::basic_filebuf<char, std::char_traits<char> >'
/Volumes/MacData/Android/SDK/ndk-bundle/toolchains/x86-4.9/prebuilt/darwin-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin/ld: the vtable symbol may be undefined because the class is missing its key function
/Volumes/MacData/Cocos2d-x/external/spidermonkey/prebuilt/android/x86/libjs_static.a(jscntxt.o)(.text+0x11a3): error: undefined reference to 'std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)'
/Volumes/MacData/Cocos2d-x/external/spidermonkey/prebuilt/android/x86/libjs_static.a(jscntxt.o)(.text+0x11fe): error: undefined reference to 'std::string::find(char const*, unsigned int, unsigned int) const'
/Volumes/MacData/Cocos2d-x/external/spidermonkey/prebuilt/android/x86/libjs_static.a(jscntxt.o)(.text+0x127b): error: undefined reference to 'std::__throw_bad_cast()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/armeabi-v7a/libcocos2dlua.so] Error 1
make: *** Waiting for unfinished jobs....
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/x86/libcocos2dlua.so] Error 1
make: Leaving directory `/Volumes/MacData/Cocos2d-x/tools/simulator/frameworks/runtime-src/proj.android'
Error running command, return code: 2.
Error running command, return code: 14.

I don’t know, if it’s possible even to build libs via android studio?
I just copied that config from some old topic and some guys helped me with that. Too bad, that there is nobody making proper tutorial about that and documentation is abandoned and old for prebuilt libs and other…

Hmmm I see. I definitely understand the benefits of using prebuilt libs. I guess it’s just that no one has tried to standardize and modernize it for cocos2d-x.

Maybe we should work together to do that. I am currently an Android developer so I know my way around the ecosystem pretty well.

We should migrate from Ant to Gradle first and get pre-built libraries working with Android Studio

@slackmoehrle is that something we can do since it is kind of a big part of the dev process for android or is that something that we should leave up to the cocos developers?

1 Like

Thats good for you :slight_smile: but me, as iOS only dev is nothing understandable… I just know that some current work in some way for my game and I can run it on android. But if it best, or stable I don’t know…
Also, c++_static should be used because gnustl_static will be abandoned soon. However, it’s nobody starting to use and test it for now…

I think we are going to recommend not using the pre-built libraries on Android because there are so many factors that play into how they are compiled and and what SDK version, etc. on IOS and Windows pre-builts are great. very simple, no frustration. Android not so much. Let me talk to the team and get their thoughts.

I’m forked the GitHub repo and I’m going to make a pull request for it. I have it working! I’ll update this thread when uploaded

1 Like
2 Likes

Cool. Can you please also advise maybe how to use it with prebuilt libs?
Ideally I’m looking for a sample project.

Actually I’m using it without problems, for last 3-4 month. Main problems arrive when I’m trying to use latest NDK and static and documentation abandoned, without examples.

Prebuilt libs sholud be n1 method of using cocos2d-x. It will save time and space, also update of cocos game project is amazingly simple.(you don’t need to change anything in project, just recompile libs once and it will work for all games you develop, because cocos is shared between).