In the previous blog post, we set up the Android NDK development environment, as a code farm, we are not first to write a HelloWorld to express their arrival.
(1) First build an Android project in your workspace, here I built for Ndkhelloworld. Project Structure (1)
、
Figure (1)
(2) in mainactivity, declare the local method you need, and the Mainactivity code is as follows:
Package Com.gc.ndkhelloworld;import Android.app.activity;import Android.os.bundle;import android.widget.TextView;/ * * * @author Android General * */public Class Mainactivity extends Activity {private TextView mtextview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mtextview= (TextView) Findviewbyid (r.id.tv); Mtextview.settext (HelloWorld ()); } /** * Declare local methods in Java code * @return * /public native String HelloWorld ();}
(2) Select the item, right-click to select Android Tools, and then select Add Native Support ..., (2) as shown:
Figure (2)
Then the situation shown in figure (3) appears:
Figure (3)
After clicking Finish, you will find the project's directory structure (4) as shown:
Figure (4)
Here we start implementing the local method, the Helloworld.cpp code is as follows:
#include <jni.h> #include <stdio.h>/** * @author Android General *//** * * When implementing a local method, one is named according to the function naming rules, which are named as follows: * Java_ Packagename_classname_methodname * from this rule, it can be seen that any NDK function name called by a Java program must begin with Java, followed by the package names (in the middle of the Java Class) where the function is to be called. To "_"), and then the Java class name, and finally the method name, the parts are directly connected with "_". */extern "C" {jstring java_com_gc_ndkhelloworld_mainactivity_helloworld (jnienv *env,jobject obj) {return env-> Newstringutf ("ndk,helloworld!");}}
After implementing the local method, we need to compile the local library, select the project, right-click Select Build Project to compile the local library, after compiling the local library, you need to load the local library. Calling System.loadlibrary to load a local library within a static initialization block is the simplest way to load the library, and the code for loading the library in this example looks like this:
static {system.loadlibrary ("HelloWorld");}
After compiling the local library, you can find a Armeabi directory in the Libs directory because we did not specify a application.mk file for the project, and the Android NDK build system generates binary files for the Armeabi Abi by default. For our local libraries to run on all the architectures supported by the NDK, we need to add a application.mk file in the JNI directory with the following file contents:
App_abi: = All
If all goes well, you can see the results of this case in the simulator or on the real machine as follows:
If you encounter could not being resolved or unresolved inclusion when implementing the Local method: <jni.h>, please press resolve:
The case Demo:demo upload csdn download channel, but how also can not brush out the address, and so on after the address, then the address update.
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/44899927
Android Ndk writes a HelloWorld