JNI practices for Android 1 (Environment setup and simple examples)

Source: Internet
Author: User

JNI practices for Android 1 (Environment setup and simple examples)
Note: Most of the steps below are from network information. due to incomplete or even incorrect network information, I have practiced and sorted out the entire process to take study notes. Thank you very much!

Intention: This is mainly because the engine is used for development of the game version 2.0, and the engine is implemented in c/c ++. Currently, the engine is being transplanted, so I started to work on this convenient information. I have been playing a pure android game before, but I didn't take it into consideration!

Next, let's start the journey of the day [before you start, you should have an android Development Environment ]:
1. Install Cygwin first. The purpose of installation is to generate the jni Library (. so) which requires cmake and gcc. Therefore, Cygwin is used to install the two compiling environments;
2. Install the android-ndk-r5 installation package, go to the official website or online search can be downloaded, can be other relatively new version; installation method directly extract to a specific directory;

(NDK, Cygwin installation package, you can also download): http://pan.baidu.com/s/1kTmhgEz

The above two steps can be collectively referred to

AndroidNDK

Environment Construction

AndroidNDK

Environment Construction

The Android NDK environment is set up. Baidu Library has the following materials:
Http://wenku.baidu.com/view/4c112a06eff9aef8941e0623.html
This is relatively reliable after my trial. Some documents have written environment variable configuration (content in the Cygwin home/Administrator/... bash_profile file ):

ANDROID_NDK_ROOT =/cygdrive/e/develop/android-NDK/android-ndk-

1.5 _ r1

ExportANDROID_NDK_ROOT

ANDROID_NDK_ROOT =/cygdrive/e/develop/android-NDK/android-ndk-1.5_r1
Export ANDROID_NDK
This is obviously incorrect. The names of environment variables in the previous section do not correspond to each other. You should know the names of environment variables that have been used in linux.

After the above configuration, we can use Eclipse to get started with NDK development:
It is also an online tutorial, coupled with self-correction and solutions to some problems encountered [post them one by one ]:

For personal information, refer to the following documents [installation skipped, please refer to the example section ]:
Http://blog.csdn.net/samguoyi/article/details/7410716
The installation method is basically OK, but the following problems are encountered during the process:
1. javah is not an internal command:
2. Run javah-jni. sam. you. MyJNI to report that the MyJNI class cannot be found;
3. When $ NDK/ndk-build is executed, *** target pattern contains no '%' is reported '. Stop
4. the jni folder in the Data Project is created by myself [at least I did this, and the new corresponding. c and Adnroid. mk is put in, and Cygwin terminal is used later to execute $ NDK/ndk-build generation. so needs to enter this Directory ];
5. The c file Unresolved compression under android NDK jni?
6. [Supplement 1] After the above operations, there is no problem in compiling the c/c ++ project in Eclipse, but the count <"hello" <endl; cannot see the output after execution?
Solution:
Solution 1: The JAVA_HOME environment variable must not contain semicolons after the previous one, similar to C: \ Program Files \ Java \ jdk1.7.0 _ 15. [I used C: \ Program Files \ Java \ jdk1.7.0 _ 15;] This is a tragedy. Remember to switch it to cmd and run javah or javac. When you see many parameter prompts, you will be OK!

Solution 2: Enter the bin directory of the project based on the appeal information, and then execute it. I cannot do it here. Then a buddy said, go to the src directory and execute it, sure enough, and in ..... JNITest \ src is generated under. h file, this android project can see [not refresh ];

Solution 3: Some commands executed earlier fail, so .... xx under JNITest \ obj \ local \ armeabi \ objs \ hellojni. o. d. The file is corrupted. Therefore, you only need to delete the files in the directory and execute $ NDK/ndk-build again.
Solution 5:
[Callback. The solution is to add the include directory containing the file as the new linked folder to the project. The specific method is as follows:
1. Right-click the project and choose New> Folder.
2. Click Advanced in the dialog box.

3. Select Link to alternate location (Linked Folder) and select the required include directory.

4. Refresh the project after finishing and solve the problem.

The include directory can be found in the ndk installation directory.

For example,/android-ndk-r7b/platforms/android-8/arch-arm/usr/include

Project sharing: http://pan.baidu.com/share/link? Consumer id = 1124395894 & uk = 3927989065

Solution 6 [Thank you for sharing ]:
Http://blog.csdn.net/johnpub/article/details/9246887
1. Add C: \ MinGW \ bin to the PATH. Remember to add a semicolon if there are other variables in it. The semicolon must be entered in the English input mode.
2. Create a New LIBRARY_PATH variable. If yes, add C: \ MinGW \ lib to the value, which is the location of the standard library.
3. Create the c_include_path variable and set it to C: \ MinGW \ include.
4. Create the CPLUS_INCLUDE_PATH variable. The value is
C: \ MinGW \ include \ c ++ \ 3.4.5; C: \ MinGW \ include \ c ++ \ 3.4.5; C: \ MinGW \ include \ c ++ \ 3.4.5 \ backward; C: \ MinGW \ include

Or [My MinGW directory structure is like this]
1. Add MINGW_HOME with the value C: \ MinGW.
2. Add C_INCLUDE_PATH with the value % MINGW_HOME % \ include.
3. Add CPLUS_INCLUDE_PATH. The value is
% MINGW_HOME % \ lib \ gcc; % MINGW_HOME % \ lib \ gcc \ mingw32; % MINGW_HOME % \ lib \ gcc \ mingw32 \ 4.7.2; % MINGW_HOME % \ lib \ gcc \ mingw32 \ 4.7.2 \ include; % MINGW_HOME % \ lib \ gcc \ mingw32 \ 4.7.2 \ include \ c ++; % MINGW_HOME % \ lib \ gcc \ mingw32 \ 4.7.2 \ include \ c ++ \ backward; % MINGW_HOME % \ lib \ gcc \ mingw32 \ 4.7.2 \ include \ c ++ \ mingw32;
4. Add path value: % MINGW_HOME % \ bin;

In fact, windows is quite troublesome. After these attempts, I think of the company's software architecture, which was developed under ubuntu. Many of them are well-prepared command scripts, one command can call all scripts. so library generation, and then generate the apk.

In fact, after this is done, we will understand how android calls the. so library generated by c/c ++. After that, remember to meditate again ......

Software architecture-the architect's ability to analyze and solve problems is more important. Of course, there are also leadership skills. [click here ]!

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.