3

I'd started porting some Java code in native c++ in Android. I have an issue with using strings in c++:

Type 'std::string' could not be resolved

There is my sample code

#include <jni.h>
#include <lexu_me_test_native.h>
#include <string.h>
using namespace std;

JNIEXPORT jstring JNICALL Java_lexu_me_test_native_prepairToShowNative
  (JNIEnv * env, jclass javaThis, jstring str)
{
    jboolean blnIsCopy;
    jstring jstrOutput;
    char* strCOut;
    std::string ss;

    const char* strCIn = (env)->GetStringUTFChars(str , &blnIsCopy);
    // convert jstring to a char array
    // Do stuff with the char array and and store the result
    // in another char array strCOut
    (env)->ReleaseStringUTFChars(str , strCIn); // release jstring

    jstrOutput = (env)->NewStringUTF(strCOut); // convert char array to jstring
    return jstrOutput;
}

Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := native
LOCAL_SRC_FILES := native.cpp

include $(BUILD_SHARED_LIBRARY)

Application.mk file:

APP_STL := stlport_static

MinGW installed and added to path. I tried using android-ndk-r8e and android-ndk-r8-crystax-1 nothing helped. In Cygwin Terminal errors:

Compile++ thumb  : native <= native.cpp
jni/native.cpp: In function '_jstring* Java_lexu_me_test_native_prepairToShowNative(JNIEnv*, jclass, jstring)':
jni/native.cpp:11:2: error: 'string' was not declared in this scope
jni/native.cpp:11:9: error: expected ';' before 'ss'

I'm using Win 7 64bit. Can anyone say how it could be solved? Thanks.

EDIT.

In C/C++ General - Path and Symbols already set: C:\Android\android-ndk-r8e\platforms\android-14\arch-arm\usr\include

IInspectable
  • 35,521
  • 8
  • 69
  • 148
LEX
  • 564
  • 1
  • 6
  • 11

2 Answers2

1

If the other answers didn't work, then try these steps:

  1. If you have using namespace std;, use string instead of std::string.
  2. If #include <string> doesn't work and you're using Linux, try #include <unistd.h>. If you're using another OS, use #include <cstdlib>.
Ananas
  • 113
  • 4
Shervin Sorouri
  • 200
  • 1
  • 8
  • Hi, I'm changed std::string to string, and added #include . But Eclipse show errors "Type 'string' could not be resolved" although in cygwin compile fine. – LEX Sep 17 '13 at 09:03
  • Then eclipse has a problem but i suggest you try `#include ` for windows or `#include ` for linux instead of `#include ` – Shervin Sorouri Sep 17 '13 at 09:07
  • #include Eclipse accepted it, but warning didn't hide and compile in cygwing show error "'string' was not declared in this scope" – LEX Sep 17 '13 at 09:14
  • now i am sure that your computer doesn't have the string header or your ide(Eclipse) is broken. – Shervin Sorouri Sep 17 '13 at 09:17
  • i recommend visual studio if your a windows user because its really easy to work with and its free. go check it at [Microsoft's website](http://www.microsoft.com/visualstudio/eng/downloads‎) – Shervin Sorouri Sep 17 '13 at 09:20
  • @shervin Visual Studio and Android's NDK aren't exactly best friends. It is possible to integrate the NDK into Visual Studio. However, [vs-android](http://code.google.com/p/vs-android/) is flaky, at best. And [VisualGDB](http://visualgdb.com/) is a commercial product that requires a commercial Visual Studio Edition. – IInspectable Sep 17 '13 at 09:44
  • *"But Eclipse show errors "Type 'string' could not be resolved" although in cygwin compile fine..."* - Also see [Android NDK build, Method could not be resolved](http://stackoverflow.com/q/23155676). Eclipse and the plugin broke during a NDK update and it was never fixed. It was never fixed presumably because of Android Studio, which does not support JNI... – jww Jun 07 '15 at 10:56
-1

Check your Android.mk and Application.mk files. Ensure you select an STL library and include int the Application.mk file:

Application.mk: APP_STL := gnustl_static

(gnustl_static) can be replaced with the STL version you want, and STLPort is still an option

See http://www.kandroid.org/ndk/docs/CPLUSPLUS-SUPPORT.html

jww
  • 83,594
  • 69
  • 338
  • 732
Joe Plante
  • 5,952
  • 2
  • 27
  • 23
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Parag Bafna Feb 25 '15 at 12:16
  • Wow was this a long time ago. I'll update the answer. However, see http://www.kandroid.org/ndk/docs/CPLUSPLUS-SUPPORT.html – Joe Plante Feb 26 '15 at 00:12