Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.load for library loading

I loaded a library from the /system/libs/my_lib.so directory successfully. How can I use the C/C++ functions which are 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 .c and .cpp files which were compiled to test_lib.so

like image 408
Maksim Dmitriev Avatar asked Mar 24 '26 18:03

Maksim Dmitriev


1 Answers

I have solved my problem.

It was necessary to write

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

instead of

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

So strange. If I run

adb shell 
ls /system/lib

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

like image 103
Maksim Dmitriev Avatar answered Mar 27 '26 10:03

Maksim Dmitriev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!