{"id":21930,"date":"2015-06-29T10:36:43","date_gmt":"2015-06-29T05:06:43","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=21930"},"modified":"2015-07-09T15:08:05","modified_gmt":"2015-07-09T09:38:05","slug":"android-application-using-ndk","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/android-application-using-ndk\/","title":{"rendered":"Building Android Application using NDK"},"content":{"rendered":"<p>The <strong>Native Development Kit (NDK)<\/strong> is a toolset enables us to implement parts of our app using native-code languages like C and C++. Typically, good use cases for the NDK are CPU-intensive applications such as signal processing, game engines, and physics simulation.<\/p>\n<p>[java]<br \/>\npublic class MyActivity extends Activity {<br \/>\n  \/**<br \/>\n  * Native method implemented in C\/C++<br \/>\n  *\/<br \/>\n  public native void invokeNativeFunction();<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>The use of <a href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\">native code in Android apps<\/a> are particularly <a title=\"Mobile App Developers\" href=\"http:\/\/www.tothenew.com\/mobile-ios-application-development-services\">useful to developers who wish<\/a> to do one of the following:<br \/>\n<strong>1.<\/strong> Port their apps between platforms.<br \/>\n<strong>2.<\/strong> Reuse existing libraries, or provide their own libraries for reuse.<br \/>\n<strong>3.<\/strong> Increase performance in certain cases, particularly computationally intensive ones like games.<\/p>\n<p><span class=\"highlight\"><strong>Installing the Android NDK:<\/strong><\/span><br \/>\n<strong>1.<\/strong> Download the ndk package from the link:<br \/>\n<strong><a href=\"http:\/\/developer.android.com\/tools\/sdk\/ndk\/index.html\" target=\"_new\"> http:\/\/developer.android.com\/tools\/sdk\/ndk\/index.html<\/a><\/strong><br \/>\n<strong>2.<\/strong> Open a terminal window.<br \/>\n<strong>3.<\/strong> Go to the directory to which you downloaded the package.<br \/>\n<strong>4.<\/strong> Run <strong>chmod a+x<\/strong> on the downloaded package.<br \/>\n<strong>5.<\/strong> Execute the package. For example:<\/p>\n<p>[java]<br \/>\n\tndk$ chmod a+x android-ndk-r10c-darwin-x86_64.bin<br \/>\n\tndk$ .\/android-ndk-r10c-darwin-x86_64.bin<br \/>\n  [\/java]<\/p>\n<p>The folder containing the NDK extracts itself.<\/p>\n<p><span class=\"highlight\"><strong>Making a Basic NDK App:<\/strong><\/span><br \/>\n<strong>Step 1.<\/strong> In the Android project, Make a folder called <strong>jni<\/strong> in the root of project (right-click the project node, New \u2013 Folder).<br \/>\n<strong>Step 2.<\/strong> Create a file called <strong>Android.mk<\/strong> within jni folder with the following contents:<\/p>\n<p>[java]<br \/>\nLOCAL_PATH:=$(call my-dir)<br \/>\ninclude $(CLEAR_VARS)<br \/>\nHere we give our module name and source file(s)<br \/>\nLOCAL_MODULE := ndk_test<br \/>\nLOCAL_SRC_FILES := ndk_test.c<br \/>\ninclude $(BUILD_SHARED_LIBRARY)<br \/>\n[\/java]<\/p>\n<p>The <strong>Android.mk<\/strong> file is important for the NDK build process to recognize your NDK modules. In our case we named our module ndk_test and told the build tool that it consists of one source file named ndk_test.c.<\/p>\n<p><strong>Step 3.<\/strong> Create file <strong>ndk_test.c<\/strong> in jni folder<\/p>\n<p>[java]<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;jni.h&gt;<\/p>\n<p>jstring Java_com_example_ndktest_NdkTestActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {<br \/>\n  return (*env)-&gt;NewStringUTF(env, &quot;Hello from native code!&quot;);<br \/>\n}<br \/>\n  [\/java]<\/p>\n<p><strong>Step 4.<\/strong> In <strong>NdkTestActivity<\/strong> class to use the NDK code:<\/p>\n<p>[java]<br \/>\npublic class NdkTestActivity extends Activity {<\/p>\n<p>  \/\/ load the library &#8211; name matches jni\/Android.mk<br \/>\n  static {<br \/>\n    System.loadLibrary(&quot;ndk_test&quot;);<br \/>\n  }<\/p>\n<p>  \/\/ declare the native code function &#8211; must match ndkfoo.c<br \/>\n  private native String invokeNativeFunction();<\/p>\n<p>  @Override<br \/>\n    public void onCreate(Bundle savedInstanceState) {<br \/>\n        super.onCreate(savedInstanceState);<br \/>\n        setContentView(R.layout.main);<\/p>\n<p>        \/\/ this is where we call the native code<br \/>\n        String hello = invokeNativeFunction();<\/p>\n<p>        new AlertDialog.Builder(this).setMessage(hello).show();<br \/>\n    }<br \/>\n}<br \/>\n  [\/java]<\/p>\n<p>This code will invoke the NDK method that will return the string, that will be displayed as an alert on the screen. Here\u2019s the result of running the ndk_test app<\/p>\n<p>A successful run of ndk-build tool. Create an <strong>.so<\/strong> file in a new folder called libs<br \/>\nThe .so file is the binary library that will be included into the application .apk package and will be available for the Java code of the app to link to.<\/p>\n<p>Hope this was useful to the readers.<\/p>\n<p><strong>Happy NDK\u2019ing.<\/strong> \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Native Development Kit (NDK) is a toolset enables us to implement parts of our app using native-code languages like C and C++. Typically, good use cases for the NDK are CPU-intensive applications such as signal processing, game engines, and physics simulation. [java] public class MyActivity extends Activity { \/** * Native method implemented in [&hellip;]<\/p>\n","protected":false},"author":167,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[518],"tags":[1928,1927],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21930"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/167"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=21930"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21930\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=21930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=21930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=21930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}