4

I am trying out to integrate native written shared library into my app when it says that the .so file in jniLibs and other libraries cannot be loaded.

Here is the C file

#include <string.h>
#include <jni.h>
#include <dummy.h>

JNIEXPORT jstring
Java_com_example_hellojni_HelloJni_DummyInit(JNIEnv*env, jobject thiz) {

    dummy *handle;
    char *msg;
    int rc = dummy_init_from_id("ml", &handle, &msg);
    if (rc == DUMMY_SUCCESS) {
        return (*env)->NewStringUTF(env, "Init was successful");
    } else {
        return (*env)->NewStringUTF(env, msg);
    }

}

JNIEXPORT void 
Java_com_example_hellojni_HelloJni_DummySetSymbolsDir(JNIEnv *env, jobject thiz,
                                                      jstring dir) {
    dummy_set_symbols_dir(dir);

}

Here is the Java file

package com.example.hellojni;

import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import android.os.Bundle;
import java.io.File;


public class HelloJni extends Activity
{


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);

        File f = new File(getAssets() + "/ml");
        if (f.exists()) try {

            System.out.println("Scheme exists");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String dir = f.getPath();

        DummySetSymbolsDir(dir);

        TextView tv = new TextView(this);
        tv.setText(DummyInit());
        setContentView(tv);
    }


    /*** Native Methods ***/
    public native String DummyInit();
    public native void DummySetSymbolsDir(String dir);

    static {
        try {
            System.loadLibrary(("dummy"));
        } catch (UnsatisfiedLinkError e) {
            Log.e("JNI", "Warning:Could not use dummy library");
        }
        try {
            System.loadLibrary("libs/libpthead.so");
        } catch (UnsatisfiedLinkError e) {
            Log.e("JNI", "Warning:Could not use lipthread library");
        }
        try {
            System.loadLibrary("hello-jni");
        } catch (UnsatisfiedLinkError e) {
            Log.e("JNI", "Warning:Could not use hello-jni library");
        }
    }
}

The exact error is given below

FATAL EXCEPTION: main
Process: com.example.hellojni, PID: 2558
java.lang.UnsatisfiedLinkError: No implementation found for void com.example.hellojni.HelloJni.DummySetSymbolsDir(java.lang.String) (tried Java_com_example_hellojni_HelloJni_DummySetSymbolsDir and Java_com_example_hellojni_HelloJni_DummySetSymbolsDir__Ljava_lang_String_2)
at com.example.hellojni.HelloJni.DummySetSymbolsDir(Native Method)
at com.example.hellojni.HelloJni.onCreate(HelloJni.java:34)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

The Android.mk file is as follows

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)

Do let me know if anymore informations are needed. Thanks.

2 Answers2

0

You might need to surround your code with an extern "C" block:

extern "C" {
    Your JNIEXPORT functions
}

For both C and C++ you can do:

#ifdef __cplusplus
extern "C" {
#endif

    Your JNIEXPORT functions

#ifdef __cplusplus
}
#endif
Max Plakhuta
  • 310
  • 2
  • 14
0

Put the code to retrieve the file in a try catch block (or anything similar). Also, when directing the code to the file, make sure you have the whole directory starting with C: (in windows).