PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14793 Views | Rate 70.44
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 17667 Views | Rate 57.55
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4768 Views | Rate 57.45
NMEA GPS Library for AVR Microcontrollers
Developing for Blackberry 10 | 52 Views | Rate 52
Developing for Blackberry 10
Building a robot – Part 2 | 4522 Views | Rate 44.77
Building a robot – Part 2
Programmatically Injecting Events on Android - Part 2 | 4834 Views | Rate 44.35
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 15818 Views | Rate 41.3
Simple Switched power Supplies
Repairing a Victoreen CDV-700 6B Dosimeter  | 118 Views | Rate 39.33
Repairing a Victoreen CDV-700 6B Dosimeter

 
  

Android NDK for Windows setup guide


By Radu Motisan Posted on August 24th, 2010 , 5787 Views (Rate 5.79)



  




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:

    1.  
    2. @echo off
    3.  
    4. C:
    5. chdir C:\cygwin\bin
    6.  
    7. bash --login -i
    8.  

    Using any text editor change it to:

    1.  
    2. @echo off
    3.  
    4. set IS_UNIX=
    5. set DEV_ROOT=d:/work_code/android/BlueInput
    6. set JAVA_HOME=c:/Progra~1/Java/jdk1.6.0_21
    7. set CLASSPATH=d:/work_code/android/BlueInput/obj
    8. set PATH=D:/work_code/android/android-sdk-windows/tools;D:/work_code/android/android-ndk-r4b
    9. set ANDROID_NDK_ROOT=/cygdrive/d/work_code/android-ndk-r4b
    10. set NDK_PROJECT_PATH=/cygdrive/d/work_code/android/BlueInput
    11.  
    12. C:
    13. chdir C:\cygwin\bin
    14.  
    15. bash --login -i
    16.  

    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.






      

    More on PocketMagic:

    How to make metallic sodium | 662 Views | Rate 30.09
    How to make metallic sodium
    Tube: USN-5J29 | 308 Views | Rate 28
    Tube: USN-5J29
    Programmatically Injecting Events on Android - Part 1 | 9947 Views | Rate 25.84
    Programmatically Injecting Events on Android - Part 1
    Atmega8 and enc28J60 for ethernet support | 7100 Views | Rate 24.23
    Atmega8 and enc28J60 for ethernet support
    How to set the AVR Fusebits | 1647 Views | Rate 23.87
    How to set the AVR Fusebits
    Tube: 1B40 | 234 Views | Rate 23.4
    Tube: 1B40

    36 Responses to “Android NDK for Windows setup guide”

    1. 1
      Radu Motisan:

      For debug output use ./ndk-build NDK_LOG=true APP=BlueInput V=1

    2. 2
      George:

      Thanks. That helper a lot.

    3. 3
      PocketMagic » Android C native development using the NDK under Windows:

      [...] NDK has been released and the JNI development is not only possible, but also easy. Have a look on this article on the Android NDK. The new challenge is to use the NDK to compile Native ARM-EABI executables that [...]

    4. 4
      Nikhil:

      I get the error that GNUMAKE must be defined. for a sample app.
      I am using android NDK r5

    5. 5
      PocketMagic » Developing for Android – the SDK – update 2011:

      [...] Some additional resources: http://www.pocketmagic.net/?p=665 http://developer.android.com/sdk/installing.html http://www.pocketmagic.net/?p=1332 [...]

    6. 6
      Android NDK: Wie verwenden? - Android-Hilfe.de:

      [...] AW: Android NDK: Wie verwenden? schau dir mal folgendes Tutorial an: Android NDK for Windows setup guide « PocketMagic [...]

    7. 7
      Mehmet Yildirim:

      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.

    8. 8
      Radu Motisan:

      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.

    9. 9
      Mehmet Yildirim:

      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.

    10. 10
      Radu Motisan:

      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.

    11. 11
      Mehmet Yildirim:

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

    12. 12
      Radu Motisan:

      I see, unfortunately you can’t (yet) relay on L2CAP either.

    13. 13
      Mehmet Yildirim:

      Do you have any suggestions for my case then??

    14. 14
      Radu Motisan:

      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.

    15. 15
      Mehmet Yildirim:

      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?

    16. 16
      Radu Motisan:

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

    17. 17
      Mehmet Yildirim:

      Does it make sense to root my device and push “l2ping” inside?

    18. 18
      Radu Motisan:

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

    19. 19
      Peter:

      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

    20. 20
      Peter:

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

    21. 21
      Karen:

      Thanks man. This got me up and running on Windows.

    22. 22
      Radu Motisan:

      @Karen, you’re welcome, glad it helped

    23. 23
      Kak0:

      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.

    24. 24
      kaps:

      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.

    25. 25
      kaps:

      Sorry for the typo error. Its APP_BUILD_SCRIPT.

    26. 26
      mvinay:

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

    27. 27
      Jan Ehrhardt:

      @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

    28. 28
      adam:

      can i use this to change an imei?

    29. 29
      Radu Motisan:

      I don’t think you can.

    30. 30
      Loy:

      In what version of the windows does this work? Works on 7 too?

    31. 31
      shah hassan:

      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

    32. 32
      Building Pocketsphinx For Android - Android-Hilfe.de:

      [...] [...]

    33. 33
      SpiritMts:

      Thank you Very much

    34. 34
      SKC:

      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

    35. 35
      Joel Nuesca:

      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

    36. 36
      azizachom:

      hi,

      thanks for this tuto,

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

    Thank you for commenting. Your comment won't show until approved, which can take some time.

    Please copy the string rChqHz to the field below: