using ndk to call c code from android apps - marakana

12
Using NDK to Call C code from Android Apps - Marakana http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM] Login Create an Account Contact us 1 (415) 647-7000 Home Training Stream TechTalk TechTV About Us Bookshelf Tutorials to hear about new tech videos/tutorials/posts. Upcoming Android classes See Complete Schedule Android™ Bootcamp with Mark Murphy on Mar 5 (5 days) in New York Intro to Android™ with Mark Murphy on Mar 5 (3 days) in New York Advanced Android™ with Mark Murphy on Mar 8 (2 days) in New York Android™ Internals with Marko Gargenta on Mar 12 (3 days) in New York PhoneGap with Paul Beusterien on Mar 26 (1 day) in San Francisco Android™ Bootcamp with Aleksandar Gargenta on Apr 16 (5 days) in San Francisco Intro to Android™ with Aleksandar Gargenta on Apr 16 (3 days) in San Francisco Advanced Android™ with Aleksandar Gargenta on Apr 19 (2 days) in San Francisco Android™ Internals with Marko Gargenta on Apr 23 (3 days) in San Francisco Android™ Internals on Apr 23 (3 days) on Online Classroom Marakana Tutorials Python Tutorial Using NDK to Call C code from Android Apps Marko Gargenta @MarkoGargenta Marakana, Inc. Member since Jan 19, 2007 Location: San Francisco Forum 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 need access to native code. This code is usually done in C. While you were able to access native code via Java Native Interface (JNI) all along, the process was rather hard. You would've typically 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 the entire toolchain needed to build for your target platform (ARM). It is designed to help you create that shared library. Note that native code accessible via JNI still runs inside the Dalvik VM, and as such is subject to the same life-cycle rules that any Android application lives by. The advantage of writing parts 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. For me that's /Users/marko/WorkArea/android-ndk-1.6_r1. I'm assuming all other directories and files are relative to your Eclipse project root, in my case /Users/marko/Workspace/Android/NDKDemo. Overview We are roughly going to do this: 40 44 3 Reddit 155 ShareThis

Upload: leopold-raphael

Post on 12-Oct-2014

242 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

Login Create an Account Contact us 1 (415) 647-7000

Home Training Stream TechTalk TechTV About UsBookshelf Tutorials

to hear about new techvideos/tutorials/posts.

Upcoming Android classes

See Complete Schedule

Android™ Bootcampwith Mark Murphy on Mar 5(5 days) in New York

Intro to Android™with Mark Murphy on Mar 5(3 days) in New York

Advanced Android™with Mark Murphy on Mar 8(2 days) in New York

Android™ Internalswith Marko Gargenta onMar 12 (3 days) in New York

PhoneGap with PaulBeusterien on Mar 26 (1day) in San Francisco

Android™ Bootcampwith Aleksandar Gargentaon Apr 16 (5 days) in SanFrancisco

Intro to Android™with Aleksandar Gargentaon Apr 16 (3 days) in SanFrancisco

Advanced Android™with Aleksandar Gargentaon Apr 19 (2 days) in SanFrancisco

Android™ Internalswith Marko Gargenta on Apr23 (3 days) in San Francisco

Android™ Internalson 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 Francisco

Forum 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:

40 44 3Reddit 155ShareThis

Page 2: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

jQuery Tutorial

HTML5 Tutorial

Java Tutorial

JBoss Admin Tutorial

Selenium Tutorial

TechTV

Announcing TechTV - fun andeducational community videoswith focus on cutting edgesoftware. Enjoy!

All content in TechTalk and TechTVis licensed under Creative 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/NativeLib.javaCode:

Create C Header FileIn 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.hCode:

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/

Page 3: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

And the corresponding implementation looks like this:

<EclipseWorkspace>/NDKDemo/jni/ndk_demo.cCode:

Build The Library

To build the library, first we need to create a makefile for how to compile the C code:

<EclipseWorkspace>/NDKDemo/jni/Android.mkCode:

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 * M th d h ll

#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.c

include $(BUILD_SHARED_LIBRARY)

Page 4: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[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/ApplicationCode:

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 yourlibndk_demo.so 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/NDKDemo.javaCode:

APP_PROJECT_PATH := $(call my-dir)/projectAPP_MODULES := ndk_demo

android-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 : libndk_demo.so => apps/ndk_demo/project/libs/armeabi

package com.marakana;

import android.app.Activity;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. */ @Override public void onCreate(Bundle savedInstanceState) {

Page 5: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

The UI for this example is not that significant, but I'm going to include it here for the sake ofcompleteness.

/res/layout/main.xmlCode:

Output

Source Code

super.onCreate(savedInstanceState); setContentView(R.layout.main);

i ib i ib()

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="NDK Demo" android:textSize="22sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textOut" android:text="output"></TextView> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/value1" android:hint="Value 1"></EditText> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:textSize="36sp"></TextView> <EditText android:layout_width="wrap_content" d id l h i h " " d id id "@ id/ l 2"

Page 6: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

Edited 12 times. Last edit by Ashok Marannan on Feb 9, 2011 at 2:28:51 AM (about one year ago).

Doug HeggeTechnical ArchitectJDH Consulting, Inc.

Member since Jul 9, 2010Forum Posts: 1

Marko Gargenta@MarkoGargentaMarakana, Inc.

Member since Jan 19, 2007Location: San Francisco

Forum Posts: 153

Joe KnappLucent

Member since Sep 19, 2010Forum Posts: 1

Munir HoqueRUET

Member since Sep 29, 2010Forum Posts: 1

Member since Nov 12, 2010

http://marakana.com/static/tutorials/NDKDemo.ziphttp://marakana.com/static/tutorials/NDKHOME.zip

July 9, 2010 8:41:30 PM PDT (one year ago) [reply] [permalink]

Nice article I am planning todo just this to load a kernel module that I have already developedto take the funtional place of IPTABLES my only question is when I call the native C modulewhich contains the C equivelant of insmod / rmmod how do I believe I have to have rootaccess to pull this off dont I.

Obviously the Android OS itself does this for the WIFI driver via the wifi.c moduleas part of othe android distro. Obviously it can do it so I should be able to as well I guess.

If nothing else for development purposes I can install a custom rom with my own androidbuild with the appropriate libary what am I missing?

Is there an easy wat to do this? I would be running as an Android Service and use an RPCfrom an Android GUI application to change the status load/unload my kernel module.

Thanks,[email protected]

July 12, 2010 10:36:04 AM PDT (one year ago) [reply] [permalink]

NDK allows you to put native code insite your application, BUT that native code is still subjectto all restrictions that apply to running the app.

Now, if you are installing custom ROM, then you can do whatever you want. Consider it just alinux OS. You want to put your libraries in /system/lib directory.

September 19, 2010 11:24:38 AM PDT (one year ago) [reply] [permalink]

Thanks for the nice tutorial!

One thing--symbolic links are available with Vista and Windows 7 using the mklink command.The syntax from the command shell would be:

mklink /d <NDKHOME>\apps\ndk_demo\project <EclipseWorkspace>\NDKDemo

Note that the order of arguments is reversed from the Unix ln command.

Joe

September 29, 2010 5:02:35 AM PDT (one year ago) [reply] [permalink]

@Marko Gargenta - Boss, is it possible to develop all parts of Android ApplicationDevelopment using C++. (Actually, I know nothing about Android and JAVA)

November 12, 2010 9:08:23 AM PST (one year ago) [reply] [permalink]

Page 7: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

Edited one time. Last edit by Mohamed M on Nov 12, 2010 at 9:15:50 AM (about one year ago).

Mohamed MForum Posts: 2

Mohamed M Member since Nov 12, 2010Forum Posts: 2

HyHow can I use libmms in android project with NDK. I have mms.c and mms.h fromhttp://radiotime.com/apps/android.aspx

For example how can I use C methods in NativeLib.java :

mms_t* mms_connect (mms_io_t *io, void *data, const char *url, int bandwidth);int mms_read (mms_io_t *io, mms_t *instance, char *data, int len);

Finally, should I modify the original mms.c and mms.h?

Thanks

November 12, 2010 10:38:22 AM PST (one year ago) [reply] [permalink]

if I understand you, I should create a NativeLib.java like that package com.saturne.radio; public class NativeLib { static {System.loadLibrary("mms");} public native int mms_connect (int[] t, int[] a, String url, int bandwidth); public native int mms_read(String data, int len); public native int mms_request_time_seek (double time_sec); public native int mms_time_seek (double time_sec); }

and by javah -jni NativeLib I have the header like that /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_saturne_radio_NativeLib */ #ifndef _Included_com_saturne_radio_NativeLib #define _Included_com_saturne_radio_NativeLib #ifdef __cplusplus extern "C" { #endif /* * Class: com_saturne_radio_NativeLib * Method: mms_connect * Signature: ([I[ILjava/lang/String;I)I */ JNIEXPORT jint JNICALL Java_com_saturne_radio_NativeLib_mms_1connect (JNIEnv *, jobject, jintArray, jintArray, jstring, jint); /* * Class: com_saturne_radio_NativeLib * Method: mms_read * Signature: (Ljava/lang/String;I)I */ JNIEXPORT jint JNICALL Java_com_saturne_radio_NativeLib_mms_1read (JNIEnv *, jobject, jstring, jint); /* * Class: com_saturne_radio_NativeLib * Method: mms_request_time_seek * Signature: (D)I */ JNIEXPORT jint JNICALL Java_com_saturne_radio_NativeLib_mms_1request_1time_1seek (JNIEnv *, jobject, jdouble); /* * Class: com_saturne_radio_NativeLib * Method: mms_time_seek * Signature: (D)I

Page 8: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

William WuWistron

Member since Nov 16, 2010Forum Posts: 1

Archana Udayananchs,India

Member since Nov 21, 2010Forum Posts: 1

Muhammad Kamran AfridiStudent- TU ilmenau, Germany

Member since Nov 26, 2010Forum Posts: 1

Rajiv DayanandaMindTree

Member since Dec 22, 2010Forum Posts: 1

Ashok MarannanUsing NDK to Call C code from Android AppsStudent

Member since Feb 8, 2011Forum Posts: 1

*/ JNIEXPORT jint JNICALL Java_com_saturne_radio_NativeLib_mms_1time_1seek (JNIEnv *, jobject, jdouble); #ifdef __cplusplus }

#endif #endif but Now should I change all code source of mms.c to make corresponding between .h and .c file ? Are their another solution with not using JNI (use IPC) ? the mms.c file is in this link: http://radiotime.com/apps/android.aspx Thanks

November 16, 2010 6:14:27 PM PST (one year ago) [reply] [permalink]

Perfect,and thank you for your sharing

November 24, 2010 2:51:40 AM PST (one year ago) [reply] [permalink]

hey i am very new to android.Still it wasn't really tough following the steps.Its actually madequite simple by you.Thanks a lot.But the thing is its not completely working out fine.my application is stucking out at a pointwhen i click for final output.I think the OnClickListener call is posing a problem.kindly help....

November 26, 2010 11:46:26 AM PST (one year ago) [reply] [permalink]

Can anyone please help me in this part, please elaborate it a bit more...I am new to all of thisstuff, What should I do?

"Build The Library

To build the library, first we need to create a makefile for how to compile the C code:"

December 22, 2010 3:03:10 AM PST (one year ago) [reply] [permalink]

superb tutorial thank you

February 9, 2011 2:28:51 AM PST (one year ago) [reply] [permalink]

Hi this is great and i got it working!! but i would like to incorporate assembly language code inthe c file ndk_demo.c in the function add()i have written my implementation as

JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add(JNIEnv * env, jobject obj, jint value1, jint value2) {

asm ("addl %%ebx,%%eax"

Page 9: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

Edited one time. Last edit by Thilagaraj M on Jun 3, 2011 at 1:57:06 AM (about one year ago).

Al RametDeveloperFree Lance

Member since Jun 2, 2011Forum Posts: 1

Thilagaraj MDeveloper

Member since Jun 3, 2011Forum Posts: 2

Thilagaraj MDeveloper

Member since Jun 3, 2011Forum Posts: 2

:"=a"(value1):"a"(value1), "b"(value2));

return (value1);}

but when i create shared library using nkd build tool with the command $make APP=ndk_demoit throws the followin error

Android NDK: Building for application 'ndk_demo'Compile thumb : ndk_demo <= ndk_demo.capps/ndk_demo/project/jni/ndk_demo.c: In function 'Java_com_marakana_NativeLib_add':apps/ndk_demo/project/jni/ndk_demo.c:14: error: impossible constraint in 'asm'make: *** [out/apps/ndk_demo/armeabi/objs/ndk_demo/ndk_demo.o] Error 1

please help to fix this error

June 2, 2011 11:44:20 AM PDT (37 weeks ago) [reply] [permalink]

Hello,I am using linux Ubuntu with Eclipse.I am trying to create an app using NDK to access USB portI tried to include libusb and have not been successfull yet.I used Java JNI with .c program and were able to access usb portbut no luck using Anroid NDK with the same .c program that I have alreay got.I think my problems are how to include/access libusb for ARM in the Eclipse project I have.

If you have a small sample app where I could connect/send a message to USB port usingAndroid NDK in Eclipse Env. please posted

ThanksA.

June 3, 2011 1:50:44 AM PDT (36 weeks ago) [reply] [permalink]

hi I dont about the jni brifely but i know i have one doubt regarding the JNI i am using androidsdk some one told write jni in android NDK only.in sdk its is possibleplease tell me how to write the jni in android

ThanksThilak.

June 3, 2011 3:25:14 AM PDT (36 weeks ago) [reply] [permalink]

hi

I download your application and Run the code its working but i import the my eclipse jni foldernot there why i dont know can you anyone help me

ThanksThilak

Page 10: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

Gachon MickaelStudent

Member since Jul 4, 2011Forum Posts: 1

Vigneshwaran SekarEmbedded

Member since Jul 13, 2011Forum Posts: 1

Ganga sai Pradeep Reddy PeddamalluSelf

Member since Jul 25, 2011Forum Posts: 1

July 4, 2011 4:50:09 AM PDT (32 weeks ago) [reply] [permalink]

hi,first, many thanks for your topic, it is very usefull ! However, I get a problem when running make command :make: *** No targets specified and no makefile found. Stop.Indeed we had juste create an Android.mk file and not a makefile ... Moreover the android.mkfile is not in <NDKHOME>/ but in <NDKHOME>/apps/ndk_demo/project/jni/ I dowloaded your source code but I did not see any folder jni/ as you wrote in your tutorial.Does someone know what is my problem?Thank you by advence,Mick

July 13, 2011 11:05:29 PM PDT (31 weeks ago) [reply] [permalink]

mr marko,i try to run ur exmple code ,atlast after install tion and after giving values while click the '=' button suddenly

the application NDKDemo(process com.marakana) has stopped unexpectedly.please try again.

like it showing.

what is the problem?

August 2, 2011 5:02:30 AM PDT (28 weeks ago) [reply] [permalink]

Hello,

I am new to Android-JNI ,I did above example ,while running i got below error in logcat.Mydoubt is how to add jni.h path in eclipse.

# logcat --------- beginning of /dev/log/systemI/ActivityManager( 1002): Starting: Intent { act=android.intent.action.MAINcat=[android.intent.category.LAUNCHER] flg=0x10200000cmp=com.android.mms/.ui.ConversationList } from pid 1097W/InputManagerService( 1002): Starting input on non-focused clientcom.android.internal.view.IInputMethodClient$Stub$Proxy@40732108 (uid=10025 pid=1234)I/ActivityManager( 1002): Starting: Intent { act=android.intent.action.MAINcat=[android.intent.category.LAUNCHER] flg=0x10200000cmp=com.uandroid.ndkdemo/.NdkDemo } from pid 1097I/ActivityManager( 1002): Start proc com.uandroid.ndkdemo for activitycom.uandroid.ndkdemo/.NdkDemo: pid=5163 uid=10031 gids={1015}E/AndroidRuntime( 5163): FATAL EXCEPTION: mainE/AndroidRuntime( 5163): java.lang.UnsatisfiedLinkError: helloDevE/AndroidRuntime( 5163): at com.uandroid.ndkdemo.NativeJavaLib.helloDev(Native Method)E/AndroidRuntime( 5163): at com.uandroid.ndkdemo.NdkDemo.onCreate(NdkDemo.java:29)E/AndroidRuntime( 5163): atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)E/AndroidRuntime( 5163): at

Page 11: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

Arun Kumarethics technologies

Member since Aug 8, 2011Forum Posts: 3

Sujit AchayraSoftware EngineerDeveloper

Member since Sep 8, 2011Forum Posts: 1

Arun Kumarethics technologies

Member since Aug 8, 2011Forum Posts: 3

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)E/AndroidRuntime( 5163): atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)E/AndroidRuntime( 5163): atandroid.app.ActivityThread.access$1500(ActivityThread.java:117)E/AndroidRuntime( 5163): atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:928)E/AndroidRuntime( 5163): at android.os.Handler.dispatchMessage(Handler.java:99)E/AndroidRuntime( 5163): at android.os.Looper.loop(Looper.java:123)E/AndroidRuntime( 5163): at android.app.ActivityThread.main(ActivityThread.java:3647)E/AndroidRuntime( 5163): at java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime( 5163): at java.lang.reflect.Method.invoke(Method.java:507)E/AndroidRuntime( 5163): atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)E/AndroidRuntime( 5163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)E/AndroidRuntime( 5163): at dalvik.system.NativeStart.main(Native Method)W/ActivityManager( 1002): Force finishing activity com.uandroid.ndkdemo/.NdkDemoW/ActivityManager( 1002): Activity pause timeout for HistoryRecord{4068e450com.uandroid.ndkdemo/.NdkDemo}W/ActivityManager( 1002): Activity destroy timeout for HistoryRecord{4068e450com.uandroid.ndkdemo/.NdkDemo}I/ActivityManager( 1002): Process com.uandroid.ndkdemo (pid 5163) has died.W/InputManagerService( 1002): Window already focused, ignoring focus gain of:com.android.internal.view.IInputMethodClient$Stub$Proxy@406ab528

August 8, 2011 4:33:20 AM PDT (27 weeks ago) [reply] [permalink]

i am having the error with this step., help me to clear this error

ethics@ethics-desktop:~/ndk-work/android-ndk-r6$ make APP=ndk_demo

Android NDK: There is no Android.mk under apps/ndk_demo/project/jni Android NDK: If this is intentional please define APP_BUILD_SCRIPT to point Android NDK: to a valid NDK build script. build/core/add-application.mk:126: *** Android NDK: Aborting... . Stop.

September 8, 2011 2:38:02 AM PDT (23 weeks ago) [reply] [permalink]

Mr Arun,

Have you created the steps

Here two make file is required.

1. Eclipse_Path>Project>jni>Android.mk2. <NDKHOME>/apps/ndk_demo/Application.mk

If you are using linux then use symbolic link stepln -s ~/Workspace/Android/NDKDemo <NDKHOME>/apps/ndk_demo/project

or copy whole project to NDKHome Directory.

September 10, 2011 12:16:30 AM PDT (22 weeks ago) [reply] [permalink]

Hi sujit,

i have copied the whole project to the specified directory now it compiles.,

Page 12: Using NDK to Call C Code From Android Apps - Marakana

Using NDK to Call C code from Android Apps - Marakana

http://marakana.com/forums/android/examples/49.html[2/17/2012 1:45:13 AM]

301 Howard Street Suite 550

San Francisco, CA 94105

1 (415) 647-7000

Marakana

©2011 Marakana Inc, purveyors of fine training since 2005. Terms of Use

Home

Schedule

Courses

TechTalk

TechTV

Bookshelf

Contact Us

Privacy Policy

Affiliate Registration / Login

All trademarks belong to their respective owners.

This site is powered by TrainingRocket.

Post Reply

Fadi SouilemStudentINSAT

Member since Sep 19, 2011Forum Posts: 1

Arun PachauriSoftware EngineerSamsung

Member since Oct 7, 2011Forum Posts: 1

Cuong ThaiHTK-INC

Member since Oct 20, 2011Forum Posts: 1

Thank you..,

September 19, 2011 3:15:04 AM PDT (21 weeks ago) [reply] [permalink]

Hello Marko,

Thanks a lot for the titorials and video, I really enjoyed them. As a master student, I need tolink my android code to nativve Linux libraraies (*.so) and include header files (.h) of a nativelinux framwork( Gstreamer). to be able to move on on my project, However I can't findsomething usefull for doing this in eclipse .

could you help me about this, please ?

many thanks in advance.

October 7, 2011 12:36:42 AM PDT (18 weeks ago) [reply] [permalink]

Hello Marko,

When i am using given commands in cygwin in Windows 7 then some errors are displaying

$ javah -jni com.example.NativeLib

error: cannot access com.example.NativeLibclassfile for com.example.NativeLib is not foundjavadoc error: - Class com.example.NativeLib is not foundError: No classes were specified on the command line. Try -help

Please help me to solve this problem.Thanks in advance

October 20, 2011 3:24:47 AM PDT (17 weeks ago) [reply] [permalink]

Hi Arun Pachauri,

You have to build project first. Exactly, we need NativeLib.class first, it is a reason why weshould stand in bin directory.