Is the naming convention for java native interface

2019-02-15 12:11发布

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".

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;
}

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.

2条回答
男人必须洒脱
2楼-- · 2019-02-15 12:33

You're free to call your package and class and methods whatever you like at the Java level, subject to the rules of the language, but the naming convention at the C level is completely defined by the output of the javah tool.

You can call the shared library whatever you like as well subject to filename rules and the rules of ’System.load()/loadLibrary()`.

查看更多
走好不送
3楼-- · 2019-02-15 12:40

From Oracle's documentation:

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

  • 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);
}

Then the corresponding C function should be:

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

If you have an underscore in the Java method name:

public native void A_Grill(int i);

Then the C function would be:

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

The _1 escape sequence matches the _ in A_Grill.

查看更多
登录 后发表回答