Get the FULL version

Import STL libraries to the Android NDK code

Import STL libraries to the Android NDK code thumbnail

This is a quick tip for those who are beginning to write native Android code. As one may have noticed, it isn’t possible to use containers like, string, vector, list inside the NDK samples. These are all part of the STL (Standard Template Library), and are expected to be available when writing C++ code.

To add STL to your NDK code, locate the Application.mk file inside your project’s jni folder. If it isn’t there, create it. Please note that the Application.mk is not the Android.mk file! The Android.mk file instructs the compiler and the JNI on how NDK code should be handled. The Application.mk, works similarly as the Android manifest file for your NDK code, allowing the programmer to add permissions and define other applications’ properties, like such as ‘enabling’ the STL support.

After creating the Application.mk, add this line of code:

APP_STL := stlport_static

Now at the .c or .cpp file or at the header of the class where STL needs to be included, add the following:

//to use strings
#include <string>

//to use vectors
#include <vector>

//and so on...

/* add this line, to avoiding writing 'std::' every time a string (or any 
other container) is declared.*/
using namespace std;

This was tested using Android NDK, Revision 5b, released in January 2011. I don’t know if works on previous version of the NDK.

7 Comments to “Import STL libraries to the Android NDK code”

  1. Natan says:

    Wow this really helped me thanks A LOT!!!

  2. Animal456 says:

    Your instructions didnt work out well for me with NDK-r5c on windows.
    Do I need to copy the STL-libs into my project? And will the compiled source files work on Android Versions below 2.3 like on Android 1.6? I would be so glad if someone could help me out cuz after 2 days wasted on making it compile, I guess Im going nuts if I have to read “string is not a member of std” ever again…
    Anyway any help on this would be highly appreciated :)

  3. Phạm Quân says:

    I successfully define a STL variable in my program
    But… I can’t use iterator. can any of you help me?

    Thanks

  4. Dorien says:

    Same here, I can’t use a vector iterator.

  5. Dorien says:

    omg ok. I had to create a seperate application.mk file. Thanks!

  6. ufologist says:

    thank you very much…i am looking for it for a long time!

Leave a Comment

Post Comments RSS