second, the Android ndk development---learn from Hello Word
Source: Internet
Author: User
<span id="Label3"></p>NDK Directory Brief Introduction before NDK development, we have to be familiar with what is included in the NDK directory, and what is the role of these things for development? Now open the Ndk's unzip directory and look at the files in the extracted directory:<p><p>1)<strong>Samples Directory. </strong>This directory contains some of the small examples Google has written for NDK development, including local jni development, image processing, Multiple library file development, and so on, these examples are small but all-encompassing, can understand the samples directory of small example programs, then for the NDK development, it is very good to deal with.</p></p><p><p>2)<strong>Docs directory. </strong>This directory is all about Google's documentation for developers, and it's important to guide developers on how to develop NDK in an Android Environment.</p></p><p><p>3)<strong>Sources Directory. </strong>because Android is an open-source operating system, as part of Android's ndk, is also open source, This directory is stored in the NDK source Code.</p></p><p><p>4)<strong>Platforms Directory. </strong>It is stored in the current NDK version of all supported Android platform version, do the NDK development of C code can also be specified by a specific version of the platform to compile, the platforms directory is a different version contains C library files and header files, There are some minor changes in different versions.</p></p><p><p>5)<strong>prebuilt directory. </strong>This is a set of tools available for developing NDK programs under Windows.</p></p><p><p>6)<strong>Build Directory. </strong>It contains a large number of Linux programming scripts and batch files under Windows to accomplish cross-compilation in NDK Development.</p></p>Specific development 1,NDK Development steps<p><p>first, Let me list the simple steps of NDK development, and then outline it, using an example of Hello world to describe the NDK development:</p></p><p><p>(1) Create an Android project</p></p><p><p>(2) Write declaration in Java code native method public native String Hellofromjni ();</p></p><p><p>(3) Create the Jni directory, write C code, the method name to correspond in C code import jni.h header file</p></p><p><p>(4) Writing android.mk files</p></p><p><p>(5) NDK compilation generates a dynamic library</p></p><p><p>(6) Java code load dynamic library. calling native code</p></p>2,NDK Development Specific Practice<p><p>Follow the steps outlined above to establish a HelloWorld small case to step-by-step implementation of NDK development</p></p>1, Create an Android project, and declare a native method in the Java code:<pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.example.ndkdemo01;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.Activity;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Bundle;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View.OnClickListener;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.Button;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.Toast;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">{system.loadlibrary (</span></span>"Hello"<span style="color: #000000;"><span style="color: #000000;">); System.loadlibrary (string file name), which is the method used to load the dynamic library, where the parameter type is a string and the parameter is the name Local_module defined in the Android.mk file. }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Button bt_click;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">native</span></span><span style="color: #000000;"><span style="color: #000000;">String Javafromjni (); Declares the native method @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Bt_click</span></span>= (Button)<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">. Findviewbyid (r.id.bt_click); Bt_click.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>Toast.maketext (mainactivity.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">, Javafromjni (), toast.length_short). show (); } }); }}</span></span></pre><p><p></p></p>2. Create the JNI directory and write the hello.c file. Let's start with the simple format of the Jni Code: method signature Rules:<strong><strong>return value type Java_ Package Name _ Class name _native method name (jnienv* env, jobject obj)</strong></strong>1)<span style="font-family: 宋体; font-size: 14px;"><span style="font-family: 宋体; font-size: 14px;">Java: represents being called by Java. Package Name: represents the name of the packages (com_example_ndkdemo01), where the "." Be underlined instead. Mainactivity: declares the class name of the calling Function. Javafromjni: the declared function Name. </span></span><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: 宋体; font-size: 14px;">2)</span></span></span>The weird return value, in the ndk, whether it's passing values from java, or returning the return value of java, is a special parameter (such as the string in Java corresponding to the Ndk's jstring,int[] corresponding to jintarray, see jni.h).<p><p>3) more out of the transfer parameters, jnienv* env and Jobject thiz is the underlying function, must take the Parameters. The env developer uses this parameter for querying and transmitting data types (such as converting jintarray into an int array, which is explained in detail in a later chapter). Thiz represents the class object that calls this function, and this article is the object of Mainactivity.</p></p><pre><pre>#include <stdio.h><span style="color: #000000;">#include</span><jni.h><span style="color: #000000;">jstring java_com_example_ndkdemo01_mainactivity_ Javafromjni (jnienv</span>*<span style="color: #000000;"> env, jobject obj) { </span><span style="color: #0000ff;">return</span> (* (*env)). Newstringutf (env, "hello jni!" <span style="color: #000000;">);}</span></pre></pre>3. Write the Android.mk file<p><p>How do you write this android.mk file? At this point we have to open the NDK document to see, location e:/ndk/android-ndk-r10d/docs/start_here.html, find</p></p><p><p></p></p><p><p>ok, Let's start by creating a android.mk file in the JNI directory, copy and paste the above paragraph, and change the Local_module and local_src_files to our own name:</p></p><p><p></p></p><pre><pre>Local_path: = $ (call my-<span style="color: #000000;">dir) include $ (clear_vars) local_module :</span>=<span style="color: #000000;"> Hello local_src_ FILES:</span>=<span style="color: #000000;"> hello.c include $ (build_shared_library)</span></pre></pre><p><p></p></p><pre><pre></pre></pre><p><p><strong>4,NDK Compiling a dynamic library</strong></p></p><p><p><strong>Click Projects in eclipse, right-click->>android tools->>add Native Support .... A dialog box pops up and the module name is entered in the input box. Can see the compilation passed, the following refresh the project, you can see the project Libs directory more than a libhello.so file</strong> , this is the Android knowledge of the dynamic Library.</p></p><p><p></p></p><p><p></p></p>5,java code load dynamic library. calling native code<p><p>After compiling this libhello.so file, you need to load The. so library file in Java code, the code is simple, and then toast to see the effect:</p></p><p><p>System.loadlibrary (string file name), which is the method used to load the dynamic library, where the parameter type is a string and the parameter is the name Local_module defined in the Android.mk file.</p></p><p><p>Reprinted from:<strong>http://blog.csdn.net/allen315410/article/details/41805719</strong></p></p><p><p>second, the Android ndk development---learn from Hello Word</p></p></span>
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