Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana Login Create an Account Contact us 1 (415) 647-7000 Home Bookshelf Training Tutorials Stream TechTalk TechTV About Us TechTalk » Android - Examples Using NDK to Call C code from Android Apps Using NDK to Call C code from Android Apps November 25, 2009 3:45:56 PM PST (2 years ago). Seen 72,726 times. 23 replies. [reply] [permalink] 155 Member since Jan 19, 2007 Location: San Francisco Forum Posts: 153 to hear about new tech videos
of 12
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.

Document 1

  Using NDK to Call C code from Android Apps - Marakana[2/17/2012 1:45:13 AM] Login   Create an Account Contact us 1 (415) 647-7000 HomeTrainingStream TechTalk  TechTV  About Us Bookshelf Tutorials to hear about new techvideos/tutorials/posts. Upcoming Android classes See Complete ScheduleAndroid™ Bootcamp withMark Murphyon Mar 5 (5 days) in New York Intro to Android™ withMark Murphyon Mar 5 (3 days) in New York Advanced Android™ withMark Murphyon Mar 8 (2 days) in New York Android™ Internals withMarko GargentaonMar 12 (3 days) in New York PhoneGap withPaulBeusterienon Mar 26 (1day) in San Francisco Android™ Bootcamp withAleksandar Gargentaon Apr 16 (5 days) in SanFrancisco Intro to Android™ withAleksandar Gargentaon Apr 16 (3 days) in SanFrancisco Advanced Android™ withAleksandar Gargentaon Apr 19 (2 days) in SanFrancisco Android™ Internals withMarko Gargentaon Apr23 (3 days) in San Francisco Android™ Internals on Apr 23 (3 days) onOnline Classroom Marakana Tutorials Python Tutorial   Using NDK to Call C code from Android Apps Marko Gargenta @MarkoGargentaMarakana, Inc. Member since Jan 19, 2007Location: San FranciscoForum Posts: 153 TechTalk»Android - Examples> Using NDK to Call C code from Android Apps November 25, 2009 3:45:56 PM PST (2 years ago). Seen 72,726 times. 23 replies. [reply] [permalink]   5 Updated for NDK 1.6 While Android SDK is great for application development, every once in a while you may needaccess to native code. This code is usually done in C. While you were able to access nativecode via Java Native Interface (JNI) all along, the process was rather hard. You would'vetypically had to compile everything on your host computer for the target architecture,requiring you to have the entire toolchain on your development machine.Android NDK (Native Development Kit) simplifies working with native code. It includes theentire toolchain needed to build for your target platform (ARM). It is designed to help youcreate that shared library.Note that native code accessible via JNI still runs inside the Dalvik VM, and as such is subjectto the same life-cycle rules that any Android application lives by. The advantage of writingparts of your app code in native language is presumably speed in certain cases. Note: I'm using <NDKHOME> to refer to the root directory in which you installed your NDK. Forme that's /Users/marko/WorkArea/android-ndk-1.6_r1 . I'm assuming all other directoriesand files are relative to your Eclipse project root, in my case /Users/marko/Workspace/Android/NDKDemo . Overview We are roughly going to do this: 40443Reddit155ShareThis  Using NDK to Call C code from Android Apps - Marakana[2/17/2012 1:45:13 AM]  jQuery TutorialHTML5 TutorialJava TutorialJBoss Admin TutorialSelenium Tutorial TechTV  Announcing TechTV - fun andeducational community videoswith focus on cutting edgesoftware. Enjoy!All content inTechTalkandTechTV is licensed underCreative CommonsLicense  (non-commercial) . Please Share! 1. Create the Java class representing the native code2. Create the native code header file3. Implement the native code by writing your C code4. Compile everything and build you Shared Library5. Use your native code inside Android activity Create Native Library This is just a Java file that lives in standard src directory in your Eclipse project. It serves asthe glue to the native code that we'll write later.  /src/com.marakana/ Code: Create C Header File In your project bin directory (in my case, <EclipseWorkspace>/NDKDemo/bin ), run javah toolto create the JNI header file.Next, create a jni directory in your project directory (in my case, <EclipseWorkspace>/NDKDemo/jni ).Next, copy the JNI header from <EclipseWorkspace>/NDKDemo/bin to <EclipseWorkspace>/NDKDemo/jni Here's my command line: Code: Write the C Code In your <EclipseWorkspace>/NDKDemo/jni/ folder, create ndk_demo.c file. This is where we'llimplement the native code. To start, copy the function signatures from the header file, andprovide the implementation for those functions. In this example, the header file looks like this: <EclipseWorkspace>/NDKDemo/jni/com_marakana_NativeLib.h Code: package com.marakana;public class NativeLib {  static {System.loadLibrary( ndk_demo );}  /*** Adds two integers, returning their sum*/   public native int add( int v1, int v2 ) ; /*** Returns Hello World string*/   public native String hello() ;}NDKDemo/bin$ javah -jni com.marakana.NativeLibNDKDemo/bin$ mv com_marakana_NativeLib.h ../jni/  Using NDK to Call C code from Android Apps - Marakana[2/17/2012 1:45:13 AM] And the corresponding implementation looks like this: <EclipseWorkspace>/NDKDemo/jni/ndk_demo.c Code: Build The Library To build the library, first we need to create a makefile for how to compile the C code: <EclipseWorkspace>/NDKDemo/jni/ Code: Next, we need to tell NDK how to build the shared library and put it in the correct place inside /* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_marakana_NativeLib */#ifndef _Included_com_marakana_NativeLib#define _Included_com_marakana_NativeLib#ifdef __cplusplusextern C {#endif/** Class: com_marakana_NativeLib* Method: add* Signature: (II)I*/JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add(JNIEnv *, jobject, jint, jint);/** Class: com_marakana_NativeLib #include com_marakana_NativeLib.h JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello(JNIEnv * env , jobject obj ) {  return (*env)->NewStringUTF(env, Hello World! ); }JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add(JNIEnv * env , jobject obj , jint value1 , jint value2 ) {  return (value1 + value2); }LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := ndk_demoLOCAL_SRC_FILES := ndk_demo.cinclude $(BUILD_SHARED_LIBRARY)  Using NDK to Call C code from Android Apps - Marakana[2/17/2012 1:45:13 AM] the Eclipse project. To do this, create a folder <NDKHOME>/apps/ndk_demo/ and inside thisfolder create the Application file: <NDKHOME>/apps/ndk_demo/Application Code: Next, create a symbolic link <NDKHOME>/apps/ndk_demo/project to your Eclipse project: ln -s ~/Workspace/Android/NDKDemo <NDKHOME>/apps/ndk_demo/project If you are on Windows, or another OS that doesn't support symbolic links, you may have tocopy entire Eclipse project into <NDKHOME>/apps/ndk_demo/project directory, then copy backto Eclipse. I'm running all this on Mac OS X 10.6 and I assume Linux-type shell.You can now to to your <NDKHOME> and run make APP=ndk_demo The output should look lie this: Code: You can now refresh your Eclipse project and you should /lib/ directory containing your file. Calling Native Code from Java So now that we have the native C library implemented, compiled, and placed in the rightplace, let's see how we can call it from our Activity. It's actually rather simple - you just haveto instantiate the instance of your NativeLib class and from there on, it's just a regular Javaobject.  /src/com.marakana/ Code: APP_PROJECT_PATH := $(call my-dir)/projectAPP_MODULES := ndk_demoandroid-ndk-1.5_r1$ make APP=ndk_demoAndroid NDK: Building for application 'ndk_demo'Compile thumb : ndk_demo <= sources/ndk_demo/ndk_demo.cSharedLibrary : libndk_demo.soInstall : => apps/ndk_demo/project/libs/armeabipackage com.marakana;import;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class NDKDemo extends Activity {   NativeLib nativeLib; /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks