Android uses JNI to call ndk

Source: Internet
Author: User

Configure the ndk environment variables and generate the so file:
1. First, find the installation directory of cygwin and find a home \ <your username> \. bash_profile file. My files are: e: \ cygwin \ home \ Administrator \. bash_profile. (Note: When I installed my home folder, there was no magic horse in it. Solution: First open the environment variable and delete the home variable in the user variable, in E: in the \ cygwin \ home folder, create a folder named "Administrator" (user name), and then set E: \ cygwin \ etc \ skel \. copy bash_profile to this folder).

2. Open the bash_profile file. (You must use the ultraedit file editor to open it and do not convert the format after opening it! If you use another editing tool to open it, it will be garbled and cannot be executed !) Add ndk =/cygdrive/<your drive letter>/<Android ndk directory> example:

Ndk =/cygdrive/e/Android-ndk-r8b

Export ndk

The ndk name is random. For future convenience, select a short name and save it.

3. Open cygwin and input CD $ ndk. If the/cygdrive/e/Android-ndk-r5 information configured above is output (the output is the installation directory of ndk ), the environment variable is successfully set.

 

4. compile it into the so file. After configuration 3 is successful, you can configure to generate the so file. First, you need to go to the project where you need to generate the so file. Here I am using the example path (/cygdrive/e/android-ndk-r8b/samples/Hello-JNI) then, type $ ndk/ndk-build to automatically generate the libs \ armeabi folder and the corresponding so file under the project.

 

. C file parsing:
Jstringjava_com_example_hellojni_hellojni_stringfromjni (jnienv * ENV, jobject thiz) {return (* env)-> newstringutf (ENV, "hello from JNI! ");}

The corresponding function name of native must start with "Java _", followed by Java's "package name", "Class Name", and "function name ", in the middle of the package name, the underscore "_" is separated. "should also be changed to" _ ". In addition, there are rules for function parameters and return values. For the basic types in Java, suchInt,Double,CharIn native, there are corresponding types, suchJint,Jdouble,JcharOther object types areJobjectTo indicate (StringIs an exception. Because it is widely used, it hasJstringThis type is represented as the return value in the preceding example.StringCorresponding to the return value in native codeJstring). For arrays in JavaJarrayCorresponding, specific to the basic type and the general object type array, there areJintarrayAndJobjectarrayCorresponding (StringArray is not an exception here.Jobjectarray).

Note that in the native function of JNI, the first two parametersJnienv* AndJobjectIs required -- the former isJnienvThe struct pointer is the core data of JNI. This struct defines many JNI interface function pointers so that developers can use the interface functions defined by JNI;

Use of the jnienv * env Parameter

The first parameter of all JNI interfaces is jnienv * env. In C

(* Env)-> newstringutf (ENV, "hello from JNI! ");

However, in C ++, the call method is

Env-> newstringutf ("hello from JNI! ");


The latter refers to the Java object that calls this JNI function, which is somewhat similarThisPointer. After the preceding two parameters, you also need to add them according to the Java function declaration. In the preceding example, if the JNI function declared in Java does not have a parameter, only the corresponding function type of native isJnienv* AndJobject.

 

Java file:

From the above native function naming, we can understand the naming rules of JNI function: the function declaration in Java code needs to be addedNative

Example: Public native string stringfromjni ();

Of course, to use the JNI function, you also need to first load the dynamic library file compiled by native code (. dll in windows and. So in Linux ). This action is completed using the following statement:
  1. Static {
  2. System. loadlibrary ("hello-JNI ");
  3. }

 

Generate the corresponding. h file:

Note that you must first compile the Java file so that it can generate the. Class file before generating the corresponding. h file.

1. Enter the corresponding project in the command line.

2. Type javah-classpath bin-d jni com. example. hellojni. hellojni green to package name + class name

* *********** Later, it was found that the generated. Class file is not directly placed in the bin folder, but contains a subdirectory named classes.
* ***********. Therefore, change the preceding command:
* *********** Javah-calsspath bin/classes-d jni <package>. <class>

* ********** That is: javah-classpath bin/classes-d jni com. example. hellojni. hellojni Green indicates the package name + class name

 

 

Compile the Android. mk File:

Create an android. mk file under the JNI directory (that is, the hello-jni.c directory at the same level). The Android. mk file is the MAKEFILE file of Android, the content is as follows:
 

Local_path: = $ (call my-DIR)

The local_path variable must be defined in an android. mk file. It is used to search for source files in the Development tree. In this example, the macro function 'my-dir' is provided by the compilation system and used to return the current path (that is, the directory containing the Android. mk file ).

 

Include $ (clear_vars) clear_vars is provided by the compilation system, specifying that GNU makefile clears many local_xxx variables (such as local_module, local_src_files, local_static_libraries, etc. ..) for you, except local_path. This is necessary because all the compilation control files are in the same GNU make execution environment, and all the variables are global.

Local_module: = hello-JNI

The target object to be compiled. The local_module variable must be defined to identify each module you describe in the Android. mk file. The name must be unique and contain no spaces. Note: The compilation system automatically generates the appropriate prefix and suffix. In other words, a shared library module named 'hello-JNI 'will generate 'libello-JNI. so 'file. Important: If you name the library 'libello-JNI ', the compiling system will not add any lib prefix or generate 'libello-JNI. so ', to support Android from the source code of the Android platform. MK file, if you do need to do so.

Local_src_files: = hello-jni.c

The local_src_files variable must contain the C or C ++ source code files to be compiled and packaged into the module. Note that you do not need to list header files and contained files here, because the compilation system will automatically find the dependent files for you; just list the source code files directly transmitted to the compiler. Note that the default C ++ source code file extension is '. CPP '. it is also possible to specify a different extension. As long as you define the local_default_cpp_extension variable, do not forget the starting dot (that is '. cxx ', not 'cxx ')

Include $ (build_shared_library)

Build_shared_library indicates compiling and generating shared libraries. It is a variable provided by the compilation system and points to a GNU makefile script to collect information from the previous call of 'include $ (clear_vars, defines all information in the local_xxx variable and determines

Determine what to compile and how to do it correctly. The build_static_library variable also indicates generating static libraries: Lib $ (local_module). A, build_executable indicates generating executable files.

 

Local_static_libraries: =?
Reference a third-party library

 

A self-written Demo:/files/lee0oo0/myjnindk.rar

 

The following describes the role of each example under the android-ndk-r8b: Next detailed analysis of the following 5, 6, 7, 8, 12

1. Bitmap-Plasma :In the example of how to use bitmap in ndk, bitmap cannot be used directly in earlier ndk versions, and bitmap is supported in later versions. 2. hello-gl2 :How to Use opengles in ndk 3. Hello-JNI :The most basic ndk usage method. Obtain the string through ndk and display it in the Android app. 4. Hello-neon :Neon Optimization in ndk 5. Module-Exports :The call method of multiple databases. Foo is compiled as a static library, bar is compiled as a dynamic Library and the Library foo is called. Zoo is compiled as a dynamic library and the library bar is called. 6. Native-Activity :Use ndk to implement the entire android Program 7. Native-Audio :Audio-related operations in ndk 8. Native-Media :Video operations in ndk 9. Native-Plasma :Fully implement the entire android program with ndk and provide optimization involving plasma 10. San-Angeles :Examples of OpenGL ES transplanted to Android platforms 11. Test-libstdc ++ :C ++ support, but not all features of C ++

12. Two-libs:Use of the two databases. The first database is a static database, the second database is a dynamic database, and the second database calls the first database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.