Using NDK to Call C code from Android Apps

Posted on Nov 25, 2009 (4 years ago). Seen 142,307 times. 33 comments. Permalink Feed
Photo Marko Gargenta
@MarkoGargenta
Member since Jan 19, 2007
Location: San Francisco
Stream Posts: 42
Tagged as: Android Tutorial
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:

1. Create the Java class representing the native code
2. Create the native code header file
3. Implement the native code by writing your C code
4. Compile everything and build you Shared Library
5. 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 as the glue to the native code that we'll write later.


/src/com.marakana/NativeLib.java
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();
}



Create C Header File

n your project bin directory (in my case, <EclipseWorkspace>/NDKDemo/bin), run javah tool to 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:

NDKDemo/bin$ javah -jni com.marakana.NativeLib
NDKDemo/bin$ mv com_marakana_NativeLib.h ../jni/


Write the C Code

In your <EclipseWorkspace>/NDKDemo/jni/ folder, create ndk_demo.c file. This is where we'll implement the native code. To start, copy the function signatures from the header file, and provide the implementation for those functions. In this example, the header file looks like this:


<EclipseWorkspace>/NDKDemo/jni/com_marakana_NativeLib.h
Code:

/* 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 __cplusplus
extern "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
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

And the corresponding implementation looks like this:


<EclipseWorkspace>/NDKDemo/jni/ndk_demo.c
Code:

#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);
}



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.mk
Code:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := ndk_demo
LOCAL_SRC_FILES := ndk_demo.c

include $(BUILD_SHARED_LIBRARY)

Next, we need to tell NDK how to build the shared library and put it in the correct place inside the Eclipse project. To do this, create a folder <NDKHOME>/apps/ndk_demo/ and inside this folder create the Application file:


<NDKHOME>/apps/ndk_demo/Application
Code:

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

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 to copy entire Eclipse project into <NDKHOME>/apps/ndk_demo/project directory, then copy back to 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:

android-ndk-1.5_r1$ make APP=ndk_demo
Android NDK: Building for application 'ndk_demo'
Compile thumb : ndk_demo <= sources/ndk_demo/ndk_demo.c
SharedLibrary : libndk_demo.so
Install : libndk_demo.so => apps/ndk_demo/project/libs/armeabi

You can now refresh your Eclipse project and you should /lib/ directory containing your libndk_demo.so file.


Calling Native Code from Java

So now that we have the native C library implemented, compiled, and placed in the right place, let's see how we can call it from our Activity. It's actually rather simple - you just have to instantiate the instance of your NativeLib class and from there on, it's just a regular Java object.


/src/com.marakana/NDKDemo.java
Code:

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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

nativeLib = new NativeLib();
String helloText = nativeLib.hello();

// Update the UI
TextView outText = (TextView) findViewById(R.id.textOut);
outText.setText(helloText);

// Setup the UI
Button buttonCalc = (Button) findViewById(R.id.buttonCalc);

buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(R.id.result);
EditText value1 = (EditText) findViewById(R.id.value1);
EditText value2 = (EditText) findViewById(R.id.value2);

public void onClick(View v) {
int v1, v2, res = -1;
v1 = Integer.parseInt(value1.getText().toString());
v2 = Integer.parseInt(value2.getText().toString());

res = nativeLib.add(v1, v2);
result.setText(new Integer(res).toString());
}
});
}
}


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


/res/layout/main.xml
Code:

<?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"
android:layout_height="wrap_content" android:id="@+id/value2"
android:hint="Value 2"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/buttonCalc"
android:text="="></Button>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="result"
android:textSize="36sp" android:id="@+id/result"></TextView>
</LinearLayout>


Output


Source Code

http://thenewcircle.com/static/tutorials/NDKDemo.zip
http://thenewcircle.com/static/tutorials/NDKHOME.zip

Comments

Posted on Jul 9, 2010
Photo Doug Hegge
Technical Architect
JDH Consulting, Inc.
Member since Jul 9, 2010
Nice article I am planning todo just this to load a kernel module that I have already developed to take the funtional place of IPTABLES my only question is when I call the native C module which contains the C equivelant of insmod / rmmod how do I believe I have to have root access to pull this off dont I.

Obviously the Android OS itself does this for the WIFI driver via the wifi.c module
as 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 android build 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 RPC from an Android GUI application to change the status load/unload my kernel module.


Thanks,
Doug
doug@jdhcon.com
Posted on Jul 12, 2010
Photo Marko Gargenta
@MarkoGargenta
Member since Jan 19, 2007
Location: San Francisco
NDK allows you to put native code insite your application, BUT that native code is still subject to 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 a linux OS. You want to put your libraries in /system/lib directory.
Posted on Sep 19, 2010
Photo Joe Knapp
Lucent
Member since Sep 19, 2010
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
Posted on Sep 29, 2010
Photo Munir Hoque
RUET
Member since Sep 29, 2010
@Marko Gargenta - Boss, is it possible to develop all parts of Android Application Development using C++. (Actually, I know nothing about Android and JAVA)
Posted on Nov 12, 2010
Photo Mohamed M
Member since Nov 12, 2010
Hy
How can I use libmms in android project with NDK. I have mms.c and mms.h from http://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
Posted on Nov 12, 2010
Photo Mohamed M
Member since Nov 12, 2010
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
*/
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
Posted on Nov 16, 2010
Photo William Wu
Wistron
Member since Nov 16, 2010
Perfect,and thank you for your sharing
Posted on Nov 24, 2010
Photo Archana Udayanan
chs,India
Member since Nov 21, 2010
hey i am very new to android.Still it wasn't really tough following the steps.Its actually made quite simple by you.Thanks a lot.
But the thing is its not completely working out fine.my application is stucking out at a point when i click for final output.I think the OnClickListener call is posing a problem.kindly help....
Posted on Nov 26, 2010
Photo Muhammad Kamran Afridi
Student- TU ilmenau, Germany
Member since Nov 26, 2010
Can anyone please help me in this part, please elaborate it a bit more...I am new to all of this stuff, 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:"
Posted on Dec 22, 2010
Photo Rajiv Dayananda
MindTree
Member since Dec 22, 2010
superb tutorial thank you
Posted on Feb 9, 2011
Photo Ashok Marannan
Using NDK to Call C code from Android Apps
Student
Member since Feb 8, 2011
Hi this is great and i got it working!! but i would like to incorporate assembly language code in the 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"
:"=a"(value1)
:"a"(value1), "b"(value2)
);


return (value1);
}

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

Android NDK: Building for application 'ndk_demo'
Compile thumb : ndk_demo <= ndk_demo.c
apps/ndk_demo/project/jni/ndk_demo.c: In function 'Java_com_marakana_NativeLib_a
dd':
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
Posted on Jun 2, 2011
Photo Al Ramet
Developer
Free Lance
Member since Jun 2, 2011
Hello,
I am using linux Ubuntu with Eclipse.
I am trying to create an app using NDK to access USB port
I tried to include libusb and have not been successfull yet.
I used Java JNI with .c program and were able to access usb port
but 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 using Android NDK in Eclipse Env. please posted

Thanks
A.
Posted on Jun 3, 2011
Photo Thilagaraj M
Developer
Member since Jun 3, 2011
hi
I dont about the jni brifely but i know i have one doubt regarding the JNI i am using android sdk some one told write jni in android NDK only.in sdk its is possible
please tell me how to write the jni in android

Thanks
Thilak.
Posted on Jun 3, 2011
Photo Thilagaraj M
Developer
Member since Jun 3, 2011
hi

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


Thanks
Thilak
Posted on Jul 4, 2011
Photo Gachon Mickael
Student
Member since Jul 4, 2011
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.mk file 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
Posted on Jul 13, 2011
Photo Vigneshwaran Sekar
Embedded
Member since Jul 13, 2011
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?
Posted on Aug 2, 2011
Photo Ganga sai Pradeep Reddy Peddamallu
Self
Member since Jul 25, 2011
Hello,



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




# logcat
--------- beginning of /dev/log/system
I/ActivityManager( 1002): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.mms/.ui.ConversationList } from pid 1097
W/InputManagerService( 1002): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40732108 (uid=10025 pid=1234)
I/ActivityManager( 1002): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.uandroid.ndkdemo/.NdkDemo } from pid 1097
I/ActivityManager( 1002): Start proc com.uandroid.ndkdemo for activity com.uandroid.ndkdemo/.NdkDemo: pid=5163 uid=10031 gids={1015}
E/AndroidRuntime( 5163): FATAL EXCEPTION: main
E/AndroidRuntime( 5163): java.lang.UnsatisfiedLinkError: helloDev
E/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): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 5163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
E/AndroidRuntime( 5163): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
E/AndroidRuntime( 5163): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 5163): at android.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): at com.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/.NdkDemo
W/ActivityManager( 1002): Activity pause timeout for HistoryRecord{4068e450 com.uandroid.ndkdemo/.NdkDemo}
W/ActivityManager( 1002): Activity destroy timeout for HistoryRecord{4068e450 com.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
Posted on Aug 8, 2011
Photo Arun Kumar
ethics technologies
Member since Aug 8, 2011
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.
Posted on Sep 8, 2011
Photo Sujit Achayra
Software Engineer
Developer
Member since Sep 8, 2011
Mr Arun,

Have you created the steps

Here two make file is required.

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

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

or copy whole project to NDKHome Directory.
Posted on Sep 10, 2011
Photo Arun Kumar
ethics technologies
Member since Aug 8, 2011
Hi sujit,

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

Thank you..,
Posted on Sep 19, 2011
Photo Fadi Souilem
Student
INSAT
Member since Sep 19, 2011
Hello Marko,

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

could you help me about this, please ?

many thanks in advance.
Posted on Oct 7, 2011
Photo Arun Pachauri
Software Engineer
Samsung
Member since Oct 7, 2011
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.NativeLib
classfile for com.example.NativeLib is not found
javadoc error: - Class com.example.NativeLib is not found
Error: No classes were specified on the command line. Try -help


Please help me to solve this problem.Thanks in advance
Posted on Oct 20, 2011
Photo Cuong Thai
HTK-INC
Member since Oct 20, 2011
Hi Arun Pachauri,

You have to build project first. Exactly, we need NativeLib.class first, it is a reason why we should stand in bin directory.
Posted on Mar 14, 2012
Photo April Randolph
Programmer
Personal
Member since Mar 14, 2012
Hey Marko,
I have started using the Native Development Kit as of recently only to find errors with the zip files for the NewsReader example. After installing API13-15 thru eclipse I get the 'can not find target android-4'. Any suggestions? I'm running the latest JDK 1.7 64Bit Indigo.
Posted on Mar 17, 2012
Photo Arun Kumar
ethics technologies
Member since Aug 8, 2011
w
Posted on Mar 17, 2012
Photo Arun Kumar
ethics technologies
Member since Aug 8, 2011
Hai Arun Pachauri,

i want to know the contents of the bin folder(in detail with sub-folders ,if any), of your project..
Posted on Jun 7, 2012
Photo Alejandro Pagani Bonnot
-
Propia
Member since Jun 7, 2012
Hello achievement to walk around, but when I run to see the results in the emulator throws me the following error

it can be?

# logcat
--------- beginning of /dev/log/system
I/ActivityManager( 1002): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.mms/.ui.ConversationList } from pid 1097
W/InputManagerService( 1002): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40732108 (uid=10025 pid=1234)
I/ActivityManager( 1002): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.uandroid.ndkdemo/.NdkDemo } from pid 1097
I/ActivityManager( 1002): Start proc com.uandroid.ndkdemo for activity com.uandroid.ndkdemo/.NdkDemo: pid=5163 uid=10031 gids={1015}
E/AndroidRuntime( 5163): FATAL EXCEPTION: main
E/AndroidRuntime( 5163): java.lang.UnsatisfiedLinkError: helloDev
E/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): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 5163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
E/AndroidRuntime( 5163): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
E/AndroidRuntime( 5163): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 5163): at android.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): at com.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/.NdkDemo
W/ActivityManager( 1002): Activity pause timeout for HistoryRecord{4068e450 com.uandroid.ndkdemo/.NdkDemo}
W/ActivityManager( 1002): Activity destroy timeout for HistoryRecord{4068e450 com.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
Posted on Jul 5, 2012
Photo Andy Madden
RIT
Member since Jul 5, 2012
Hey,
So I'm still literally at like the second step. I created my NDKDemo.java file, and my NativeLib.java file. Now when I call javah -jni com.marakna.NativeLib, it tells me the file could not be found.

andrew@Win7Spare /cygdrive/c/Users/andrew/workspace/NDKDemo/bin
$ javah -jni.marakana.NativeLib
Error: Could not find class file for com.marakana.NativeLib

However, I am positive that it is within the src folder. Please let me know if you can help. Thanks.
Posted on Jul 5, 2012
Photo Alejandro Pagani Bonnot
-
Propia
Member since Jun 7, 2012
Hi Andy Madden, look I think your error is in one more point, the command would be
$ Javah-jni marakana.NativeLib
no point of Marakana Deante.

PS: Sorry for my English
Posted on Jul 6, 2012
Photo Andy Madden
RIT
Member since Jul 5, 2012
That was a typo in my post, it WAS -jni marakana.NativeLib

Fortunately, I realized that my I already have my full C code which compiles, so I will not need this step.

Thank you for your quick response.
Posted on Jul 10, 2012
Photo Ross Yoo
Harvard
Member since Jul 10, 2012
Thank you, Marko, for the helpful guide.
Posted on Sep 18, 2012
Photo Mitesh Sardhara
Appmax Technologies
Member since Sep 18, 2012
Hi Marco,

I have one .dll file which I need to use for Encryption in Android Project. Can you tell about how to use .dll file in android project as I am new to andorid development. Although I have Searched for the answer but still not getting proper solution. Any help is appreciated.

Thanks.
Posted on Jul 9, 2013
Photo Rahul S
CDAC
Member since Jul 9, 2013
Hi,
I am developing my android application in Ecclipse ADT using NDK. I have written a native code in c. My native code is look like this :

#include "com_adroidappl_androidagent_ServerThread.h"
#include <stdio.h>
#include <fcntl.h>
jint fd;
void ReadMemory(jbyte* buf, jint bufflength){

fd=open("/dev/block/mmcblk0",O_RDONLY);
read(fd,buf,bufflength);
}
JNIEXPORT jlong JNICALL Java_com_adroidappl_androidagent_ServerThread_AndroidImager
(JNIEnv *env, jobject obj, jbyteArray buffer){

jbyte* buf;
jsize buflngth;

buf= (jbyte*)GetByteArrayElements(env, buffer,NULL);
buflngth= GetArrayLength(env, buffer);
ReadMemory(buf,buflngth);
return 1;
}

In my native code i am using JNI built in APIs, GetByteArrayElements() & GetArrayLength(). But when i am compiling my source file i am getting following linking error:
1. undefined reference to 'GetByteArrayElements'.
2. undefined reference to 'GetArrayLength'.
I have googled a lot. by i could n't find proper answr. Please help me