4

I am developing an android app using C++ code. Trying to use JNI but failed. The code in myFirstApp.cpp

JNIEXPORT jint JNICALL Java_com_example_myfirstapp_CreateApp_findMarkersNative(
        JNIEnv* env, jobject, jlong addrRgba) {
    //clock_t begin = clock();
    Mat& mRgb = *(Mat*) addrRgba;
    Mat mgray(mRgb.rows, mRgb.cols, CV_8UC1);
    cvtColor(mRgb, mgray, CV_RGBA2GRAY, 1); // the working one

    clearVectors();

    findSquares(mgray);
    mergeRectangles();

    processFilteredSquares(mgray);

    drawFilteredSquaresWithoutMarker(mRgb);
    __android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "Candidates %i",candidates.size());
    return clusterBoundaries.size();
//  clock_t end = clock();

//  mgray.release();
}

In the android activity(CreateApp), I've declared the method

public native int findMarkersNative(long imgAdd);

The package name in the activity is

package com.example.myfirstapp;

The error appearing the logcat

Caused by: java.lang.UnsatisfiedLinkError: Native method not found com.example.myfirstapp.CreateApp.findMarkersNative
omarsafwany
  • 3,695
  • 8
  • 44
  • 75

1 Answers1

1

Your definitions seem correct. According to several similar posts, it may be because of C / C++ name mangling. Try surrounding your methods, around where this API is defined with

extern "C" { }

for example:

extern "C" {
JNIEXPORT jint JNICALL Java_com_example_myfirstapp_CreateApp_findMarkersNative(JNIEnv* env, jobject, jlong addrRgba) 
{
... function code ...
}
}

Source: Unsatisfied link error

Community
  • 1
  • 1
ılǝ
  • 3,440
  • 2
  • 33
  • 47
  • Is this the complete cpp code in your library? I have to admit I am no expert on jni, but if this is the case, it seems you are missing some loading and init routines. Most working examples that I have start with "jint JNI_OnLoad", get_native_data, after which is the extern "C" and all the methods. Unless somebody else comes around with a better advice, I can only recommend going through the Android "getting starting" resources and tips pages, eg. http://developer.android.com/training/articles/perf-jni.html#faq_ULE – ılǝ May 07 '13 at 01:56
  • That's not all of the code. I attached the part where the problem seems to occur. – omarsafwany May 07 '13 at 02:07
  • There should not be curly braces after extern "C", just add it before JNIEXPORT without anything else. – gregko Jul 25 '13 at 02:50