Android NDK for Windows setup guide

Introduction

Since most of the NDK tools are coming from the Linux environment, that OS was my first approach in building native C applications for Android. See this tutorial for a start.

However, I also needed to be able to compile JNI Android apps using the Windows environment. It proved an easy task, but there were some issues, so I decided to write this guide.

Install Android NDK on Windows and use it to compile a JAVA/JNI application

You will need an internet connection to install Cygwin and the Android SDK automatically from the Internet.

  • 1) Download the Android NDK for Windows
  • Download link.
    Current version of the file is: android-ndk-r4b-windows.zip 45MB

  • 2) Download Cygwin
  • Download link. (Direct Link)
    Select the mirror you need to download from then follow the installation guide.
    On package selection, Open Devel category and check “make”:
    2.1
    I’ve installed it to C:\cygwin

  • 3) Download the latest Android SDK for Windows
  • Download link.
    Current version is android-sdk_r06-windows.zip 23MB

  • 4) Install the SDK
  • 4.1) Unzip the zip content to a location on your harddisk. I choose:
    D:\work_code\android\android-sdk-windows\
    You will find SDK Setup.exe . Run it. I get a warning to install JAVA JDK
    4.2) Download Java JDK
    Download link.
    Select platform “Windows”. Current file is: jdk-6u21-windows-i586.exe (Direct link)
    4.3) Install the Java JDK: simply run the exe . It takes a few minutes to complete.
    4.4) Re-run the SDK Setup.exe from the Android SDK.
    This opens the Android SDK and AVD Manager. If you get a HTTPS Error:
    4.5.1
    go to Settings and check the “Force https:// …” checkbox:
    4.5.2
    4.5) Go to Available Packages, select all, and press Install Selected:
    4.6
    The installer will download the packages from the internet, and might prompt you to accept terms, which you must do if you want to continue with the installation. This step takes a few minutes.

  • 5) Install the NDK
  • 5.1) Unzip the NDK zip file. My choice is: D:\work_code\android\android-ndk-r4b\
    5.2) Configure Cygwin parameters
    Go to the Cygwin folder and open Cygwin.bat. It contains something like this:

    @echo off
    
    C:
    chdir C:\cygwin\bin
    
    bash --login -i
    

    Using any text editor change it to:

    @echo off
    
    set IS_UNIX=
    set DEV_ROOT=d:/work_code/android/BlueInput
    set JAVA_HOME=c:/Progra~1/Java/jdk1.6.0_21
    set CLASSPATH=d:/work_code/android/BlueInput/obj
    set PATH=D:/work_code/android/android-sdk-windows/tools;D:/work_code/android/android-ndk-r4b
    set ANDROID_NDK_ROOT=/cygdrive/d/work_code/android-ndk-r4b
    set NDK_PROJECT_PATH=/cygdrive/d/work_code/android/BlueInput
    
    C:
    chdir C:\cygwin\bin
    
    bash --login -i
    

    The path in the config, d:/work_code/android/BlueInput, is something I’m currently working on. Change to your Own project name. The project must be organized as follows:
    A) The JNI part (the .c and .h and Android.mk files) in:
    d:/work_code/android/BlueInput/jni
    B) The JAVA part (assets,bin,gen,jni,libs,obj,res,src with the .java files) in:
    d:/work_code/android/BlueInput
    For these paths always use forward slashes.
    5.3) Open Cygwin and browse to the NDK folder, using cd [enter] :
    5.4
    cd d:/work_code/android-ndk-r4b
    5.4) Type ./ndk-build to compile the JNI project
    5.5
    The C files have been successfully built. Now using Eclipse, you can build the Java android project, and it will include the JNI part automatically. Easy.

    This article has 36 Comments

    1. I am trying to write some part of an indoor navigation application in my HTC Desire with Android 2.2 is installed in. I am currently working on writing a bluetooth device detector. I am going to use bluetooth headsets whose mac addresses already known and try to detect whether they are visible or not. I would like only to ping all the bluetooth devices in the list I have. Isn’t there any way to do that without using “hcitool” or “l2ping” applications and parsing their results? I dont want to root my phone if it is possible.

    2. Hi Mehmet , thanks for posting your message here.

      You said “..and try to detect whether they are visible or not” . If they are visible (discoverable), you can do a bluetooth discovery to detect the nearby devices. Would this suit you?

      l2cap is not implemented on all Android devices and a general solution is currently problematic.

    3. The problem is, the devices I want to detect are not discoverable. They only stay discoverable for about 30 minutes, however I want them to use longer.

    4. if those remote devices as smartphones, you could write a service that runs on them and keeps a bluetooth host port open (serial, l2cap, etc).

      Then you can “detect” if they are in range by opening a connection to your predefined list of addresses.

    5. No they are not smartphones, they are just bluetooth headsets. So I am not able to do anything on the other side.

    6. Clearly what you need is L2CAP Ping. I’ve used it on Windows Mobile for a similar purpose and it works great.

      But on Android you’ll run into problems with L2CAP – it will only work on a few devices.

      If compatibility is not an issue for you, go for L2CAP Ping.

    7. I am using an HTC Desire, I did not found “l2ping” inside the file system, is it possible to push “l2ping” somehow into the filesystem without root access and use it?

    8. Unfortunately, the Desire is one of the devices that doesn’t have L2CAP support.

    9. No, it won’t work. The BlueZ stack installed on the Desire seems to be incomplete.

    10. Hi!

      I’m trying to write an application that reads the rssi value of a connection, but I have difficulties connecting to the phone using MAC address.
      I managed to build my project using ndk r5b, and I see the logs written in my C code, so the building is complete I think, but every time I want to connect to a device, my phone returns “can’t connect’. I copied all the needed header files into build path and libbluetooth.so too, rooted my phone using z4root and granted superuser rights to the app using “Process p=Runtime.getRuntime().exec(“su”);”(so my app asked for superuser rights and I granted it to it), but I couldn’t connect to any device so far using this code for connection:

      jint Java_com_bluetooth_RSSI_connectbt(JNIEnv* env, jobject this, jstring mac)
      {
      int s, status;
      int dd;
      int i;
      struct sockaddr_rc addr = { 0 };
      struct hci_conn_info_req *cr;
      char * dest;// = “00:1E:E1:7D:54:B4”;
      struct timeval timeout;

      dest = (*env)->GetStringUTFChars(env, mac, 0);

      __android_log_print(ANDROID_LOG_INFO, TAG, “rfcomm %s”, dest);
      // allocate a socket
      s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

      // set the connection parameters (who to connect to)
      addr.rc_family = AF_BLUETOOTH;
      addr.rc_channel = (uint8_t) 1;
      str2ba( dest, &addr.rc_bdaddr );

      // connect to server
      __android_log_print(ANDROID_LOG_INFO, TAG, ” Trying to connect”);

      //setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval));

      status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
      if (status == 0){
      __android_log_print(ANDROID_LOG_INFO, TAG, ” CONNECTED!!!”);
      return s;
      }
      __android_log_print(ANDROID_LOG_INFO, TAG, ” no connect”);
      return -1;
      }

      I get every time
      04-17 11:08:11.524: INFO/native_service(7973): Trying to connect
      04-17 11:08:11.524: INFO/native_service(7973): no connect

      Do you have any ide, what should be the problem?
      Thanks in advice

      Peter

    11. Oh, and I’m using Samsung Galaxy S(rooted, SDK 2.1) and tryed to connect to several phones/laptops.
      Peter

    12. What if our pathes contain spaces ? I tried everything I could found (backslash before space, 40 instead of space, path between quotes,…) but it didn’t help.

    13. I followed the steps, i’m getting following error,

      Android NDK: Your APP-BUILD_SCRIPT points to an unknown file: /cygdrive/c/ndkworkspace/ndkfoo/jni/Android.mk
      /cygdrive/c/android-ndk-r5b/build/core/add-appplication.mk:116: *** Android NDK:Aborting… .Stop.

      Strugling hard to fix this. Please suggest.

    14. wat does obj contain i thing it has object file where do i get this file…
      can any body help me…

    15. @Nikhil: Same here. Did you ever resolve it?

      $ ndk-build
      ERROR: You are using a non-Cygwin compatible Make program.
      Currently using: C:/utils/mingw/bin/mingw32-make.exe

      To solve the issue, follow these steps:

      1. Ensure that the Cygwin ‘make’ package is installed.
      NOTE: You will need GNU Make 3.81 or later!

      2. Define the GNUMAKE environment variable to point to it, as in:

      export GNUMAKE=/usr/bin/make

      3. Call ‘ndk-build’ again.

      Administrator ~
      $ c:/utils/mingw/bin/mingw32-make.exe –version
      GNU Make 3.81
      Copyright (C) 2006 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions.
      There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

      This program built for i386-pc-mingw32

    16. i am having problem in my cygwin does not appear as here in @echo off instead is appear my laptop name that’s why the command chdir c:\cygwind\bin does not works what should i do to fix this please help me as soon as possible

    17. Hi, i am able to build using ndk-build from the project directory using cygwin in Windows 7, after that i refresh my eclipse project and try to run the android app.
      Still i am getting a force close with the following exception:

      12-07 17:17:23.260: E/AndroidRuntime(713): FATAL EXCEPTION: main
      12-07 17:17:23.260: E/AndroidRuntime(713): java.lang.ExceptionInInitializerError

      12-07 17:17:23.260: E/AndroidRuntime(713): Caused by: java.lang.UnsatisfiedLinkError: Couldn’t load test-jni: findLibrary returned null

    18. Hi,
      It’s a good thing I came across this. Thanks for posting. Will I be able to get an idea from this on how I can pair and connect my android phone with a bluetooth mouse? Hope you can advise and guide me.
      Thanks and have a nice day.
      Joel

    19. hi,

      thanks for this tuto,

      so i have to change the cygwin.bat forr every project?

    Leave a Reply