Activity中两次调用第三方库的native方法导致Android应用程序关闭 [英] Calling native method twice of third party library in an Activity causes the Android application to close down

查看:25
本文介绍了Activity中两次调用第三方库的native方法导致Android应用程序关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中集成了两个本机库 (.so).这些库编译得很好,我也可以将它们加载到我的应用程序中.我第一次调用库的本地方法时它工作正常,但如果我在 Activity 中再次调用相同的方法,应用程序将关闭.

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.

我面临的问题与此处提到的完全相同:
http://grokbase.com/t/gg/android-ndk/1226m68ydm/app-exit-on-second-native-call

The problem I am facing is exactly the same as mentioned in here :
http://grokbase.com/t/gg/android-ndk/1226m68ydm/app-exit-on-second-native-call

有效的解决方案是调用另一个 Activity 中的本地方法并通过 System.exit(0) 强行关闭它.按照文章,我尝试在成功操作后将被调用方法的指针设置为 NULL,但这也没有帮助我.一旦通过 System.loadLibrary() 加载库,也无法卸载它.

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().

我想在不创建新活动的情况下多次调用本机方法.任何想法如何解决这个问题?

I want to call the native methods more than once without creating a new Activity. Any ideas how to solve this issue ?

(我终于找到了解决方案......在这里)

(I FINALLY FOUND A SOLUTION ... HERE IT IS)

好的,我终于找到了解决这个问题的方法.解决方法其实很简单.构建另一个独立的原生库(实用程序库)来加载和卸载其他库.我们需要做的是在实用程序的本机方法中使用 dlopen() 和 dlclose().我们可以像以前一样通过 System.loadLibrary() 加载实用程序库.

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().

那么在实用程序库的本地方法中我们需要做的是:

So in the native method of the utility library what we need to do is:

Use#include //这是调用 dlopen() 和 dlclose() 函数所必需的.

提供处理程序和函数原型:

Use#include <dlfcn.h> // this is required to call dlopen() and dlclose() functions.

Provide handler and function prototype:

void *handle;
typedef int (*func)(int); // define function prototype
func myFunctionName; // some name for the function

通过 dlopen() 打开库:

Open the library via dlopen() :

handle = dlopen("/data/data/my.package.com/lib/somelibrary.so", RTLD_LAZY);

获取和调用库的函数:

myFunctionName = (func)dlsym(handle, "actualFunctionNameInLibrary");
myFunctionName(1); // passing parameters if needed in the call

现在通话已经完成.通过 dlclose() 关闭它:

Now that the call is done. Close it via dlclose():

dlclose(handle);

希望这能帮助其他面临同样问题的人.

Hope this will help others facing the same issue.

推荐答案

所以...我的解决方案是启动一个运行共享库代码的服务,这个服务有一个不同的进程名称(你可以在 Android Manifest 中设置)),因为它是一个不同的进程,您可以在它完成运行后终止它(使用 Process.killProcess(Process.myPid()),而不会以任何方式影响您的应用程序.

So ... my solution was starting a service that runs the shared library code, this service has a different process name ( you can set it in the Android Manifest ), as it is a different process you can kill it ( Using Process.killProcess(Process.myPid()) when it finishes running, without affecting your application in any way.

对我来说效果很好,希望对其他人有帮助.

Worked very well for me, hope it helps someone else.

这篇关于Activity中两次调用第三方库的native方法导致Android应用程序关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆