Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using existing shared library (.so) in Android application

I have the follow scenario to work on. I was given a shared library (libeffect.so) to use in a Android project i am working for a client. I dont have the shared library source code, i have just the .so file with me. The library is pre-compiled to work on android devices. Along with the shared library I have the method signature

public static native void doEffect(int param1, IntBuffer intBuffer);

So now I have some questiosn on how to make the call to this native method, of source, if this is possible having just the .so file, so there they are:

  1. Do I need to place the native method signature in the same package/class as those defined when the .so was or I can use this signature in any package/class in my project that during runtime the jvm will be able to find the method in the shared library? For example, if this shared library was firstly used in a class mypackage.MyClass, do I need to create the same package, class and then put the method signature there?

  2. Where do I need to place this .so file inside my eclipse android project to get this file deployed inside my apk file?

These question might sound noob, but I have never worked with jndi before so I am a bit concerned if calling the method doEffect without any error can be achieve. Any answer that can guide me is very welcome.

Many Thanks Thiago

like image 388
Thiago Avatar asked Jun 23 '11 04:06

Thiago


People also ask

How do I open a .so file?

SO files can technically be opened with GNU Compiler Collection but these types of files aren't intended to be viewed or used like you might another type of file. Instead, they're just placed in an appropriate folder and used automatically by other programs via Linux's dynamic link loader.

What is Android shared library on Android?

Shared library type: this library is not backwards -compatible, can be updated and updates can be uninstalled. Clients link against a specific version of the library. Static shared libraries simulate static linking while allowing for multiple clients to reuse the same instance of the library.


1 Answers

  1. Do I need to place the native method signature in the same package/class as those defined when the .so was or I can use this signature in any package/class in my project that during runtime the jvm will be able to find the method in the shared library? For example, if this shared library was firstly used in a class mypackage.MyClass, do I need to create the same package, class and then put the method signature there?

No need to create same package/class. You can put the method signature in any package.

public class NativeLib {

  static {
    System.loadLibrary("so_file");
  }

  public static native void doEffect(int param1, IntBuffer intBuffer);

}

2.Where do I need to place this .so file inside my eclipse android project to get this file deployed inside my apk file?

You have put this .so file in lib folder of your application . IF lib folder is not there then you can create a lib folder and put the .so file. you can call it by using System.loadLibrary("so_ file");

like image 187
Sujit Avatar answered Sep 21 '22 15:09

Sujit