Speaking of these four words, which surprised 1/2 people all over the world, said the poor saying: "Is it just a patent for the Disabled ". To prevent harm to the human world, we are prepared to torch the book. At this moment, a small breeze blew through, and I saw the lower part of the book page missing out, I saw the complete writing above, "to practice the magic, you only need to work hard ". Day does not let me, the poor said Yang Tian Long smile.
In other words, welfare is about to be paid off. While the poor side hang the song "today is a good weather", he began to work with his sleeves.
First, create an Android project named TestNDK through Eclipse. This project will be used to demonstrate NDK development in the future (for details and directory structure, see Figure 1 ). For NDK development, no additional settings are required when creating a project. Same as other common projects.
Figure 1
2. Here we will give a simple example to help you have a simple and intuitive understanding of NDK. This example shows how to return a string from the underlying layer and display it on the interface.
In MainActivity. java, declare that JNI calls the stringFromJNI function. Its function is to return a string from the underlying layer. Note that the native keyword should be added to indicate that the function is called through JNI. At the same time, we also noticed that in order to call stringFromJNI, we also need to load the module containing the stringFromJNI function, so in the MainActivity class, we need to use the static call module of the System. loadLibrary function. We name this underlying module ndktest.
package com.example.testndk;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); String str = stringFromJNI(); tv.setText( str ); setContentView(tv); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private native String stringFromJNI(); static{ System.loadLibrary("ndktest"); }}
3. Create a jni directory under the project directory (in this article, d: \ workspace \ TestNDK, as shown in Figure 1 ).
4. In the jni directory, create a c file named ndktest. c. At this time, the crowd began to cheer, and finally do not have to look at the face of the VM, in order to express their joy, wrote the following test code to remember. On this day, the poor path found a bit of discord.
A. the first is the weird function name. In NDK, the Native Code function name has certain rules, that is, the Java + package name + The called java file name + the declared function name, ". "It is underlined.
So in this example, the function name Java_com_example_testndk_MainActivity_stringFromJNI,
Java: indicates that it is called by Java.
Com_example_testndk: indicates the package name (com. example. testndk), where "." is replaced by an underscore.
MainActivity: declare the name of the class that calls the function.
StringFromJNI: the declared function name.
B. weird return values. In NDK, both passing values from Java and returning Java return values are specialized parameters (for example, the string in java corresponds to the jstring of ndk, int [] corresponds to jintarray. For details, see jni. h ).
C. Multiple passing parameters. JNIEnv * env and jobject thiz are underlying functions and must be included. Env developers use this parameter to query and pass data types (for example, converting jintarray to an int array will be described in later sections ). Thiz indicates the class object that calls this function. In this article, it is the MainActivity object.
#include <string.h>#include <jni.h>jstringJava_com_example_testndk_MainActivity_stringFromJNI( JNIEnv* env, jobject thiz ){ return (*env)->NewStringUTF(env, "Hello from JNI ! We are free !");}
5. create Android in the jni directory. mk, in which LOCAL_MODULE sets the module name (same as MainActivty above. the names of the modules loaded in java are the same.) LOCAL_SRC_FILES sets the file name to be compiled.
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := ndktestLOCAL_SRC_FILES := ndktest.cinclude $(BUILD_SHARED_LIBRARY)
4. Enter Command mode in Windwos, enter the project directory, and enter ndk-build (see figure 2 ). After compilation, the armeabi directory is automatically generated in the libs directory of the project directory, which contains the underlying module libndktest. so (in this article, d: \ workspace \ TestNDK \ libs \ armeabi \ libndktest. so ). This file is automatically inserted into the installation package during Eclipse compilation.
Figure 2
Everything is ready. The poor road rubs his beard, and the dust shakes on the running key. In the sound of fairy music, the following results are obtained.
Suddenly, the camera thundered. Many C/C ++ people burst into tears, dancing and singing "turning the slaves and singing ". It is hoped that the Community will comment on "another great victory for human rights after the elimination of South Africa's racial isolation". In a piece of worship, the poor waved and said, "comrades have worked hard ". While waiting for everyone to respond, I don't know where a dead fat man came from. I had two horns hanging on my head, wearing a furry wool coat and a green grassland cake on my mouth. I asked with milk, "the command line is too tired. Is there any easier way? ". Poor Tao suddenly looked at the black line, but in order to maintain a elegant image, poor Tao showed a sun-like smile, shot the head of the fat man: "The Lazy goat asked a very good question." At this time, I saw a 4.0 degree of difficulty in the poor road, and then rolled back to the podium, clicked the Eclipse interface, and then zookeeper opened the menu on the TestNDK project, click Android Tools-> Add Native Support... (See figure 3 ). A dialog box pops up. In the input box, enter the module name, and then follow the Steps 3 and 4 to develop the module. At last, when we ran the key, another miracle came into being. We saw that the underlying module was automatically compiled by Eclipse, and we no longer needed to manually enter the command line. At this time, the lazy goat and goat opened their mouths with a big mouthful of saliva falling on the poor keyboard. Cool
Figure 3
The audience once again stood up and applauded. Many C/C ++ people held up the portrait of the poor path and became loyal fans of the poor path. In this crowded moment, a trace of glass light across, a tender and calm voice sounded: "This is all the truth "?
Please wait for the next time to resolve the issue.