Since I have been engaged in C ++ development, it is inevitable to use the JNI mode for development when I transfer to the Android platform.
My ndk version is r5b. The directory contains a hello-JNI sample program, which breaks the cost of importing the project to eclipse,
According to the online tutorial, it did not work. Later I checked the official documents to complete the import and compilation.
(For details about how to build the eclipse + ndk + cygwin platform, see this document: Integrating cygwin with eclipse in windows and using Android ndk R5 (supporting opengles), integrating (transferring)
)
Official documentation:
In eclipse:
- ClickFile> New Android Project...
- SelectCreate project from existing source
Radio button.
- Select any API level above Android 1.5.
- InLocation
Field, clickBrowse...
And select
The<ndk-root>/samples/hello-jni
Directory.
- ClickFinish
.
Then you can compile it.
If you want to build a JNI project by yourself, it is also very easy to create a new Android project, and then manually create a "JNI" directory under the project directory (level with the src directory ), then create an android. MK (makefile of Android) and your c file or CPP file. For function declaration and call, refer to the sample program Hello-JNI, note that the function name must follow the rule "java_packetname_projectname_functionname". Otherwise, a link error is reported during running.
A small note when writing C or C ++ programs:
1. C code. Use the Env parameter as in the instance, (* env)-> newstringutf (ENV, "hello from JNI! ");
2. c ++ code, you need to call env-> newstringutf ("hello from JNI! ");
The reason is that the jnienv statement in JNI. H is different for C and C ++, as follows:
# If defined (_ cplusplus)
Typedef _ JNIEnv;
# Else
Typedef const struct JNINativeInterface * JNIEnv;
# Endif