Search code examples
c++dictionaryandroid-ndkjava-native-interfacelist-initialization

C++ / JNI brace-enclosed initializer Map (Android NDK)


I have an engine write in C++ to integrate with JNI in AndroidStudio. I read & follow all tutorials I found.

In the Cpp file there is header's import which include 2 maps brace-enclosed initialized like that (just example):

The first one is initiazed with 2500 lines... The second one is like that :

std::map <StateEnum, std::string> StateToString = {
  { state_one, "State 1" },
  { state_two, "State 2" },
  { state_three, "State 3" },
  { state_four, "State 4" }
};

Application.mk

APP_ABI     := all
APP_STL     := stlport_static
APP_CFLAGS  := -std=c++11 -fPIC

And here is the error :

jni/My_header.h:line: error: could not convert '{{state_one, "State 1"}, {state_two, "State 2"}..} from '< brace-enclosed initializer list>' to 'std::map< StateEnum, std::string>'

I tried also to compile c++ file to library then integrate it to my Android project. And the result is same.

Anyone can help me. I didn't want to translate 2500 lines of map initialization (with map.add(...)) into 5000 lines.


Solution

  • STLport implementation is out-of-date and don't support C++11 (in particular, brace initializers). You should switch to GNU libstdc++ or LLVM libc++ implementation to get it working:

    APP_STL := gnustl_static # GNU libstdc++
    # Or:
    APP_STL := c++_static    # LLVM libc++