Can't compile android getting format not a string literal and no format arguments [-Werror=format-security]

This is more of a c/c++ question and hoping to get some suggestions from the community. I’m sure a lot of people soved this in different ways in their games.

I created macros for logging a warning/error/breadcrumb with Fabric.

My goal was to be able to use it like CCLOG, and format the string inline and have parts of it removed on release build, like CCLOG.

The macros are using cocos2d::StringUtils::format(...) method like so:

std::string formattedString = cocos2d::StringUtils::format(stringFormat,  ##__VA_ARGS__);

Calling my macros look something like this

KGWARNING("IAPManager | onFailure() | message: %s", message.c_str());

The problem is that on android I get the compile error:

Error:(60, 88) error: format not a string literal and no format arguments [-Werror=format-security]

On iOS I simply get a warning, but on android studio it is actually an error.

The following is my macro definitions, any better ideas on how to do this?

#define KGLOG(stringFormat, ...)\
do{\
std::string formattedString = cocos2d::StringUtils::format(stringFormat,  ##__VA_ARGS__);\
CCLOG("%p | %s", this, formattedString.c_str());\
}while(0);

#define KGBREADCRUMBONLY(stringFormat, ...)\
do{\
std::string formattedString = cocos2d::StringUtils::format(stringFormat,  ##__VA_ARGS__);\
KGFabric::setBreadcrumb(formattedString.c_str());\
}while(0)

#define KGBREADCRUMB(stringFormat, ...)\
do{\
KGLOG(stringFormat,  ##__VA_ARGS__);\
KGBREADCRUMBONLY(stringFormat,  ##__VA_ARGS__);\
}while(0);

#define KGWARNING(stringFormat, ...)\
do{\
KGWARNINGD(cocos2d::ValueMap(), stringFormat,  ##__VA_ARGS__);\
}while(0);

#define KGWARNINGD(customAttributes, stringFormat, ...)\
do{\
std::string formattedString = cocos2d::StringUtils::format(stringFormat,  ##__VA_ARGS__);\
std::string formattedString2 = cocos2d::StringUtils::format("[WARNING] %s", formattedString.c_str());\
KGBREADCRUMB(formattedString2.c_str())\
KGFabric::sendWarning(formattedString2.c_str(), customAttributes);\
}while(0);

I’ve seem a few posts about this already on this forum for that error. I’ve tried adding APP_CFLAGS += -Wno-error=format-security in my Application.mk as suggested but I still get the same compile errors.

Here is my complete Application.mk file.

APP_STL := gnustl_static

APP_CPPFLAGS := -frtti \
-DCC_ENABLE_CHIPMUNK_INTEGRATION=1 \
-std=c++11 \
-fsigned-char
APP_LDFLAGS := -latomic

APP_ABI := armeabi
APP_SHORT_COMMANDS := true


ifeq ($(NDK_DEBUG),1)
APP_CPPFLAGS += -DCOCOS2D_DEBUG=1
APP_OPTIM := debug
else
APP_CPPFLAGS += -DNDEBUG
APP_OPTIM := release
endif
APP_PLATFORM := android-9

APP_CFLAGS += -Wno-error=format-security

Don’t know why the flag doesn’t work. But if someone knows how to fix the flag issue or has a better way to achieve this, i’m curious to see how others solved this.