System.load to load the library

I downloaded the library from the directory /system/libs/my_lib.so

successfully. How can I use the C / C ++ functions defined in this library?

public class MainFrom extends Activity {

    private static final String LOG_TAG = "MainFrom";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main); 
        // How to use the functions of test_lib.so? 

        /*
            java.lang.UnsatisfiedLinkError: stringFromC


        String s1 = stringFromC(), s2 = stringFromCpp();

        Log.w(LOG_TAG, stringFromC());   
        Log.w(LOG_TAG, stringFromCpp());  */
    }

    public native String stringFromC();
    public native String stringFromCpp();

    static {
        try { 
            System.load("/system/lib/test_lib.so");
            Log.i(LOG_TAG, "MainFrom. Success!");
        } catch (UnsatisfiedLinkError e) {
            Log.e(LOG_TAG, "MainFrom. UnsatisfiedLinkError");
        }
    }

}

      

stringFromC

and stringFromCpp

exist in files .c

and .cpp

that were compiled intotest_lib.so

+3


source to share


3 answers


I solved my problem.

It was necessary to write

System.load("/system/lib/libtest_lib.so");

      

instead



System.load("/system/lib/test_lib.so");

      

So weird. If I run

adb shell 
ls /system/lib

      

I will see the file test_lib.so. Why is it correct to load a library using the lib prefix ?

+3


source


you need to put LOCAL_CPPFLAGS := $(YOURMODULE_CPPFLAGS)

and in LOCAL_SRC_FILES := yourfile.cpp

in android.mk file to compile .cpp file using android NDK.



Hope this helps you.

0


source


Starting with Android 7.0, the system prevents applications from dynamically linking to non-NDK libraries, which may cause the application to crash. This change in behavior aims to create a consistent app experience across platform updates and across devices.

android 7.0 change description

0


source







All Articles