Show me the code! – By Davanum Srinivas

December 9, 2007

Android – Invoke JNI based methods (Bridging C/C++ and Java)

Filed under: Uncategorized — Davanum Srinivas @ 11:23 pm

Here’s a screen shot


Step By Step Instructions below:

Step #1: Compile the project using ant. Some snippets are shown below. class (with Native method)

package org.apache;

import android.util.Log;

public class NativeAdd {
    static {
        try {
            Log.i("JNI", "Trying to load");
        catch (UnsatisfiedLinkError ule) {
            Log.e("JNI", "WARNING: Could not load");

    public static native long add(long a, long b);

Snippet that shows how this class/method is invoked from the main activity

    public void onClick(View view) {
        Log.i(LOG_TAG, "onClick");

        EditText a = (EditText) findViewById(;
        EditText b = (EditText) findViewById(;
        EditText c = (EditText) findViewById(;
        Log.i(LOG_TAG, "calling native method");
        long sum = NativeAdd.add(Long.parseLong(a.getText().toString()),
        Log.i(LOG_TAG, "back from native method");
        String text = Long.toString(sum);
        c.setText("Native add returns = " + text.subSequence(0, text.length()));

Step #2: Generate header files using java by running the following command.

javah -classpath ../../android.jar;../bin/classes; org.apache.NativeAdd

Here’s how the header file (org_apache_CallNative.h) looks like

/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class org_apache_NativeAdd */

#ifndef _Included_org_apache_NativeAdd
#define _Included_org_apache_NativeAdd
#ifdef __cplusplus
extern "C" {
 * Class:     org_apache_NativeAdd
 * Method:    add
 * Signature: (JJ)J
JNIEXPORT jlong JNICALL Java_org_apache_NativeAdd_add
  (JNIEnv *, jclass, jlong, jlong);

#ifdef __cplusplus

Step #3: Code a tiny C file (org_apache_NativeAdd.c) as shown below:

#include "org_apache_NativeAdd.h"

JNIEXPORT jlong JNICALL Java_org_apache_NativeAdd_add
  (JNIEnv *env, jclass c, jlong a, jlong b)
    return a + b;

Step #4: Please read the following article before you try the next step.
Shared library “Hello World!” for Android

Step #5: Compile and link the org_apache_NativeAdd.c/org_apache_NativeAdd.h into a shared library.

arm-none-linux-gnueabi-gcc  -I/usr/lib/jvm/java-1.5.0-sun/include -I/usr/lib/jvm/java-1.5.0-sun/include/linux  -fpic -c org_apache_NativeAdd.c
arm-none-linux-gnueabi-ld -T armelf_linux_eabi.xsc -shared -o org_apache_NativeAdd.o

NOTE: details on armelf_linux_eabi.xsc are in the Step #4.

Step #6: copy the library to the emulator. Copy the Application as well.
adb push native/ /system/lib
adb install bin/CallNative.apk

Step #7: Run the app to try some values. That's it!

Download the sources and Application -


  1. […] capture video or use location based services, you may however try to use JNI. Davanum Srinivas presents how to achieve this step by step. This way one can make part of his app partially native to gain performance, for instance in case […]

    Pingback by Do Android dream of something more than Java? « Mantrid’s Weblog — April 28, 2008 @ 3:53 am

  2. thanks for the infomation

    the download is not work, can you fix that?


    Comment by Yael — December 7, 2008 @ 6:16 am

  3. […] example […]

    Pingback by Yu-Teh Wiki: Jni — January 31, 2009 @ 8:36 pm

  4. […] Android by Davanum […]

    Pingback by Hello World на Си для андроида | Androider — March 14, 2009 @ 6:31 am

  5. […] some C (or C++) code (perhaps for speed-sensitive code paths)? A. One can do that, using JNI. A blog post on this topic. « Porting Android kernel to the TS-72xx […]

    Pingback by A few “Android FAQs for Beginners” Answered « KaiwanTech’s Blog — March 23, 2009 @ 3:02 am

  6. can you Explain this?
    “javah -classpath ../../android.jar;../bin/classes; org.apache.NativeAdd”
    what do you mean by “bin/classes”? and where should I store android.jar?

    Comment by paresh — April 2, 2009 @ 7:16 am

  7. Thanks for this very good introdurction to using JNI on Android.

    I am using Windows XP with JDK1.6.0 and I got some problems which I could solve. So here is a short description of how I got it running:

    Rewriting …/include/linux to …/include/win32 did not work:

    arm-none-linux-gnueabi-gcc  -I%JAVA_HOME%/include -I%JAVA_HOME%/include/win32 -fpic -c org_apache_NativeAdd.c

    The problem seems to be the declaration for __int64 (as long long) and the __stdcall and __declspec(x) compiler directive in “include/win32/jni_md.h”.
    I solved this by definig them on the command line:

    arm-none-linux-gnueabi-gcc -D__int64="long long" -D"__declspec(x)"=" " -D"__stdcall"=" "  -I %JAVA_HOME%/include -I %JAVA_HOME%/include/win32 -fpic -c org_apache_NativeAdd.c

    Now it works!

    For linking the file “armelf_linux_eabi.xsc” is needed.
    It can be found under the Sourcery G++ Lite program dir: …\arm-none-linux-gnueabi\lib\ldscripts\armelf_linux_eabi.xsc
    Using the default installation of Sourcery G++ Lite the link command is as follows:

    arm-none-linux-gnueabi-ld -T "C:\Programme\CodeSourcery\Sourcery G++ Lite\arm-none-linux-gnueabi\lib\ldscripts\armelf_linux_eabi.xsc" -shared -o de_hechler_jni_NativeAdd.o

    Next suprise: No acces right to copy into “/system/lib” using adb push

    Copied it to “/data”

    adb push /data

    and changed the loadLibrary in

    // OLD: System.loadLibrary("NativeAdd");

    And… SUCCESS!!! 🙂

    Hope this might help others to get things working under windows,

    Best regards,


    Comment by Ferenc Hechler — April 2, 2009 @ 9:37 am

    • The above step by Ferenc Hechler also worked fine for me too. Thanks for that. Just to add to that you can get arm-none-linux-gnueabi-gcc from for free having evaluation period of 30 days.
      Want to know, if the same .so library, that worked on windows will work on device?

      Comment by shruthirap — May 18, 2009 @ 7:32 am

    • i am getting jni.h :no such file or directory error

      Comment by satya — December 28, 2009 @ 1:44 am

  8. Thanks for the infomation!!!

    the download is not work, can you fix that?


    Comment by david — May 20, 2009 @ 4:03 am

  9. With emulator,I have push in to System/lib is OK, but with G1, i can’t push System/lib,

    Please help How to push into System/lib or using System.loadLibrary(“NativeAdd”);

    Comment by can't push in System/lib — June 3, 2009 @ 8:40 pm

  10. I have big problem . When I try to “push” my library on /system/lib, The emulator is OK, but The production T-mobile G1 tell me that partition is read only.
    Then, i’ve tried to push the library on another location and use the method system.load instead of system.loadlibrary. In this way I can load the library.

    Please tell me, How to do push my library in to System/lib in T-mobile G1 or using System.loadLibrary(“NativeAdd”) with T-mobile G1.



    Comment by pdquang — June 3, 2009 @ 9:30 pm

  11. […] Davanum Srinivas […]

    Pingback by Android JNI Blogs « Heejin Kim — June 17, 2009 @ 12:46 pm

  12. Screenshot doesn’t work 😦

    Comment by Mohammed Khoory — June 20, 2009 @ 12:42 am

  13. Hi All,

    I am a newbie on android platform.
    my query goes like this, I want to develop my api to the Android OS by submitting it to the Android OS source on my local machine.

    I have which has add() native method. Which i suppose need to be implemented in c/cpp.
    I do not understand
    1. where do i keep this java file in directory structure?
    2. do i need to create .class and .h files manually or build system takes care of it?
    3. where shud my reside?
    4. where do i keep cpp implementation file in dir stuctur?

    if anyone has some documents please fwd them to me.

    I hope u have understood what i am looking for.

    Thanks and regards,

    Comment by Anu — March 9, 2010 @ 5:45 am

  14. if you using System.loadLibrary(“mathlib”) please push to system/lib
    and if you use SystemLoad(“Path relative”) Please push to Path relative.

    Comment by Quang Pham — March 9, 2010 @ 10:07 pm

  15. You can also use JNIpp to simplify creations of Java class wrappers.
    That way you can have the whole application including activity and views in native code.

    Comment by Dmitry — June 10, 2011 @ 5:59 am

  16. Hi All,

    I am doing project in android in which I require libraries plus c code of linux..Will anyone guide me how am I include these codes and libraries in android??
    Thanks in Advance

    Comment by naeem898 — March 8, 2012 @ 12:36 pm

  17. hi,
    I have tried writing a jni call for the simple c code. when i try to install this on the phone running 2.3.3 it doesnt install, sometimes even if it installs then it is being force closed.Please help me with this. the code details are as follows:

    The java code of which i generate the header file.

    package com.hosa;

    public class edgejava{
    public native int main();

    the generated header file is as below

    /* DO NOT EDIT THIS FILE – it is machine generated */
    /* Header for class com_hosa_edgejava */

    #ifndef _Included_com_hosa_edgejava
    #define _Included_com_hosa_edgejava
    #ifdef __cplusplus
    extern “C” {
    * Class: com_hosa_edgejava
    * Method: main
    * Signature: ()I
    JNIEXPORT jint JNICALL Java_com_hosa_edgejava_main
    (JNIEnv *, jobject);

    #ifdef __cplusplus

    the implementation of native function is as below

    #include “com_hosa_edgejava.h”

    using namespace cv;

    JNIEXPORT jint JNICALL Java_com_pes_edgejava_main(JNIEnv *, jobject){
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened()) // check if we succeeded
    return -1;

    Mat edges;
    Mat frame;
    cap >> frame; // get a new frame from camera
    cvtColor(frame, edges, CV_BGR2GRAY);
    GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    Canny(edges, edges, 0, 30, 3);
    imshow(“edges”, edges);
    if(waitKey(30) >= 0) break;
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;

    the file

    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)

    include /home/srijith/android-opencv/OpenCV-2.3.1/share/OpenCV/

    LOCAL_MODULE := edgejava
    LOCAL_SRC_FILES := edgecpp.cpp
    LOCAL_LDLIBS += -llog -ldl



    APP_MODULES := edgejava
    APP_STL := gnustl_static
    APP_CPPFLAGS := -frtti -fexceptions

    the activity file is as follows

    package com.hosa;

    import android.os.Bundle;

    public class Andedge2Activity extends Activity {
    edgejava nativelib;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

    nativelib= new edgejava();
    int i=nativelib.main();
    System.out.println(“value returned to andedgeactivity “+i);

    All necessary changes to the manifest has been done like mention of use of camera and permissions has been set.

    Comment by gaurav — April 13, 2012 @ 3:11 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: