Android NDK Development Summary

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>I. Installation configuration Environment</strong></p></p><p><p>1. Install android Studio, download path https://developer.android.com/studio/index.html?hl=zh-cn. I am downloading the Windows 64-bit integrated Android SDK version https://dl.google.com/dl/android/studio/install/2.3.2.0/ android-studio-bundle-162.3934792-windows.exe?hl=zh-cn, do not install the Android SDK separately. Because the company supports access to google, it is convenient to Download.</p></p><p><p>2. After installation, download the CMAKE,LLDB and NDK tools</p></p><p><p></p></p><p><p>3. If you want to use GIT version control, you need to download the installation package separately, the official website address https://git-scm.com/, set up the path in Android studio after installation</p></p><p><p></p></p><p><p>You can then specify the download path to get the code, such as Kotlin's code warehouse</p></p><p><p></p></p><p><p><strong>Two. analysis of the new NDK project</strong></p></p><p><p>First create a new project, select include C++support</p></p><p><p></p></p><p><p>Open the project with an example of a C + + output string</p></p><p>The <p> path is in D:\Project\MyApplication\app\src\main\cpp\native-lib.cpp, and the code is as follows </p> </p><pre><pre> #include <jni.h><span style="color: #000000"> #include </span> <<span style="color: #0000ff">string </span> ><span style="color: #0000ff">extern </span> <span style="color: #800000"> "</span> <span style="color: #800000">c </span> <span style="color: #800000"> "</span> <span style="color: #000000">jniexport jstring jnicalljava_com_example_myapplication_mainactivity_stringfromjni ( JNIEnv </span> *<span style="color: #000000">env, jobject </span> <span style="color: #008000">/* </span> <span style="color: #008000"> This </span> <span style="color: #008000">*/</span> <span style="color: #000000">) {std:: </span> Span style= "color: #0000ff" >string hello = <span style="color: #800000"> "</span> <span style="color: #800000">hello from C + + </span> <span style="color: #800000"> "</span> <span style="color: #000000">; </span> <span style="color: #0000ff">return </span> env-><span style="color: #000000">newstringutf (hello.c_str ());}</span></pre></pre><p><p>The called file is mainactivity.java, and the path is D:\Project\MyApplication\app\src\main\java\com\example\myapplication\ mainactivity.java, the contents are as follows</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"></span> package</span><span style="color: #000000"><span style="color: #000000">com.example.myapplication;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.support.v7.app.AppCompatActivity;</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.widget.TextView;</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">appcompatactivity {</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">used to load the ' Native-lib ' Library on application Startup.</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>"native-lib"<span style="color: #000000"><span style="color: #000000">); } @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: #0000ff"><span style="color: #0000ff">Super</span></span><span style="color: #000000"><span style="color: #000000">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Example of a call to a native method</span></span>TextView TV =<span style="color: #000000"><span style="color: #000000">(TextView) Findviewbyid (r.id.sample_text); Tv.settext (stringfromjni ()); } </span></span><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* A Native method That's implemented by the ' native-lib ' native library, * which are packaged with this appli Cation. </span></span><span style="color: #008000"><span style="color: #008000">*/</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 stringfromjni ();}</span></span></pre><p><p>Press shift+f9, debug run, Select Real Machine</p></p><p><p></p></p><p><p>On the phone side output is as follows</p></p><p><p></p></p><p><p>The default for this C + + code is compiled with cmake, CMakeLists.txt path in D:\Project\MyApplication\app\CMakeLists.txt, the content is condensed as follows:</p></p><pre><pre>cmake_minimum_required (VERSION 3.4.1) add_library (# Sets the name of the Library. Native-lib # Sets the library as a shared library. SHARED # provides a relative path to your source file (s). Src/main/cpp/native-lib.cpp) find_library (# Sets the name of the the path Variable. Log-lib # Specifies the name of the NDK library That's want CMake to Locate. Log) target_link_libraries (# Specifies the target library. Native-lib # Links The target library to the log Library # included in the NDK. ${log-lib})</pre></pre><p><p>Among them, #代表行注释. It probably means that the library name to be compiled is native-lib, and the source code path is Src/main/cpp/native-lib.cpp</p></p><p><p>In the Module's gradle file (note that the Gradle file is not a project, the former path is D:\Project\MyApplication\app\build.gradle, the latter path is D:\Project\MyApplication\ Build.gradle) The default configuration is compiled with the CMake file</p></p><p><p></p></p><p><p><strong>Three. writing NDK programs in a cmake+gradle manner</strong></p></p><p><p>Next I try to imitate the example of using the NDK to write a C + +</p></p><p><p>Write a Java class that contains the native method, such as testnative.java, where the path is D:\Project\MyApplication\app\src\main\java\com\example\myapplication\ Testnative.java</p></p><p><p>The code is as Follows:</p></p><pre><pre><span style="color: #0000ff"></span> package <span style="color: #000000">com.example.myapplication;</span> <span style="color: #0000ff"></span> public <span style="color: #0000ff">class</span> <span style="color: #000000">testnative { </span><span style="color: #0000ff">static</span> <span style="color: #000000"> { system.loadlibrary (</span>"nativelib"<span style="color: #000000">); } </span> <span style="color: #0000ff"></span> public <span style="color: #0000ff">Static</span> <span style="color: #0000ff">native</span> <span style="color: #000000">String getstr ();}</span></pre></pre><p><p>Where nativelib specifies the name of the library to be loaded by this class, I will generate this library later, the method must be decorated with native, and in order to not define the object, declare this method as static type, so that you can directly use the TESTNATIVE.GETSTR () Access in such a way.</p></p><p><p>Next need to generate the corresponding library, the first to write C + + files, There are two ways, one is to use javah, Generate header files, the corresponding header file to write the source code, the other is based on the format of the JNI generated function to write a CPP manually, without a header file, first in the implementation of the First:</p></p><p><p>Enter CMD using the console or the terminal terminal below Android studio, first CD to find the location of the current Testnative.java file</p></p><pre><pre>D:\PROJECT\MYAPPLICATION>CD d:\project\myapplication\app\src\main\java\com\example\myapplicationd:\project\ Myapplication\app\src\main\java\com\example\myapplication></pre></pre><p><p></p></p><p><p>Then enter Javac Testnative.java compile to generate the class file</p></p><pre><pre>D:\project\myapplication\app\src\main\java\com\example\myapplication>javac TestNative.javaD:\Project\ Myapplication\app\src\main\java\com\example\myapplication><br><br></pre></pre><p><p>A testnative.class file is generated under D:\Project\MyApplication\app\src\main\java\com\example\myapplication</p></p><p><p>And then the <span style="color: #ff0000">CD to the top package path, This is very important, is I repeatedly try to carry the package name of the class Java or Javah method</span></p></p><p><p><span style="color: #ff0000">Execute javah, parameter is class with package name</span></p></p><pre><pre>D:\PROJECT\MYAPPLICATION\APP\SRC\MAIN\JAVA\COM\EXAMPLE\MYAPPLICATION>CD D:\Project\MyApplication\app\src\ Main\javad:\project\myapplication\app\src\main\java>javah com.example.myapplication.testnatived:\project\ Myapplication\app\src\main\java></pre></pre><p><p>The com_example_myapplication_testnative.h file is generated under the D:\Project\MyApplication\app\src\main\java path with the following content</p></p><pre><span style="color: #008000"><span style="color: #008000">/*</span></span><span style="color: #008000">Do not <span style="color: #008000">EDIT This file-it</span> are machine generated</span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #000000"><span style="color: #000000">#include</span></span><jni.h><span style="color: #008000"><span style="color: #008000">/*</span></span><span style="color: #008000"><span style="color: #008000">Header for Class com_example_myapplication_testnative</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #000000"><span style="color: #000000">#ifndef _included_com_example_myapplication_testnative</span></span><span style="color: #0000ff"><span style="color: #0000ff">#define</span></span>_included_com_example_myapplication_testnative<span style="color: #000000"><span style="color: #000000">#ifdef __cplusplus</span></span><span style="color: #0000ff"><span style="color: #0000ff">extern</span></span> <span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">C</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000"> {</span></span><span style="color: #0000ff"><span style="color: #0000ff">#endif</span></span><span style="color: #008000"><span style="color: #008000">/*</span></span><span style="color: #008000"><span style="color: #008000">* class:com_example_myapplication_testnative * method:getstr * Signature: () ljava/lang/string;</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #000000"><span style="color: #000000">jniexport jstring jnicall java_com_example_myapplication_testnative_getstr (jnienv</span></span>*<span style="color: #000000"><span style="color: #000000">, jclass); #ifdef __cplusplus}</span></span><span style="color: #0000ff"><span style="color: #0000ff">#endif</span></span><span style="color: #0000ff"><span style="color: #0000ff">#endif</span></span></pre><p><p>Follow this declaration to write a cpp, such as TestNative.cpp, the path is in D:\Project\MyApplication\app\src\main\cpp\TestNative.cpp, contains the header file, the implementation can be</p></p><pre><pre><br>#include <string><br>#include ". /java/com_example_myapplication_testnative.h "<br><br>extern "C" {<br>Jniexport jstring Jnicall Java_com_example_myapplication_testnative_getstr<br> (jnienv * env, Jclass) {<br> std::string str = "My First Native Test";<br> return Env->newstringutf (str.c_str ());<br>}<br>}<br><br></pre></pre><p><p>The second is based on the characteristics of such a declaration, <span style="color: #ff0000">directly write a TestNative.cpp, note that the function naming must be consistent with the method of Javah generated header files</span> , carefully observe and remember on the LINE. This does not use Javac and Javah to manually generate class files and header files, but to include the jni.h header file, the code is as follows</p></p><pre><pre><br>#include <string><br>#include <jni.h><br><br>extern "C" {<br>Jniexport jstring Jnicall Java_com_example_myapplication_testnative_getstr<br> (jnienv * env, Jclass) {<br> std::string str = "My First Native Test";<br> return Env->newstringutf (str.c_str ());<br>}<br>}<br><br></pre></pre><p><p>Then modify the CMakeLists.txt file as follows</p></p><pre><pre>cmake_minimum_required (VERSION 3.4.1) add_library (nativelib SHARED src/main/cpp/testnative.cpp) find_ Library (log-lib log) target_link_libraries (nativelib ${log-lib})</pre></pre><p><p>Modify the file that calls Jni as follows</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"></span> package</span><span style="color: #000000"><span style="color: #000000">com.example.myapplication;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.support.v7.app.AppCompatActivity;</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.widget.TextView;</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">appcompatactivity {@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: #0000ff"><span style="color: #0000ff">Super</span></span><span style="color: #000000"><span style="color: #000000">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Example of a call to a native method</span></span>TextView TV =<span style="color: #000000"><span style="color: #000000">(TextView) Findviewbyid (r.id.sample_text); Tv.settext (testnative.getstr ()); }}</span></span></pre><p><p>Run the program on the phone output</p></p><p><p></p></p><p><p>In CMake mode, various platform versions of so files are generated under path D:\Project\MyApplication\app\build\intermediates\cmake\debug\obj, such as libnativelib.so in armeabi, Note that the name of the build library is in front of the set library name with Lib</p></p><p><p><strong>Three. writing NDK programs in a ndk-build+gradle manner</strong></p></p><p><p>Compile as ndk-build, you need to build The. Mk file</p></p><p><p>On the basis of the previous project</p></p><p><p>1. first, to create a new JNI folder under D:\Project\MyApplication\app\src\main, Create a new android.mk file inside, The content is as follows</p></p><pre><pre>Local_path: = $ (call my-dir) include $ (clear_vars) local_module = nativeliblocal_src_files: =. /cpp/testnative.cpplocal_ldlibs: =-landroid-llog-latomicinclude $ (build_shared_library)</pre></pre><p><p>The general meaning is to specify the name of the target library, the path of the source code, you can compare CMakeLists.txt.</p></p><p><p>2. <span style="color: #000000">next, in the android.mk of the same sibling directory, the establishment of the APPLICATION.MK file, the contents are as follows</span></p></p><pre><pre>App_stl: = c++_static</pre></pre><p><p>That is, the use of C + + version, specifically refer to Https://developer.android.com/ndk/guides/cpp-support.html#runtimes</p></p><p><p><span style="color: #ff0000">Note that when using C + + to create a new application.mk file, write a C + + version, write directly in the ANDROID.MK no, otherwise it will prompt the STL library can not find</span></p></p><p><p>3. Modify Gradle to compile the ndkbuild, the content is as follows</p></p><p><p></p></p><p><p>In a ndk-build way, various platform versions of so files are generated under path D:\Project\MyApplication\app\build\intermediates\ndkBuild\debug\obj\local\</p></p><p><p><strong>Four. set the library for the specified platform version</strong></p></p><p><p>1. Clear the folder under D:\Project\MyApplication\app\build</p></p><p><p>2. Add the following in the Module's build.gradle file:</p></p><p><p></p></p><p><p>It will only generate Libnativelib.so.</p></p><p><p></p></p><p><p>Android NDK Development Summary</p></p></span>

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.