Skip to content Skip to sidebar Skip to footer

Use Prebuilt Jni Library In Android Studio 3.1

I was given a shared object (*.so file) that I need to reference from my app. I know for a fact that the shared object uses JNI. Now, I have some experience with Android app develo

Solution 1:

If you don't have the Java sources that defined these native methods originally, you can often reverse engineer the necessary native method definitions from the .so file itself. E.g., if the library exports Java_com_example_testjni_MainActivity_stringFromJni, then you must add a Java class

package com.example.thestjni;

classMainActivity {
    publicnativestatic String stringFromJni();
}

This does not give your the correct parameters or return types for the native methods. Also, you must guess whether the native method should be declared static or not.

To be on the safe side, you will add a static constructor to this class, to make sure the library is loaded:

package com.example.thestjni;

classMainActivity {
    static {
        System.loadLibrary("thirdparty");
    }
    publicnativestatic String stringFromJni();
}

You are free to rename the 3 party library to your liking, but not the package and not the class. You don't care if the original Java class declared the native method private, you can safely declare it public in your project.

To find the names of JNI functions exported by prebuilt library libthirdparty.so, you can use the nm tool provided with Android NDK toolchains:

nm -D libthirdparty.so | grep Java_

Solution 2:

If you have the Java code for another app that uses it- find the Java code in the other library that calls it. Those functions should be defined with the keyword native. Take that class(es), without renaming or changing the package, and put it in your project. Now you can call those native functions via the Java native function definitions.

Of course without source you can't compile for any other architecture. So hopefully you have the .so files for all the appropriate ones. Remember to load the library if those class(es) don't do it for you.

Post a Comment for "Use Prebuilt Jni Library In Android Studio 3.1"