<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Start IT up &#187; Kamboj</title>
	<atom:link href="http://www.techjini.com/blog/author/kamboj/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techjini.com/blog</link>
	<description>The blog of TechJini Solutions</description>
	<lastBuildDate>Sat, 31 Jul 2010 13:31:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Android NDK &#8211; an introduction (How to work with NDK)</title>
		<link>http://www.techjini.com/blog/2009/10/26/android-ndk-an-introduction-how-to-work-with-ndk/</link>
		<comments>http://www.techjini.com/blog/2009/10/26/android-ndk-an-introduction-how-to-work-with-ndk/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 05:49:46 +0000</pubDate>
		<dc:creator>Kamboj</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.techjini.com/blog/2009/10/26/android-ndk-an-introduction-how-to-work-with-ndk/</guid>
		<description><![CDATA[ 	
What is the Android NDK?
The Android NDK is a companion tool used only in conjunction with Android SDK which<a href="http://www.techjini.com/blog/2009/10/26/android-ndk-an-introduction-how-to-work-with-ndk/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<p><title></title> 	<!-- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } 		H2 { margin-bottom: 0.08in } 		H2.western { font-family: "Nimbus Roman No9 L", "Times New Roman", serif } 		H2.cjk { font-family: "DejaVu Sans" } 		H2.ctl { font-family: "DejaVu Sans" } 		H3 { margin-bottom: 0.08in } 		H3.western { font-family: "Nimbus Roman No9 L", "Times New Roman", serif } 		H3.cjk { font-family: "DejaVu Sans" } 		H3.ctl { font-family: "DejaVu Sans" } 		CODE { font-family: "DejaVu Sans Mono", monospace } 	--></p>
<h2><a title="overview" name="overview"></a>What is the Android NDK?</h2>
<p><font size="2">The Android NDK is a companion tool used <em>only</em> in conjunction with Android SDK which allows application developers to build performance-critical portions of their apps by use of native (C/C++) code.</font></p>
<p><font size="2">This provide benefits in form of reuse of existing code and increased speed.</font></p>
<p><font size="2">Contents of NDK:</font></p>
<ul>
<li><font size="2">A set of tools and 	build files for generating native code libraries from C and C++ 	sources. </font></li>
<li><font size="2">A way to embed the 	corresponding native libraries into application package files 	(.apks) that can be deployed on Android devices. </font></li>
<li><font size="2">A set of native 	system headers and libraries supported starting from Android 1.5 . </font></li>
<li><font size="2">Documentation, samples, and tutorials.</font></li>
</ul>
<p><font size="2">Download NDK from </font><font color="#000080"><u><a href="http://developer.android.com/sdk/ndk/1.6_r1/index.html"><font size="2">http://developer.android.com/sdk/ndk/1.6_r1/index.html</font></a></u></font><font size="2">  and uncompress it.Now run the script in &lt;NDK&gt;/build/host-setup.sh.This creates &lt;NDK&gt;/out/host/config.mk which configure NDK for system.</font><strong><code></code></strong></p>
<p><strong><strong><font size="3">How to write an  application using NDK? </font></strong></strong></p>
<p><font size="2">Follow underneath steps to create our own application using NDK and make it running on emulator.</font></p>
<p><strong><strong><font size="2">Write an Android Activity</font></strong></strong></p>
<p><font size="2">You need to include following lines of extra code in our java file. </font></p>
<ul>
<li><font size="2">Call 	native method inside on create method.</font></li>
<li><font size="2">Declare 	native method outside oncreate method.</font></li>
<li><font size="2">Call 	to â€œSystem.loadLibraryâ€ method inside a static initializer block 	to load our shared library object.</font></li>
</ul>
<p><font size="2"><font color="#dc2300">package com.example.FileLoaderActivity;</font></font></p>
<p><font size="2"><font color="#dc2300">public class FileLoaderActivity extends Activity {</font></font></p>
<p><font size="2"><font color="#dc2300">	private static String TAG = &#8220;FileLoaderActivity&#8221;;</font></font></p>
<p><font size="2"><font color="#dc2300">	// The name of a test file to write</font></font></p>
<p><font size="2"><font color="#dc2300">	private static String FILENAME = &#8220;hello&#8221;;</font></font></p>
<p><font size="2"><font color="#dc2300">	// The full path to the test file</font></font></p>
<p><font size="2"><font color="#dc2300">	private static String PATH = &#8220;/data/data/com.example.FileLoaderActivity/&#8221; + FILENAME;</font></font></p>
<p><font size="2"><font color="#dc2300">	</font></font></p>
<p><font color="#dc2300">    <font size="2">@Override</font></font></p>
<p><font color="#dc2300">    <font size="2">public void onCreate(Bundle savedInstanceState) {</font></font></p>
<p><font color="#dc2300">        <font size="2">super.onCreate(savedInstanceState);</font></font></p>
<p><font color="#dc2300">        <font size="2">setContentView(R.layout.main);</font></font></p>
<p><font color="#dc2300">          <font size="2">&#8230;&#8230;&#8230;&#8230;&#8230;</font></font></p>
<p><font color="#dc2300">        <font size="2">// Call the native method to read the file just written</font></font></p>
<p><font color="#dc2300">        <font size="2">byte[] bytes = loadFile(PATH);</font></font></p>
<p><font color="#dc2300">       </font><font color="#dc2300">&#8230;&#8230;&#8230;&#8230;..</font></p>
<p><font color="#dc2300">    <font size="2">}</font></font></p>
<p><font color="#dc2300"> </font></p>
<p><font color="#dc2300"><font size="2">public native byte[] loadFile(String fileName);</font></font><font color="#dc2300">    <font size="2">static {</font></font></p>
<p><font color="#dc2300">        <font size="2">System.loadLibrary(&#8220;fileloader&#8221;);</font></font></p>
<p><font color="#dc2300">    <font size="2">}    </font></font></p>
<p><font size="2"><font color="#dc2300">}</font></font></p>
<h3></h3>
<h3><strong><strong><font size="2">Generating C/C++ header files                                                                                                                                                      </font></strong><font size="2">For generating C/C++ header file for our shared library: </font></strong></h3>
<ul>
<li><font size="2">Build 	project which generates Java.class files under &lt;<em>project_home</em>&gt;/bin</font></li>
<li><font size="2">Use<em> 	javah </em>tool to generate a C/C++ header file.</font></li>
</ul>
<p><font size="2">Issue the following command at the top of &lt;project_home&gt;, my command looks like:</font></p>
<p><font color="#00ae00"> </font><strong><font size="2"><font color="#00ae00">javah -o fileloader.h -classpath bin com.example.FileLoaderActivity.FileLoaderActivity</font></font></strong></p>
<p><font size="2">The above command will generate a file called fileloader<em>.h .</em></font></p>
<h3><strong><strong><font size="2">Creating our Shared Library</font></strong></strong></h3>
<p><font size="2">Create two folders which will contain source code and make files:</font></p>
<ul>
<li><font size="2"><font color="#007800">mkdir</font> 	<font color="#660033">-p</font> <font color="#007800">~/workspace</font><font color="#000000">/&lt;project_home&gt;/jni 	folder</font></font><strong><font size="2"><font color="#000000"> &#8211; </font></font></strong><font size="2"><font color="#000000">contain 	the source code for our shared library.</font></font></li>
<li><font size="2"><font color="#007800">mkdir</font><font color="#000000"> 	</font><font color="#660033">-p</font><font color="#000000"> 	</font><font color="#007800">$NDK_HOME</font><font color="#000000">/apps/fileloader 	folder</font></font><font size="2"><font color="#000000">-      &#8220;project definition&#8221; folder for our shared library</font></font></li>
</ul>
<p><font size="2">Now copy header file &#8216;hellondk.h&#8217; from Eclipse project folder to <font color="#007800">~/workspace</font><font color="#000000">/&lt;project_home&gt;/jni</font><em>/</em></font></p>
<p><font size="2">For building our NDK project, we need to create two Makefiles which will specify how and from where to pick source code and  library file.</font></p>
<ul>
<li><font size="2">Android.mk</font></li>
<li><font size="2">Application.mk</font></li>
</ul>
<p><strong><font size="2">&#8220;<em><font color="#007800">~/</font></em><em><font color="#007800"><strong>workspace</strong></font><font color="#000000">/&lt;<strong>project_home</strong>&gt;/<strong>jni</strong></font>/<strong>Android.mk</strong></em>&#8221;  </font></strong></p>
<p><font size="2">Android.mk file describes source file to build system and its contents are  :</font></p>
<p><em><font size="2"><font color="#dc2300">LOCAL_PATH := $(call my-dir)</font></font></em></p>
<p><em><font size="2"><font color="#dc2300">include $(CLEAR_VARS)</font></font></em></p>
<p><em><font size="2"><font color="#dc2300">LOCAL_MODULE    := fileloader</font></font></em></p>
<p><em><font size="2"><font color="#dc2300">LOCAL_SRC_FILES := fileloader.c</font></font></em></p>
<p><em><font size="2"><font color="#dc2300">LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog</font></font></em></p>
<p><em><font size="2"><font color="#dc2300">include $(BUILD_SHARED_LIBRARY)</font></font></em></p>
<p><strong><em><font size="2">&#8220;<strong>$NDK_HOME</strong>/<strong>apps</strong>/<strong>fileloader</strong>/<strong>Application.mk</strong>&#8220;</font></em></strong></p>
<p><font size="2">Application.mk file describes modules that are needed by your application and it looks like:</font></p>
<p><em><font size="2"><font color="#dc2300">APP_PROJECT_PATH := ~/workspace/FileLoaderActivity </font></font></em></p>
<p><em><font size="2"><font color="#dc2300">APP_MODULES := fileloader</font></font></em></p>
<h3></h3>
<h3><strong><strong><font size="2">Creating Shared Library Source</font></strong></strong></h3>
<p><font size="2">Now create C source file for shared library which defines JNI method called by Java file.Signature of this Jni method should match with fileloader.h file generated by eclipse build system.</font></p>
<p><strong><strong><em><font size="2">~/workspace/FileLoaderActivity/jni/fileloader.c</font></em></strong></strong></p>
<p><font size="2"><font color="#dc2300">jbyteArray Java_com_example_FileLoaderActivity_FileLoaderActivity_loadFile </font></font></p>
<p><font size="2"><font color="#dc2300">(JNIEnv* env, jobject thiz, jstring filename)</font></font></p>
<p><font size="2"><font color="#dc2300">{</font></font></p>
<h3><strong><font color="#dc2300">   <font size="2">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..</font></font></strong></h3>
<h3><strong><font size="2"><font color="#dc2300">}</font></font></strong></h3>
<h3><strong><strong><font size="2">Compiling our Shared Library</font></strong></strong></h3>
<p><font size="2">Change current working directory to $NDK_HOME and run command â€œmake APP=fileloaderâ€.You can find your shared library into folder $NDK_HOME/out/apps/fileloader/ directory.You can now run our FileLoaderActivity project from within Eclipse. Dont forget to push file â€œhelloâ€ mentioned in java code before running project.Here&#8217;s a screenshot of what you should see in the emulator.</font></p>
<p><a href="http://www.techjini.com/blog/wp-content/uploads/2009/10/device.png" title="device.png"><img src="http://www.techjini.com/blog/wp-content/uploads/2009/10/device.png" alt="device.png" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.techjini.com/blog/2009/10/26/android-ndk-an-introduction-how-to-work-with-ndk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
