Search code examples
androidc++android-ndkjava-native-interface

Return vector<string> from c++ to Java


I am trying to learn android ndk. Below is my Java method :

public static native Vector<String>     GetData(String input) ;

'input'(input parameter) will go to the c++ native method which will return a vector. How do I implement the c++ side of the method?


Solution

  • 1) It would be easier to use an array and not a vector:

        public static native String[] getData(String input);
    

    2) Lets assume your Java package name is com.example.jeff and you have a class named MyCppFacade that contains getData(). You need to create a C++ file that contains this function:

        extern "C" jobjectArray Java_com_example_jeff_MyCppFacade_getData(
        JNIEnv* env, jobject obj, jstring input)
        {
             jobjectArray result;
             ...
             return result;
        }
    

    3) To create your string array you could look at Return a String array on a JNI method.