2

I wonder why there is no proper answer to this question, I searched for couple of hours but no good answer. So, we work on a team in which my friend wrote a C library and compiled it as a .so file (it's called ttplib.so)(assume I don't have access to it's C code). Now I have to use that .so file in my android application. But I don't know how to load the library and how to use its methods. I have good documentation of it. That would be great if you can tell me how to create the Android.mk file too. Do I have to use dlopen?

foo64
  • 2,075
  • 3
  • 18
  • 25
Gabriel
  • 578
  • 3
  • 8
  • 22

1 Answers1

2

Put ttplib.so in the new project's libs/armeabi or libs/armeabi-v7a folder depending what it was compiled with.

Somewhere in your new app (before interacting with the library) add this line of code

System.loadLibrary( "ttplib" );

Now that it's loaded in memory, you'll need to interact with it using JNI. You'll have to go back to the C code to export some JNI functions:

JNIEXPORT jint JNICALL Java_com_example_package_MyClass_methodName( JNIEnv* env, jobject jthis, jfloat value )
{
    return 5;
}

Then you'll need to add ClassName.java in your new project:

package com.example.package;

public class MyClass
{
    private native int methodName( float value );

    private void someJavaMethod()
    {
        int i = methodName( 65.33f );
    }
}

That's it, in a nutshell.

foo64
  • 2,075
  • 3
  • 18
  • 25
  • But this is for when you wanna write the code in C in your application not that when you already have the shared library and you just wanna call its functions. – Gabriel May 12 '13 at 02:53
  • @Gabriel do you know now how to do that. then please help me in that – John smith May 24 '16 at 06:51