The establishment and development process of NDK in the development environment under windows, and the establishment of ndk
Android applications are often used in both games and common applications. so is the dynamic link library. what is so and what are the benefits? You can check it online and I will not explain it too much .. So is a file type in linux, so Compiling. so must be in linux. How can we compile it in win? You can search for the tutorials on the Internet. The tutorials are also varied, uncertain, and have no skills. Even when you look at the tutorials, it is estimated that they are still successful, not to mention beginners, based on some examples on the internet, I also summarized a simple one. so compilation method: GNUstep (there are a lot of cygwin online, you can also study ).
Go to the topic, win and set up the. so development environment: (because resources are not easy to find on the Internet, I put all the required tools into the Network Disk)
1. NDK (this NDK version is 10): Link: http://pan.baidu.com/s/1bn1xDQB password: lr7d
Http://pan.baidu.com/s/1ntLwd53 password: kuuj
After the download and installation are complete (the installation path is the default ):
First, import the path of the NDK in eclipse (the path after decompression)
Then, on drive C (my installation path is on drive C), find the file GNUstep. conf and enable it to configure the environment variables:
Add or modify the last two lines of the file: (Note the path of your NDK)
NDK=E:/baiyuliang/android-ndk-r10export=NDK
Open GNUstep, enter make-v, enter (enter key), and then enter gcc-v, as shown in symptoms:
Enter cd $ NDK to see the symptoms again:
Indicates that the development environment has been set up successfully!
Now let's take a simple example to learn how to use GNUstep for NDK development:
Create a project in eclipse with the package name: com. byl. testjni and main class name MainActivity (this should be clear and you will be asked to enter their path and name correctly ):
After the creation, open cmd and enter the javah command to run:
If the command is displayed, the operation goes smoothly. If the command cannot be found, check whether your jdk environment variable is correctly configured.
Next, locate the path in the hard drive of the src folder in the project:
Open MainActivity in the project and add the following code:
Public native String hello (); // defines the method name static {System. loadLibrary ("testJni") in. so; // defines the name of the generated. so}
You need to write this information in advance. in so Activity, the method name defined in the header file will be automatically generated during compilation, and the last generated. so name, then enter the following command in cmd to generate the c header file:
javah -d ../jni com.byl.testjni.MainActivity
Do not write an error in the path. If no prompt is displayed after confirmation, the execution is successful. In eclipse, right-click to refresh the project:
Create a c file in the jni folder named ". so", that is, testJni. c:
Next, write the Android. mk file in the jni folder. The Android. mk file can be found in samples/hello-jni/jni in the ndk installation path and copied directly:
Open Android. mk and modify LOCAL_MODULE and LOCAL_SRC_FILES:
LOCAL_MODULE refers to the description module, which is used to call the module name of java. The corresponding libtestJni. so will be generated;
LOCAL_SRC_FILES is the source file. Multiple files are separated by spaces;
The key step is to open GNUstep and start to compile and generate the. so file:
1. Enter the command to locate the project (TestJni) Location:
cd /E/AndroidProject/TestJni
2. Enter the command to generate the testJni. so file:
$NDK/ndk-build
, Indicates that the operation is successful. Refresh the project and check whether the testJni. so file under libs has been quietly lying there? (Note: A lib (libtestJni. so) will be added before the generated file by default)
The following is the time to witness the miracle:
Open MainActivity. In the Oncreate method, call the hello () method and print it. If "HelloJni!" is displayed! "That is, the. so file is generated and called successfully!
Log.e("jj", hello());
Run LogCat:
OK! Perfect run!
After that, you can put some important variables or service addresses in. so, and then put the generated. so into the project!
PS: This example is very simple, that is, printing a string, which is far from the complicated methods to be compiled in actual application development. Therefore, it is still hard to learn how to develop NDK!
Note:
Some children's shoes may be generated in this example. if so is directly used in other projects, the call method fails. Here, let's talk about compilation. so, the Activity path used must be called with the project. the Activity paths of the methods in so are consistent. For example, AActivity in Project A must call one. so, you can directly generate in this project. so, if you create another project, such as Project B, to generate. so, the Activity path in Project B should be exactly the same as that in Project:
Project A: com. byl. testjni. AActivity,
Project B must also be com. byl. testjni. AActivity.
Demo Source: http://download.csdn.net/detail/baiyuliang2013/8505977