JNI Invocation of Android

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: "times new roman", times">Android provides NDK development kits to provide C + + development for Android platforms to extend the functionality of the Android SDK. Mainly includes the Android NDK build system and the JNI implementation and the native code communication two Parts.</span></p></p><span style="font-family: "times new roman", times"><span style="font-family: "times new roman", times">first, The Android ndk build system</span></span><p><p>the <span style="font-family: "times new roman", times">Android NDK build system is based on the GNU Make. The Android GNU make system requires two files in addition to some internal GNU fragments: Android.mk and Application.mk. Android NDK source gives a lot of examples, take Hellojni as an example, android.mk source code:</span></p></p><pre><pre><span style="color: #008000; font-family: "times new roman", times">#Android. MK must start with the Local_path variable</span><br><span style="font-family: "times new roman", times">Local_path: = $ (call my-<span style="color: #000000">dir)<span style="color: #008000">#清除除了LOCAL_PATH以外的LOCAL_ <name> variables, such as Local_module and Local_src_files</span> Include $ (clear_vars)<span style="color: #008000">#每一个原生组件被称为一个模块</span>local_module :</span>= hello-<span style="color: #000000">jni<br><span style="color: #008000">#源文件</span> Local_src_files:</span>= hello-<span style="color: #000000">jni.c<span style="color: #008000">#编译为共享库, that is, the Suffix.</span>so include $ (build_shared_library)</span></span></pre></pre><p><p><span style="font-family: "times new roman", times">Application.mk source:</span></p></p><pre><pre><span style="font-family: "times new roman", times"> <span style="color: #008000">#一般选择APP_ABI: = armeabi-v7a is Enough.</span><br>App_abi: = All</span></pre></pre><p><p><span style="font-family: "times new roman", times">  </span><span style="font-family: "times new roman", times">In order to establish a module that can be used by the main application, the module must be transformed into a shared library. Following the necessary steps above, you can continue compiling multiple shared libraries.</span></p></p><span style="font-family: "times new roman", times"><span style="font-family: "times new roman", times">1.1 Building a static library</span></span><p><p><span style="font-family: "times new roman", times">Android can also compile a static library (suffix. a), but the actual Android application does not use the static library directly, and the application package does not contain a static library. Static libraries can be used to build shared Libraries. however, when a static library is connected to more than one shared library, the application package contains multiple copies of the static library, increasing the size of the application Package. In this case, instead of building a static library, you can build a common module as a shared library and dynamically connect dependent modules to eliminate duplicate Replicas. The following android.mk implements code sharing between shared Libraries. </span></p></p><pre><pre>Local_path: = $ (call my-<span style="color: #000000">dir)<span style="color: #008000">#第三方AVI库</span>include $ (clear_vars) local_module :</span>=<span style="color: #000000"> aviliblocal_src_ FILES:</span>=<span style="color: #000000"> avilib.cinclude $ (build_shared_library)<span style="color: #008000">#原生模块1</span>include $ (clear_vars) local_module : </span>=<span style="color: #000000"> module1local_src_files:</span>=<span style="color: #000000"> module1.clocal_shared_libraries:</span>=<span style="color: #000000"> Avilibinclude $ (build_ Shared_library)<span style="color: #008000">#原生模块2</span>include $ (clear_vars) local_module :</span>=<span style="color: #000000"> module2local_src_files: </span>=<span style="color: #000000"> module2.clocal_shared_libraries:</span>=<span style="color: #000000"> avilibinclude $ (build_shared_library)</span></pre></pre><span style="font-family: "times new roman", times"><span style="font-family: "times new roman", times">1.2 prebuilt Library</span></span><p><p><span style="font-family: "times new roman", times">The shared module compiles with the source code required, for this android provides the prebuilt library, the following occasions, The prebuilt library is very useful:</span></p></p> <ol> <ol> <li><span style="font-family: "times new roman", times">Want to publish your module to others without releasing the source code;</span></li> <li><span style="font-family: "times new roman", times">You want to use the pre-built version of the shared module to speed up the build process.</span></li> </ol> </ol><p><p><span style="font-family: "times new roman", times">Other build system Variables:</span></p></p><p><p><span style="font-family: "times new roman", times">Local_cflags: an optional set of compiler flags that are passed to the compiler when compiling C and C + + source files;</span></p></p><p><p><span style="font-family: "times new roman", times">Local_cpp_flags: an optional set of compiler flags that are passed to the compiler when compiling only C + + source files;</span></p></p><p><p><span style="font-family: "times new roman", times">Local_ldlibs: an optional list of link flags that are primarily used to transfer the list of system libraries to be dynamically linked. such as link log library:</span></p></p><p><p><span style="font-family: "times new roman", times">Local_ldflags + =-llog</span> <span style="font-family: "times new roman", times"> </span></p></p><p><p><span style="font-family: "times new roman", times">app_cppflags: compiler flags, which are passed to the compiler when compiling C + + source files for any module. </span><span style="font-family: "times new roman", times"> </span></p></p><p><p><span style="font-family: "times new roman", times">Nkd-build Script Command:</span></p></p><pre><pre>ndk-build–c/<span style="color: #000000">Project Pathndk</span>-<span style="color: #000000">build–bndk</span></pre></pre><span style="font-family: "times new roman", times"><span style="font-family: "times new roman", times">second, JNI Implementation and native code communication</span></span><span style="font-family: "times new roman", times"><span style="font-family: "times new roman", times">2.1/c + + header file Generator: javah</span></span><p><p><span style="font-family: "times new roman", times">The JDK comes with a command-line tool called javah, which generates the native function name and its argument list from the original definition of the Java class file, so programmers avoid writing redundant definitions. C + + source files only need to contain this header file and provide native method Implementations.</span></p></p><p><p><span style="font-family: "times new roman", times">The Javah parameter list is as Follows:</span></p></p><pre><span style="font-family: "times new roman", times">C:\users\jiayayao><span style="color: #000000"><span style="color: #000000">javah usage: Javah [options]</span></span><classes><span style="color: #000000"><span style="color: #000000">among them, [options] include:</span></span>-o <file> output file (only with-d or-<span style="color: #000000"><span style="color: #000000">o one)</span></span>-d <dir><span style="color: #000000"><span style="color: #000000">Output Directory</span></span>-v-<span style="color: #000000"><span style="color: #000000">verbose enable verbose output</span></span>-h--help-?<span style="color: #000000"><span style="color: #000000">Output This message</span></span>-<span style="color: #000000"><span style="color: #000000">version output release information</span></span>-<span style="color: #000000"><span style="color: #000000">JNI generates a Jni-style header file (default)</span></span>-<span style="color: #000000">force <span style="color: #000000">always writes to the output file</span></span>-classpath <path><span style="color: #000000"><span style="color: #000000">the path from which to load the class</span></span>-CP <path><span style="color: #000000"><span style="color: #000000">the path from which to load the class</span></span>-bootclasspath <path><span style="color: #000000"><span style="color: #000000">the path from which to load the boot class</span></span><classes><span style="color: #000000"><span style="color: #000000">is specified using its fully qualified name (for example, java.lang.Object). </span></span></span></pre><p><p><span style="font-family: "times new roman", times">The command-line arguments for generating the header file are as Follows:</span></p></p><pre><pre>Javah-classpath bin/classes Com.example.hellojni.Hellojni</pre></pre><p><p>the <span style="font-family: "times new roman", times">Java layer Code is as Follows:</span></p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span>Hellojni<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: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000">Called when the activity is first <span style="color: #008000">Created.</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #000000"><span style="color: #000000">@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">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);</span></span><span style="color: #000000"><span style="color: #000000">TextView TV</span></span>=<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>TextView (<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">); Tv.settext (stringfromjni ()); Setcontentview (tv); } </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 * ' hello-jni ' native library, which are packaged * with this applic ation. </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><span style="color: #008000"><span style="color: #008000">/*</span></span><span style="color: #008000">This is <span style="color: #008000">another native method declaration, which is *not* * implemented by ' Hello-jni '. This was simply to show that * you can declare as many native methods in your Java code * as you want, their implem entation is searched in the * currently loaded native libraries only the first time * you are call Them. * * Trying to call this function would result in a * java.lang.UnsatisfiedLinkError exception! </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 Unimplementedstringfromjni (); </span></span><span style="color: #008000"><span style="color: #008000">/*</span></span><span style="color: #008000">This was <span style="color: #008000">used to load the ' Hello-jni ' Library on application * STARTUP. The library has already been unpacked to */data/data/com.example.hellojni/lib/libhello-jni.so at * installation Time by the package Manager. </span></span><span style="color: #008000"><span style="color: #008000">*/</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-jni"<span style="color: #000000"><span style="color: #000000">); }}</span></span></pre><p><p><span style="font-family: "times new roman", times">  </span> <span style="font-family: "times new roman", times">The Java method Stringfromjni takes no parameters, but the native method takes two parameters:</span></p></p><pre><pre><span style="color: #000000">jstringjava_com_example_hellojni_hellojni_stringfromjni (jnienv</span>*<span style="color: #000000"> env, jobject thiz) { </span> <span style="color: #0000ff">return</span> (*env)->newstringutf (env, "Hello from JNI!") <span style="color: #000000">);}</span></pre></pre><p><p>  the <span style="font-family: "times new roman", times">First parameter, jnienv, is an excuse pointer to the available JNI function table, and the second parameter, jobjects, is a Java object reference to the Hellojni class Instance. </span><span style="font-family: "times new roman", times"> </span></p></p><p><p><span style="font-family: "times new roman", times">Note here that C is slightly different from C + + code and the C code is as Follows:</span></p></p><p><p><span style="font-family: "times new roman", times">Return (*env)->newstringutf (env, "Hello from JNI!");</span></p></p><p><p><span style="font-family: "times new roman", times">The C + + code is as Follows:</span></p></p><p><p><span style="font-family: "times new roman", times">Return Env->newstringutf ("Hello from JNI!");</span></p></p><p><p><span style="font-family: "times new roman", times">this is because in C + + code, jnienv is actually a C + + class instance, and the JNI function exists as a member function, so the JNI method call does not require the JNIENV instance to be a parameter. </span><span style="font-family: "times new roman", times"> </span><span style="font-family: "times new roman", times"> </span><span style="font-family: "times new roman", times"> </span></p></p><span style="font-family: "times new roman", times"><span style="font-family: "times new roman", times">2.2 Data Types</span></span><p><p><span style="font-family: "times new roman", times">Java has two types of data: the base data type and the reference data type: the mapping of java/jni/c++ in the base data type is as Follows:</span></p></p><p style="text-align: center"><p style="text-align: center"><span style="font-family: "times new roman", times"></span></p></p><p><p><span style="font-family: "times new roman", times"></span> <span style="font-family: "times new roman", times">The type mapping relationships for reference types are as Follows:</span> <span style="font-family: "times new roman", times"> </span></p></p><p style="text-align: center"><p style="text-align: center"><span style="font-family: "times new roman", times"></span></p></p><p><p><span style="font-family: "times new roman", times">  reference <span style="color: #ff0000">types are passed to native code in an opaque way, rather than as native data types, so reference types cannot be used and modified directly. </span>JNI provides a set of APIs that are closely related to these reference types.</span></p></p><p><p><span style="font-family: "times new roman", times">  </span><span style="font-family: "times new roman", times">String manipulation:</span></p></p><p><p></p></p><pre><pre> <span style="color: #008000">//</span> <span style="color: #008000"> create string </span> <span style="color: #000000"> jstring javastring;javastring </span> = (*env)->newstringutf (env, <span style="color: #800000"> "</span> <span style="color: #800000">hello world! </span> <span style="color: #800000"> "</span> <span style="color: #000000" ); < span> <span style="color: #008000">//</span> <span style="color: #008000"> When memory overflows, NULL is returned, notice the empty </span> <span style="color: #008000">//</span> <span style="color: #008000" to convert a java string c < span> <span style="color: #0000ff">const </span> jbyte*<span style="color: #000000"> Str;jboolean iscopy;str </span> = (* Env)->getstringutfchars (env, javastring, &<span style="color: #000000">iscopy); </span> <span style="color: #0000ff">if </span> (<span style="color: #800080">0 </span>!=<span style="color: #000000"> Str) {} </span> <span style="color: #008000">//</span> <span style="color: #008000"> releases the string </span> (*env)->releasestringutfchars (env, javastring, str); </span></span></pre></pre><p><p></p></p><p><p></p></p><p><p></p></p> <ol> <ol> <li><span style="font-family: "times new roman", times">Array manipulation</span></li> </ol> </ol><p><p>the <span style="font-family: "times new roman", times">get<type>arrayregion function copies the given base Java array into a given C array, such as a program:</span> <span style="font-family: "times new roman", times"> </span></p></p><p><p><span style="font-family: "times new roman", times">to commit changes from a C array to a Java array, such as a program:</span> <span style="font-family: "times new roman", times"> </span></p></p><p><p><span style="font-family: "times new roman", times"><strong>The memory allocation of the native method exceeds the management scope of the virtual machine, and the memory in the native method cannot be reclaimed by the virtual Machine's garbage Collector.</strong></span></p></p><p><p><span style="font-family: "times new roman", times">Native code back to Java lossy performance, It is recommended to pass all required parameters to the native code call instead of having the native code return to Java.</span></p></p><p><p><span style="font-family: "times new roman", times"></span></p></p><p><p><span style="font-family: "times new roman", times"></span></p></p><p><p><span style="font-family: "times new roman", times">Getstringutfchars</span></p></p><p><p><span style="font-family: "times new roman", times">Releasestringutfchars</span></p></p><p><p>JNI Invocation of Android</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.