Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling native method twice of third party library in an Activity causes the Android application to close down
    primarykey
    data
    text
    <p>I have integrated two native libraries (.so ) in my application. The libraries compile fine and I can load them in my application too. The first time I invoke a native method of a library it works fine, but if I call the same method again in the Activity the application shuts down.</p> <p>The problem I am facing is exactly the same as mentioned in here :<br> <a href="http://grokbase.com/t/gg/android-ndk/1226m68ydm/app-exit-on-second-native-call" rel="noreferrer">http://grokbase.com/t/gg/android-ndk/1226m68ydm/app-exit-on-second-native-call</a></p> <p>The solution that works is to invoke the native method in another Activity and shut it down forcefully via System.exit(0). Following the article I tried setting the pointers to NULL of the called method after a successful operation, but this too didn't help me. Also its not possible to unload a library once its loaded by System.loadLibrary().</p> <p>I want to call the native methods more than once without creating a new Activity. Any ideas how to solve this issue ?</p> <p>(I FINALLY FOUND A SOLUTION ... HERE IT IS)</p> <p>Okay, I have finally found a way to resolve this issue. The solution is actually pretty simple. Build another independent native library (utility library) to load and unload the other libraries. What we need to do is use dlopen() and dlclose() in the native method of the utility. We can load the utility library like before via System.loadLibrary().</p> <p>So in the native method of the utility library what we need to do is:</p> <p>Use<code>#include &lt;dlfcn.h&gt;</code> // this is required to call dlopen() and dlclose() functions.<br><br> Provide handler and function prototype:</p> <pre><code>void *handle; typedef int (*func)(int); // define function prototype func myFunctionName; // some name for the function </code></pre> <p>Open the library via dlopen() : </p> <pre><code>handle = dlopen("/data/data/my.package.com/lib/somelibrary.so", RTLD_LAZY); </code></pre> <p>Get and Call the function of the library:</p> <pre><code>myFunctionName = (func)dlsym(handle, "actualFunctionNameInLibrary"); myFunctionName(1); // passing parameters if needed in the call </code></pre> <p>Now that the call is done. Close it via dlclose():</p> <pre><code>dlclose(handle); </code></pre> <p>Hope this will help others facing the same issue.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload