Adrian Jandl
Adrian Jandl

Reputation: 3015

Including .so file and jar wrapper into Android project

I've been searching for the past 2 days on SO and Google for a solution to my problem but nothing I found would solve my problem.

I'm trying to include a .so library that came with a .jar wrapper for the native functions in the .so. When I extract the jar with winrar I can see that it only contains .class files - is this normal?

I am working with Eclipse and have created a new project and added the .so file to the libs/armeabi folder and the .jar to my project. enter image description here

I've set the properties so that the Native library location is set to the libs/armeabi folder.

I've also removed the Build Options to Automatically refresh Resources and Assets folder on build aswell as Force error when external jars contain native libraries though I believe the second is not my error at the moment.

I'm using this code to try to load the program to my phone.

public class MainActivity extends Activity {
    static {
        Log.w("MYAPP", "About to load library.");
        System.loadLibrary("libjdns_sd");
        Log.w("MYAPP", "Library loaded.");
    } 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

The library should be loaded through the static block.

My LogCat however shows this output:

11-30 18:13:47.830: W/MYAPP(9158): About to load library.
11-30 18:13:47.830: W/dalvikvm(9158): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/example/bonjourapple/MainActivity;
11-30 18:13:47.830: W/dalvikvm(9158): Class init failed in newInstance call (Lcom/example/bonjourapple/MainActivity;)
11-30 18:13:47.830: D/AndroidRuntime(9158): Shutting down VM
11-30 18:13:47.830: W/dalvikvm(9158): threadid=1: thread exiting with uncaught exception (group=0x41949898)
11-30 18:13:47.830: E/AndroidRuntime(9158): FATAL EXCEPTION: main
11-30 18:13:47.830: E/AndroidRuntime(9158): java.lang.UnsatisfiedLinkError: Couldn't load libjdns_sd from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.example.bonjourapple-2.apk,libraryPath=/data/app-lib/com.example.bonjourapple-2]: findLibrary returned null
11-30 18:13:47.830: E/AndroidRuntime(9158):     at java.lang.Runtime.loadLibrary(Runtime.java:355)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at java.lang.System.loadLibrary(System.java:525)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at com.example.bonjourapple.MainActivity.<clinit>(MainActivity.java:11)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at java.lang.Class.newInstanceImpl(Native Method)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at java.lang.Class.newInstance(Class.java:1130)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.app.ActivityThread.access$700(ActivityThread.java:159)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.os.Looper.loop(Looper.java:137)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at android.app.ActivityThread.main(ActivityThread.java:5419)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at java.lang.reflect.Method.invoke(Method.java:525)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-30 18:13:47.830: E/AndroidRuntime(9158):     at dalvik.system.NativeStart.main(Native Method)

Why is the library not being loaded? What am I doing wrong? If anyone could point me in the right direction I would be very grateful.

Upvotes: 3

Views: 2852

Answers (1)

Adrian Jandl
Adrian Jandl

Reputation: 3015

Turns out all I had to do was to place the .jar file into the usual libs folder and put the .so file into libs/armeabi-v7a. I was then able to load the native library and access its functions.

Upvotes: 3

Related Questions