实例化从Java JNI库的多个副本 [英] Instantiating multiple copies of a JNI library from Java

查看:241
本文介绍了实例化从Java JNI库的多个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才说的数据,我已经写了捕捉传感器(加速度计大多陀螺仪和)数据和做一些特征检测算法在JNI库。检测出的特征是通过几个配置文件配置。当检测到的特点,JNI使用回调来通知应用程序的Java端。所有这一切的伟大工程。

I have a JNI library that I've written to capture sensor (mostly accelerometer and gyro) data and do some feature detection algorithms on said data. The features detected are configurable via a few configuration files. When the features are detected, the JNI uses a callback to notify the java side of the application. All this works great.

现在我希望能够同时运行(这样我就可以一次识别来自多个配置文件功能)相同的JNI库的多个实例。要做到这一点,我写了一个实现的JNI库中的回调和照顾库的所有的初始化,以及一个包装类。我打算简单的实例化这个类,并分别使用每个实例。什么我发现是,虽然每个包装的实例是不同的,该库是在几乎就像是静态声明的情况下重复使用。当我尝试从包装类的第二个实例初始化库,我发现它已经被初始化。

Now I want to be able to have multiple instances of the same JNI library running simultaneously (so I can recognize features from multiple configuration files at once). To do this, I wrote a 'wrapper' class that implements the callbacks for the JNI library and takes care of all the initialization of the library as well. I was planning on simply instantiating this class and using each instance separately. What I've found is that while each wrapper instance is distinct, the library is reused across instances almost like it was statically declared. When I try to initialize the library from the second instance of the Wrapper class, I find it has already been initialized.

下面是类似code我写了一个包装类:

Here is a wrapper class similar to the code I've written:

public class JNIWrapper {

    public native int initializeJNI(String configPath);
    public native void endProcessing();
    public native int getInstanceIdFromJNI();

    public JNIWrapper(){
        try {
            System.loadLibrary("libjnicode.so");
        }
        catch (Exception e) {
            Log.e("JNI", "WARNING: Could not load libjnicode.so: " + e.getMessage());
        }
    }

    public int initialize(String configPath){
        return initializeJNI(configPath);
    }
    public void stop(){
        endProcessing();
    }
    public void callbackFromJNI(int output, int instanceId){
        //notify the subscribed application(s) of the feature detection
        //via message passing.
    }
}

有谁知道我怎么能实例化一个JNI库的多个副本吗?结果
谢谢!

Does anyone know how I can instantiate multiple copies of a JNI library?
Thanks!

推荐答案

您不能这样做。动态连接器将只在给定.so文件加载到进程中一次。

You can't do that. The dynamic linker will only load a given .so file into a process once.

在理想情况下,你会修改库,给它一个光面向对象的风格,让您创建实例和初始化这些(而不是进程级静态),从配置文件或电话。这不一定是那么复杂,因为它似乎 - 基本上把所有的国家在结构,并通过所有通话的指针传递给它。你必须产生一个累贴指1马拉松编辑器会话,然后是一些错误的清理工作。幸运的是,一旦你删除静态变量,你会得到编译所有剩余尝试错误使用它们。

Ideally you would modify the library to give it a light object-oriented style, allowing you to create instances and initialize those (rather than process-level static state) from your configuration files or calls. This isn't necessarily as complicated as it seems - basically put all your state in a struct and pass the pointer to it through all your calls. You'll have one marathon editor session resulting in a tired "paste" finger, and then some mistake cleanup. Fortunately once you remove the static variables you'll get compile errors on all remaining attempts to use them.

一个非常哈克替代方法可能是在你的Andr​​oidManifest.xml中声明一些远程进程服务,并加载库到每个那些。或者,真正打破了Android机型(和随机杀人理论上的风险),库加载到多个创建点播机可执行程序。

A very hacky alternative might be to declare some remote-process services in your AndroidManifest.xml and load the library into each of those. Or, really breaking the android model (and at theoretical risk of random killing), load the library into multiple created-on-demand native executables.

这篇关于实例化从Java JNI库的多个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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