2

this is where i get error.I loaded correctly opencv library but i get this error.If i go in the ximgproc all native methods are red marked with "Cannot resolve corresponding jni function name_function".How can i resolve?

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            //Log.i("prova",uri.toString());
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
           // imageView.setImageBitmap(bitmap);
            Mat g=new Mat(bitmap.getHeight(),bitmap.getWidth(), CvType.CV_8UC1);
            Utils.bitmapToMat(bitmap,g,true);
            SuperpixelSLIC x=Ximgproc.createSuperpixelSLIC(g,Ximgproc.SLIC,100,3);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

and this is the error that i get:

06-07 19:24:01.370 21090-21090/com.example.jt1995.provaemo E/art: No implementation found for long org.opencv.ximgproc.Ximgproc.createSuperpixelSLIC_0(long, int, int, float) (tried Java_org_opencv_ximgproc_Ximgproc_createSuperpixelSLIC_10 and Java_org_opencv_ximgproc_Ximgproc_createSuperpixelSLIC_10__JIIF)
06-07 19:24:01.370 21090-21090/com.example.jt1995.provaemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.jt1995.provaemo, PID: 21090
                                                                             java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.ximgproc.Ximgproc.createSuperpixelSLIC_0(long, int, int, float) (tried Java_org_opencv_ximgproc_Ximgproc_createSuperpixelSLIC_10 and Java_org_opencv_ximgproc_Ximgproc_createSuperpixelSLIC_10__JIIF)
                                                                                 at org.opencv.ximgproc.Ximgproc.createSuperpixelSLIC_0(Native Method)
                                                                                 at org.opencv.ximgproc.Ximgproc.createSuperpixelSLIC(Ximgproc.java:452)
                                                                                 at com.example.jt1995.provaemo.MainActivity.onActivityResult(MainActivity.java:108)
                                                                                 at android.app.Activity.dispatchActivityResult(Activity.java:6303)
                                                                                 at android.app.ActivityThread.deliverResults(ActivityThread.java:3818)
                                                                                 at android.app.ActivityThread.handleSendResult(ActivityThread.java:3865)
                                                                                 at android.app.ActivityThread.access$1700(ActivityThread.java:159)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5569)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Jt1995
  • 149
  • 2
  • 15

1 Answers1

1

As I explained elsewhere, don't expect Android Studio to resolve magically the native method declarations into a library that is not built with gradle in integrated externalNativeBuild.

You can simply ignore this error message: your APK will still install the prebuilt library, and the native methods will be resolved at run time, even if Android Studio marks them red.

You can add @SuppressWarnings("JniMissingFunction") annotation for this method, or for the entire class:

@SuppressWarnings("JniMissingFunction")
public class Ximgproc {

or configure this kind of Lint inspections for the given project, or for all projects:

Preferences/Editor/Inspections

But this does not resolve your runtime problem. You presumably have built your C++ code to produce a native shared library, say its name is libXimgproc-native.so. If it is packed correctly into your APK, it will be extracted to /data/app-lib/com.example.jt1995.provaemo/ (you can check this path with getContext().getApplicationInfo().nativeLibraryDir).

Your Java code should load this library before it tries to call the native methods of the org.opencv.ximgproc.Ximgproc class:

System.load("Ximgproc-native");

If all above assumptions are correct, the linker did not find an exported function in that library that implements the createSuperpixelSLIC_0 native method. It tried both Java_org_opencv_ximgproc_Ximgproc_createSuperpixelSLIC_10 and Java_org_opencv_ximgproc_Ximgproc_createSuperpixelSLIC_10__JIIF.

To check which methods are exported by the library, you can use the nm tool which is part of the NDK gcc toolchain. E.g. on my Mac this executable can be found at ~/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-nm.

Run …nm -D Ximgproc-native.so and it will list, with T, all exported functions of your library.

I believe you will not find your function in this list. Maybe, the name is slightly wrong. Maybe, you set CFLAGS to -fvisibility=hidden, and did not explicitly declare the function as JNIEXPORT (or __attribute__ ((visibility ("default")))). Maybe, the C++ function is not declared with extern "C", and its name is mangled by the compiler.

If you use static libraries (xxx.a) as intermediates to assemble the resulting shared library, you should know that the linker can throw away unused external functions. In such case, using LOCAL_WHOLE_STATIC_LIBRARIES instead of LOCAL_STATIC_LIBRARIES may help.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • This is what i get with "getContext().getApplicationInfo().nativeLibraryDir " : /data/app/com.example.jt1995.provaemo-1/lib/arm – Jt1995 Jun 08 '17 at 09:14
  • Another question, u wrote about "Ximgproc-native.so",in my folder project i have "jniLibs" folder with libs like "libopencv_ximgproc.a" but i wrote ".so",mine is ".a" @AlexCohn – Jt1995 Jun 08 '17 at 09:32
  • In the end, you need a *shared library* which is ".so". You can use *static libraries* (".a") to build a shared library, but more often than not, these static libraries are a cause of confusion. If you use `ndk-build` to build "libopencv_ximgproc.a", you can simply change $(BUILD_STATIC_LIBRARY) to $(BUILD_SHARED_LIBRARY) in an **Android.mk** file. – Alex Cohn Jun 08 '17 at 10:31