开发者

cannot use external JNI function in two different classes, unsatisfied link error

开发者 https://www.devze.com 2023-03-22 04:22 出处:网络
For an Android application, I have implemented an external function in C, which I would like to use in two separate classes.

For an Android application, I have implemented an external function in C, which I would like to use in two separate classes.

In the first class (my main Activity UI), I call the appropriate loadLibrary:

System.loadLibrary(...);

In the same class, I define the function as native:

public native int dissectPacket(byte[] header, byte[] data, int encap);

After doing this, I can call the native function with no problem in the first class. I do not get any unsatisfied link error.

Now, I want to use this function in开发者_JAVA技巧 another class. I figure I do not need to load the library again. In the second class, at the bottom, I also define:

public native int dissectPacket(byte[] header, byte[] data, int encap);

However, when I try to use the native function in the second class, I get:

07-22 23:13:13.083: ERROR/AndroidRuntime(6737): Caused by: java.lang.UnsatisfiedLinkError: dissectPacket

What is the proper way to use the function in both classes? If I do not redefine the function as native in the second class (called Packet), I get the error:

The method dissectPacket(byte[], byte[], int) is undefined for the type Packet

BTW, I do NOT want to use: class1.dissectPacket(...); I am trying to avoid passing the class.


You defined actually two separate functions. One for the first class and another one for the second. They will need two separate JNI stubs. You, probably, only have stub and implementation for the first one.

JNI and Java, in general, always refer to methods of the specific class.


"BTW, I do NOT want to use: class1.dissectPacket(...); I am trying to avoid passing the class."

If you want to do that, the member functions need to be static, otherwise the class is implicitly passed as a parameter (I don't know how because I've never done it, static functions have always worked for me, but it has to happen to work properly).

So change your method stubs to:

public static native int dissectPacket(byte[] header, byte[] data, int encap);
0

精彩评论

暂无评论...
验证码 换一张
取 消