• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4<head>
5<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
6<meta http-equiv="X-UA-Compatible" content="IE=9"/>
7<meta name="generator" content="Doxygen 1.8.5"/>
8<title>NDK Programmer&#39;s Guide: hello-jni</title>
9<link href="tabs.css" rel="stylesheet" type="text/css"/>
10<script type="text/javascript" src="jquery.js"></script>
11<script type="text/javascript" src="dynsections.js"></script>
12<link href="navtree.css" rel="stylesheet" type="text/css"/>
13<script type="text/javascript" src="resize.js"></script>
14<script type="text/javascript" src="navtree.js"></script>
15<script type="text/javascript">
16  $(document).ready(initResizable);
17  $(window).load(resizeHeight);
18</script>
19<link href="doxygen.css" rel="stylesheet" type="text/css" />
20</head>
21<body>
22<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
23<div id="titlearea">
24<table cellspacing="0" cellpadding="0">
25 <tbody>
26 <tr style="height: 56px;">
27  <td style="padding-left: 0.5em;">
28   <div id="projectname">NDK Programmer&#39;s Guide
29   </div>
30  </td>
31 </tr>
32 </tbody>
33</table>
34</div>
35<!-- end header part -->
36<!-- Generated by Doxygen 1.8.5 -->
37</div><!-- top -->
38<div id="side-nav" class="ui-resizable side-nav-resizable">
39  <div id="nav-tree">
40    <div id="nav-tree-contents">
41      <div id="nav-sync" class="sync"></div>
42    </div>
43  </div>
44  <div id="splitbar" style="-moz-user-select:none;"
45       class="ui-resizable-handle">
46  </div>
47</div>
48<script type="text/javascript">
49$(document).ready(function(){initNavTree('md_2__samples_sample--hellojni.html','');});
50</script>
51<div id="doc-content">
52<div class="header">
53  <div class="headertitle">
54<div class="title">hello-jni </div>  </div>
55</div><!--header-->
56<div class="contents">
57<div class="textblock"><p>This sample provides a bare-bones look at a minimal
58application built with the NDK.</p>
59<h3>Android.mk</h3>
60<p>The following two lines provide the name of the native source file, along
61with the name of the shared library to build. The full name of the built
62library is <code>libhello-jni.so</code>, but you should omit the
63<code>lib</code> prefix and the <code>.so</code> extension.</p>
64<pre class="fragment">LOCAL_SRC_FILES := hello-jni.c
65LOCAL_MODULE    := hello-jni
66</pre><p>For more information on what this file does, and how to use it, see
67the <a
68href="./md_3__key__topics__building__chapter_1-section_8__android_8mk.html">Android.mk</a> section.</p>
69<h3>Application.mk</h3>
70<p>This line tells the build system the architecture against which to build. If
71you don't specify anything, the build system defaults to armeabi.</p>
72<pre class="fragment">APP_ABI := all
73</pre><p>For more information on what this file does, and how to use it, see
74the <a
75href="./md_3__key__topics__building__a_p_p_l_i_c_a_t_i_o_n-_m_k.html">Application.mk</a> section.</p>
76<h3>Java-side implementation</h3>
77<p>This file calls a function to retrieve a string from the native side, then
78displays it on the screen.</p>
79<p>The source code contains three lines of particular interest to the NDK user.
80They are presented here in the order in which they are used, rather than by
81line order.</p>
82<p>This function call loads the <code>.so</code> file upon application
83startup.</p>
84<pre class="fragment">System.loadLibrary("hello-jni");
85</pre><p>The <code>native</code> keyword in this method declaration tells the
86DVM that the function is in the shared library (i.e., implemented on the native
87side).</p>
88<pre class="fragment">public native String stringFromJNI();
89</pre><p>The Android framework calls the function loaded and declared in the
90previous steps, displaying the string on the screen.</p>
91<pre class="fragment">tv.setText( stringFromJNI() );
92</pre><h3>C-side implementation</h3>
93<p>This file contains a function that returns a string that the Java side
94requested (see "Java-side implementation"). The function declaration is as
95follows:</p>
96<pre class="fragment">jstring Java_com_example_hellojni_HelloJni_stringFromJNI(
97JNIEnv* env,
98                                              jobject x )
99</pre><p>This declaration corresponds to the native function declared in the
100Java source code. The return type, <code>jstring</code>, is a data type defined
101in the <a
102href="http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html
103">Java Native Interface Specification</a>. It is not actually a string, but a
104pointer to a Java string.</p>
105<p>After <code>jstring</code> comes the function name, which is based on the
106Java function name and and the path to the file containing it. Construct it
107according to the following rules:</p>
108<ul>
109<li>Prepend <code>Java_</code> to it.</li>
110<li>Describe the filepath relative to the top-level source directory.</li>
111<li>Use underscores in place of forward slashes.</li>
112<li>Omit the <code>.java</code> file extension.</li>
113<li>After the last underscore, append the function name.</li>
114</ul>
115<p>Thus, in this example, the function name
116<code>Java_com_example_hellojni_HelloJni_stringFromJNI</code> refers to a Java
117function called <code>stringFromJNI()</code>, which resides in
118<code>hellojni/src/com/example/hellojni/HelloJni.java</code>.</p>
119<p>Finally, <code>JNIEnv*</code> is the pointer to the VM, and
120<code>jobject</code> is a pointer to the implicit “this” object passed from
121the Java side.</p>
122<p>The following line calls the VM API (*env), and passes it a return value:
123that is, the string that the function on the Java side had requested. </p>
124<pre class="fragment">return (*env)-&gt;NewStringUTF(env, "Hello from JNI !
125Compiled with ABI " ABI "."); </pre> </div></div><!-- contents -->
126</div><!-- doc-content -->
127<!-- start footer part -->
128<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
129  <ul>
130    <li class="footer">Generated on Wed Jun 25 2014 00:51:19 for NDK
131Programmer&#39;s Guide by
132    <a href="http://www.doxygen.org/index.html">
133    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.5 </li>
134  </ul>
135</div>
136</body>
137</html>
138