Markov
Markov

Reputation: 139

Can you use .so libs from one application in other application?

If there is an application that has a .so lib. say "example.so", that has a function like Java_com_domain_demo_exampleFuntion, can you call it from your application which has a different application id?

If the new application ID is com.otherdomain.demo2 then there is an error like

No implementation found for void Java_com_otherdomain_demo2_exampleFuntion()

Upvotes: 0

Views: 60

Answers (2)

Botje
Botje

Reputation: 31020

There is another means of achieving this if you cannot rename your application: write a small .so file that links with example.so and that calls JNI's registerNatives in its JNI_OnLoad function:

The following example is adapted from the Android JNI documentation:

JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
        return JNI_ERR;
    }

    // Find your class. JNI_OnLoad is called from the correct class loader context for this to work.
    jclass c = env->FindClass("com/markov/App/MyClass");
    if (c == nullptr) return JNI_ERR;

    // Register your class' native methods.
    static const JNINativeMethod methods[] = {
        {"myExampleFunction", "()V", reinterpret_cast(Java_com_domain_demo_exampleFuntion)},
    };
    int rc = env->RegisterNatives(c, methods, sizeof(methods)/sizeof(JNINativeMethod));
    if (rc != JNI_OK) return rc;

    return JNI_VERSION_1_6;
}

This example binds Java_com_domain_demo_exampleFuntion to com.markov.App.MyClass#myExampleFunction. You can add more functions to the methods array, of course.

Note that the behavior of the functions you copy may depend on certain fields of the class you bind it to. If those fields are not present on your com.markov.App.MyClass class, the JNI invocation will fail or crash. For example, many Java wrappers use long fields to store pointers to C memory.

Upvotes: 2

You can use to create special module(same package name) which It named like Java_com_domain_demo_exampleFuntion(). And you using this module another app. But, you can't use *.so file other app with different package name.

Upvotes: 0

Related Questions