Can't include C++ headers like vector in Android NDK
It is possible. Here is some step by step:
In $PROJECT_DIR/jni/Application.mk:
APP_STL := stlport_static
I tried using stlport_shared, but no luck. Same with libstdc++.
In $PROJECT_DIR/jni/Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Nothing special here, but make sure your files are .cpp.
In $PROJECT_DIR/jni/hello-jni.cpp:
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <iostream>
#include <vector>
#define LOG_TAG "hellojni"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
// Comments omitted.
void
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
std::vector<std::string> vec;
// Go ahead and do some stuff with this vector of strings now.
}
#ifdef __cplusplus
}
#endif
The only thing that bite me here was #ifdef __cplusplus.
Watch the directories.
To compile, use ndk-build clean && ndk-build
.
If you are using Android studio and you are still seeing the message "error: vector: No such file or directory" (or other stl related errors) when you're compiling using ndk, then this might help you.
In your project, open the module's build.gradle file (not your project's build.grade, but the one that is for your module) and add 'stl "stlport_shared"' within the ndk element in defaultConfig.
For eg:
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.domain.app"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "myModuleName"
stl "stlport_shared"
}
}
}
I'm using Android Studio and as of 19th of January 2016 this did the trick for me. (This seems like something that changes every year or so)
Go to: app -> Gradle Scripts -> build.gradle (Module: app)
Then under model { ... android.ndk { ... and add a line: stl = "gnustl_shared"
Like this:
model {
...
android.ndk {
moduleName = "gl2jni"
cppFlags.add("-Werror")
ldLibs.addAll(["log", "GLESv2"])
stl = "gnustl_shared" // <-- this is the line that I added
}
...
}