是java本机接口方法和模块名称的命名约定吗?

use*_*293 2 java java-native-interface android device-driver linux-device-driver

我能够很好地遵循jni教程.但是当我改变方法名称时,我遇到了麻烦.我需要遵循命名约定吗?本教程使用HelloJNI作为模块名称和库名称.我用过"useaaacom".

我得到了很好的反馈,我正在取得进展.我有一个相关的问题; 如果我应该为它创建另一个帖子,请告诉我.我喜欢在此应用程序上构建,此应用程序在此时运行.如何从设备驱动程序调用函数?我有头文件,驱动程序加载到我的图像.根据"我的意思",我是否需要在项目中获得头文件的副本?此设备驱动程序是供应商实现的,即它不是AOSP的一部分.自从我下载整个开源项目并构建它以来,我确实有它的副本.所以我要问的是我在apk中需要什么才能让app调用属于活动设备驱动程序的函数?

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

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

Mic*_*ael 21

来自Oracle的文档:

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

  • 前缀 Java_
  • 一个受损的完全限定的类名
  • 下划线(_)分隔符
  • 一个受损的方法名称
  • 对于重载的本机方法,两个下划线(__)后跟受损的参数签名

所以如果你有以下内容:

package com.foo.bar;

class Baz {
    public native void Grill(int i);
}
Run Code Online (Sandbox Code Playgroud)

那么相应的C函数应该是:

JNIEXPORT void JNICALL Java_com_foo_bar_Baz_Grill(JNIEnv *env, jobject thiz, jint i);
Run Code Online (Sandbox Code Playgroud)

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

public native void A_Grill(int i);
Run Code Online (Sandbox Code Playgroud)

然后C函数将是:

JNIEXPORT void JNICALL Java_com_foo_bar_Baz_A_1Grill(JNIEnv *env, jobject thiz, jint i);
Run Code Online (Sandbox Code Playgroud)

_1转义序列的匹配_A_Grill.