Nagaraju V
Nagaraju V

Reputation: 2757

Creating arraylist by using Android native code

If it is a duplicate question just let me know, don't downvote, I am new to Android-native development. I want to create an ArrayList by using Android-native code, 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 with the error mentioned in the code. If I place a semicolon after

template<class T>;

then after this line I get a new error:

cannot resolve T symbol.

Upvotes: 2

Views: 202

Answers (1)

Jiang YD
Jiang YD

Reputation: 3311

add extern "C" before every JNIEXPORT, rather than wrap all codes.

Upvotes: 1

Related Questions