0. Download the JDK and configure the environment variables
Download the latest JDK, Baidu search "JDK" on it. After the installation is complete, configure the environment variables (online search, a bunch, I just briefly described here)
- Computer → properties → advanced system settings → advanced → environment variables.
- System variables → new java_home variable. Variable value fill in the JDK installation directory (I am D:\Program files\java\jdk1.7.0_45)
-
System variables → new CLASSPATH variable
Variable values are filled in.; %java_home%\lib;%java_home%\lib\tools.jar (Note that there is a point in the front)
-
System variables → find Path variable → edit
Enter%java_home%\bin;%java_home%\jre\bin at the end of the variable value;
(Note that the original path of the variable value at the end of there is no; number, if not, first enter; number and then enter the above code)
System Variable Configuration complete
1, the official website download the latest ADT. Unzip (Eclipse best comes with NDK plugin)
2. Download the latest ndk from our website. Decompression can be (ndkr7 above with Cygwin)
3. Eclipse does not have its own plugin, then download the plugin.
Plug-in online has, I as my own study, also in the Baidu Cloud shared a copy,: Http://pan.baidu.com/s/1i3xImQp
Download the plugin and put it under the Plugins folder under Eclipse. Restart Eclipse.
4. Start with focus
Eclipse→window→preferences→android→ndk
Configure the path of the ndk you just downloaded
5. New Android Project
Right-click →properties→c/c++ Build. Remove the default build command
Set into ${ndkroot}/ndk-build.cmd
Then to build Variables click Add. Configure the. Name:ndkroot value: Your NDK path. Click OK.
6, the Project right button->android Tools->add Native support ..., enter the. So library name and click Finish
7. Now we can build our JNI project, select the project, Project->build project, compile our C + + code, the project structure is as follows, NDK plugin has added the include to us, The corresponding android.mk and CPP files have been generated for us. (Note: Here the plugin for us to generate a CPP file, if you do not like can be changed back to. c, and in the Mk file to make the corresponding changes)
8. It is possible to run our JNI project.
Problems encountered
1. Run the. So library generated by C + +, if the following error is reported: (Function not found)
No implementation found for native lcom/example/hellojni/mainactivity;. Stringfromjni () ljava/lang/string;
Java.lang.UnsatisfiedLinkError:stringFromJNI
At Com.example.hellojni.MainActivity.stringFromJNI (Native Method)
Workaround:
Add the extern "C" modifier to the C + + function called by Java, such as: (The CPP file inside the NDK example is also stated, refer to HELLO-GL2)
[Java]View Plaincopy
- extern "C" {
- Jniexport jstring jnicall Java_com_dgut_android_mainactivity_stringfromjni (jnienv* env, Jobject thiz);
- }
- Jniexport jstring jnicall Java_com_dgut_android_mainactivity_stringfromjni (jnienv* env, Jobject thiz)
- {
- return Env->newstringutf ("Hello from JNI Bear C + +");
- }
The reasons are:
Variables and functions modified by extern "C" are compiled and concatenated in the C language way.
Let's start with a look at how C + + is compiled for a function that is similar to the code: as an object-oriented language, C + + supports function overloading, whereas programming language C does not. Functions are compiled in C + + with different names in the symbol library than in the C language. For example, suppose a function is prototyped as: void foo (int x, int y), the function is compiled by the C compiler in the symbol library with the name _foo, and the C + + compiler produces names like _foo_int_int (different compilers may produce different names, However, the same mechanism was used to generate the new name called "Mangled name". _foo_int_int such a name includes the function name, function parameter number and type information, C + + is this mechanism to implement function overloading. For example, in C + +, the function voidfoo (int x, int y) is not the same as the symbol generated by the compilation of void foo (int x, float y), which is _foo_int_float.
Similarly, variables in C + + support class member variables and global variables in addition to local variables. The class member variable of the program that the user writes may have the same name as the global variable, and we use the "." to differentiate. In essence, the compiler, when compiling, is similar to the processing of the function and takes a unique name for the variable in the class, which differs from the name of the global variable named in the user program.
Therefore, if we do not use the extern "C" modifier to compile and concatenate in the C language, the JNI call will probably not find the function.