Ustl or Stlport for Android

uSTL or STLPort for Android?

STLport supported since Android2.3 now!!!

STL Vector, deque or containers equivalent for Android?

You can use STLPort (http://www.stlport.org/), which contains all the STL classes and has been ported to Android. Please have a look at answer of another question at uSTL or STLPort for Android?

How to use the boost library (including shared_ptr) with the Android NDK and STLport

It turned out that this approach does not entirely work when compiling a debuggable library. The release library is compiled with -O2 which optimizes out some infelicities, but the debug library is done with -O0 which reveals some additional problems. Furthermore, I wasn't too happy about having to edit the boost files. So with some additional study, I've come up with the following solution.

First, don't edit any of the boost files. Instead add the following to the header within the std namespace:

struct bad_cast : public exception {bad_cast operator()(){}};

Next add the following to the source file:

namespace boost
{
void throw_exception(std::exception const&) {}
}

This now compiles and links into the application even with android:debuggable="true" in AndroidManifest.xml. It doesn't run in the emulator, but then it wasn't doing that before I included this library either.

Using the STL with Android NDK C++

It seems that the mistake was a somehow broken NDK. I reinstalled it (delete, unzip) and now it works.

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.

Do the Android NDK toolchain binaries build with or without exceptions by default?

If you are using the provided ndk-build system to build your apps (which you probably are) then exceptions are disabled by default.

STANDALONE-TOOLCHAIN.html only applies if you're using the compiler directly with your own build system, rather than using ndk-build. Here's what it says:

It is now possible to use the toolchains provided with the Android NDK
as standalone compilers. This can be useful if you already have your
own build system, and only need to ability to invoke the
cross-compiler to add support to Android for it.



Related Topics



Leave a reply



Submit