Java本机接口方法和模块名称的命名约定吗? [英] Is the naming convention for java native interface method and module name?

查看:183
本文介绍了Java本机接口方法和模块名称的命名约定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全可以按照jni教程进行操作.但是,当我更改方法名称时,就会遇到麻烦.我需要遵循一个命名约定吗?本教程使用HelloJNI作为模块名称和库名称.我使用了"useaaacom".

I am able to follow a jni tutorial just fine. But when I change the method name, I run into trouble. Is there a naming convention I need to follow? The tutorial used HelloJNI as the module name, and library name. I used "useaaacom".

对此我获得了很好的反馈,并且我正在取得进展.我有一个相关的问题;让我知道是否要为此再创建一个帖子.我喜欢在此应用程序上构建,该应用程序现在可以运行.如何从设备驱动程序调用函数?我有头文件,并且驱动程序已加载到我的映像中.我的意思是如何",我需要在项目中拥有头文件的副本吗?该设备驱动程序是由供应商实现的,即它不是AOSP的一部分.自从下载了整个开源项目并构建起来以来,我确实有它的副本.因此,我要问的是,我需要什么才能让apk调用该应用程序,这些功能是活动设备驱动程序的一部分?

I got great feedback on this and i am making progress. I have a related question; let me know if I should create another post for it. I like to build on this app, which runs at this point. How do I call functions from a device driver? I have the header file, and the driver is loaded into my image. By "how" I mean, do I need to have a copy of the header file in my project? This device driver is vendor implemented, i.e. it is not part of AOSP. I do have a copy of it since I downloaded the entire open source project and built it. So what I am asking is what do I need in my apk for the app to call the functions which are part of an active device driver?

让我知道是否应该进一步解释它的任何部分,或者我需要发布头文件或....

Let me know if I should explain any part of it more, or I need to post the header file or ....

我已经确认可以使用以下代码行打开设备驱动程序:

I already verified that I can open a device driver with the following lines of code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
/* Our file descriptor */
int fd;
int rc = 0;
char *rd_buf[16];
printf("%s: entered\n", argv[0]);
/* Open the device */
fd = open("/dev/hello1", O_RDWR);
if ( fd == -1 ) {
perror("open failed");
rc = fd;
exit(-1);
}
printf("%s: open: successful\n", argv[0]);
/* Issue a read */
rc = read(fd, rd_buf, 0);
if ( rc == -1 ) {
perror("read failed");
close(fd);
exit(-1);
}
printf("%s: read: returning %d bytes!\n", argv[0], rc);
close(fd);
return 0;
}

我想我需要将上述代码以.c源文件的形式添加到我的jni文件夹中,并从该文件中调用设备驱动程序头文件中的函数?您可能已经注意到,上面的代码适用于名为"hello1"的测试设备驱动程序.我要将该名称更改为我的目标设备驱动程序.

I think I need to add the above code to my jni folder in the form of a .c source file, and call the functions in my device driver header file from this file? You may have noticed that the above code is for a test device driver called "hello1". I am gonna change the name to my targeted device driver.

推荐答案

来自

动态链接器根据其名称解析条目.本机方法名称由以下组件连接而成:

Dynamic linkers resolve entries based on their names. A native method name is concatenated from the following components:

  • 前缀Java_
  • 完全限定的班级名称
  • 下划线(_)分隔符
  • 错误的方法名称
  • 对于重载的本机方法,两个下划线(__)后跟错误的参数签名
  • the prefix Java_
  • a mangled fully-qualified class name
  • an underscore (_) separator
  • a mangled method name
  • for overloaded native methods, two underscores (__) followed by the mangled argument signature

因此,如果您具有以下条件:

So if you have the following:

package com.foo.bar;

class Baz {
    public native void Grill(int i);
}

然后相应的C函数应该是:

Then the corresponding C function should be:

JNIEXPORT void JNICALL Java_com_foo_bar_Baz_Grill(JNIEnv *env, jobject thiz, jint i);

如果Java方法名称中带有下划线:

If you have an underscore in the Java method name:

public native void A_Grill(int i);

那么C函数将是:

JNIEXPORT void JNICALL Java_com_foo_bar_Baz_A_1Grill(JNIEnv *env, jobject thiz, jint i);

_1转义序列与A_Grill中的_相匹配.

The _1 escape sequence matches the _ in A_Grill.

这篇关于Java本机接口方法和模块名称的命名约定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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