Creating arraylist using native Android code

If this is a double question, just let me know, don't do downvote, I'm new to Android development. I want to create an ArrayList using native code for Android, I tried the following:

#include <jni.h>
#include <android/log.h>
#include <vector>

template<class T>

extern "C"{  //here it is showing **error expected unqualified-id before string constant**

std::vector<T> list;

JNIEXPORT void JNICALL Java_com_example_nativetestapp_NativeList_add(
    JNIEnv * env, jobject obj, T t) {
list.push_back(t);
 }

JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_remove(
    JNIEnv * env, jobject obj, int pos) {
if (pos > list.size() - 1 || pos < 0)
    return false;
return list.erase(list.begin() + pos) != NULL ? true : false;
 }

JNIEXPORT jint JNICALL Java_com_example_nativetestapp_NativeList_size(
    JNIEnv * env, jobject obj) {
return list.size() == NULL ? 0 : list.size();
}

JNIEXPORT jint JNICALL Java_com_example_nativetestapp_NativeList_get(
    JNIEnv * env, jobject obj, int pos) {
return list[pos];
 }

JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_contains(
    JNIEnv * env, jobject obj, T t) {
for (int var = 0; var < list.size(); var++) {
    if(t==list[var])
        return true;
 }
return false;
}

JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_remove(
    JNIEnv * env, jobject obj, T t) {
for (int var = 0; var < list.size(); var++) {
        if(t==list[var]){
            list.erase(list.begin()+var);
            return true;
        }
    }
    return false;
}
};

But I am stuck in the error mentioned in the code. If I put a semicolon after

template<class T>;

then after this line I get a new error:

cannot resolve T.

+4
source share
1 answer

add extern "C"in front of each JNIEXPORT, and not wrap all the codes.

+1
source

All Articles