问题
I have C code (testFile.c) that I want to used in Android App, using JNI. This code contains pure C code and it has many methods inside. Moreover, it also calls some C header files (file1.h, file2.h).
For example: testFile.c
#include "file1.h"
#include "file2.h"
void myMethod1()
{
...
...
}
void myMethod2()
{
...
...
}
void myMethod3()
{
...
...
}
Whether I have to transform whole of testFile.c to JNI code, or just transform some methods that will i used later (not all of them)? Should i also transform c code inside header file (file1.h and file2.h), or i just transform c code in testFile.c?
回答1:
Or if the interface between your Java and C code is extremely simple but the overall process of using jni on Android is unfamiliar, you can more or less merge your C code into the hello-jni example from the ndk. The point being that you start with something that builds and works, add what you need bit by bit and if you run into issues you can back up and figure out at what stage you broke it.
回答2:
The easiest way to do this is to define your JNI Java side interface and use the automatic generation of c method stubs, javah
, to generate the c file. In this c file then make calls to the actual c methods method1()
, method2()
and method3()
.
http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javah.html
来源:https://stackoverflow.com/questions/10454503/how-to-transform-c-code-to-jni-code