Using cygwin with the Android NDK on Windows

Using cygwin with the Android NDK on Windows

This tutorial illustrates how to setup and use the Android NDK under Windows. It will use cygwin for compiling the native code. It has been tested on Windows XP and Windows 7.

This guide assumes that you have Eclipse with ADT and the Android SDK version 3 (1.5) up and running.

There are three important paths:

Eclipse Workspace       D:\Dev\workspace-android
NDK                     D:\Dev\SDKs\android-ndk-r4b
Cygwin                  C:\Cygwin

I am using these paths as they appear on my computer. Please adapt them to use system if necessary. Note that the paths MUST NOT CONTAIN SPACES.

The code for this tutorial is available here

Download NDK

Go to http://developer.android.com/sdk/ndk/index.html and download the Android NDK for Windows. At the time of writing, android android-ndk-r4b was the latest version. Copy the folder into D:\dev\SDKs\

Install Cygwin

Download setup.exe from http://cygwin.com/. The direct link ishttp://cygwin.com/setup.exe. Execute setup.exe and select a server to download the Cygwin files from. Then a huge list appears where you can select the components to download.

Add “Devel/make” and “Shells/bash”. Search for “make” and “shell” to find them. Press next to download. I installed all files to C:\cygwin.

Using cygwin with the Android NDK on Windows_第1张图片

Using cygwin with the Android NDK on Windows_第2张图片

Create Android Project

Create a new standard Android project. I called it HelloNDK and used the following code.

package org.pielot.hellondk;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class HelloNDK extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		System.loadLibrary("hellondk");
		int result = sayHello();
		Log.i("HelloNDK", "" + result);
	}

	private native int sayHello();
}

Make sure to compile it using the Android SDK. Javac.exe won’t work! Running this code should fail, as there is no native library yet.

Create Java Native Interface

Make sure your PATH contains your Java SDKs /bin directory. Open a terminal (cmd.exe) and enter javah. Receiving something like this means everything is alright.

Using cygwin with the Android NDK on Windows_第3张图片

Now, in the terminal, go to the root of your project in Eclipse’s workspace D:\Dev\workspace-android\HelloNDK. Enter

javah.exe -classpath bin/classes -d jni org.pielot.hellondk.HelloNDK

When no error occurs, the compilation was successful. Since some comments below report from difficulties with this step, you might want to check if HelloNDK.class is really located in bin/classes/org/pielot/hellondk/. If not, try changing bin/classes/… to bin/… or src/… . It appears to have worked for others.

Now you should find a new folder jni in your project, containing the file org_pielot_hellondk_HelloNDK.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class org_pielot_hellondk_HelloNDK */

#ifndef _Included_org_pielot_hellondk_HelloNDK
#define _Included_org_pielot_hellondk_HelloNDK
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     org_pielot_hellondk_HelloNDK
 * Method:    sayHello
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_org_pielot_hellondk_HelloNDK_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

For my convenience, I usually create a .bat file including the above javah command and put it into the project folder.

Implement the Native Interface

Now we have to provide an implementation of the generated header file. Create org_pielot_hellondk_HelloNDK.c in the same folder as org_pielot_hellondk_HelloNDK.h and fill it with:

#include "org_pielot_hellondk_HelloNDK.h"

JNIEXPORT int JNICALL Java_org_pielot_hellondk_HelloNDK_sayHello
 (JNIEnv * env, jobject obj) {

 return 42;
}

Inform the Compiler what Files to Compile

Next, we have to inform the NDK compiler what files should be compiled. In the /jni folder we therefore create “Android.mk”:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hellondk
LOCAL_SRC_FILES := org_pielot_hellondk_HelloNDK.c

include $(BUILD_SHARED_LIBRARY)

Compiling the Native Code

In the HelloNDK project folder create a batch file named “make.bat”. Fill it with:

@echo on

@set BASHPATH="C:\cygwin\bin\bash"
@set PROJECTDIR="/cygdrive/d/dev/workspace-android/hellondk"
@set NDKDIR="/cygdrive/d/dev/SDKs/android-ndk-r4b/ndk-build"

%BASHPATH% --login -c "cd %PROJECTDIR% && %NDKDIR%

@pause:

This file will NOT WORK WHEN EXECUTED FROM WITHIN ECLIPSE. Thus, always find it with the Windows Explorer and execute it by double-clicking.

If successful it should look like

Using cygwin with the Android NDK on Windows_第4张图片

In the HelloNDK project directory you now should find libs/armeabi/libhellondk.so created.

Loading and Testing library

If you now run the HelloNDK Activity, you should see no exceptions. In LogCat, something like

12-05 13:42:45.311: INFO/HelloNDK(24329): 42

should appear. Voila! You have done it!

One last note: if you are just working on the C code, Eclipse will not realize that the native lib has been updated. To make sure that the latest version of the lib will be used, mark the HelloNDK project in Eclipse’s project explorer and hit F5.

This entry was posted in  Android,  NDK and tagged  android,  cygwin,  ndk,  windows on  December 9, 2010.

Post navigation

 KISSing and Shopping OpenAL on Android 

21 thoughts on “Using cygwin with the Android NDK on Windows

  1. Pingback: OpenAL on Android | Martin's Blog

  2. Pingback: Rohit Krishnan » Setting Up Android NDK

  3. SunnyFebruary 6, 2012 at 11:51

    C:\Program Files\Java\jdk1.7.0\bin>javac D:\java\HelloWorld.java

    C:\Program Files\Java\jdk1.7.0\bin>javah -jni D:\java\HelloWorld.class
    Exception in thread “main” java.lang.IllegalArgumentException: Not a valid class
    name: D:\java\HelloWorld.class
    at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:177)
    at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:68)
    at com.sun.tools.javah.JavahTask.run(JavahTask.java:509)
    at com.sun.tools.javah.JavahTask.run(JavahTask.java:335)
    at com.sun.tools.javah.Main.main(Main.java:46)

    Reply 
    1. martinPost authorFebruary 6, 2012 at 12:51

      Dear Sunny,

      the code was written for Android OS. Your post said that you used
      javac D:\java\HelloWorld.java
      which lets me assume you have used the standard Java compiler to compile the class. This should not work, since standard Java does not contain Android specific classes, such as “Activity”.

      The solution is to set the project up as an Android project. Please checkhttp://developer.android.com/resources/tutorials/hello-world.html if you’d like to try that.

      Martin

      Reply 
      1. SunnyMarch 5, 2012 at 10:43

        do u know how to open a website in android ndk…

        Reply 
  4. danh_hungMarch 9, 2012 at 05:42

    why i run “javah.exe -classpath bin -d jni org.pielot.hellondk.HelloNDK”
    this is result:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\Internship>javah
    Usage:
      javah [options] 
    where [options] include:
      -o                 Output file (only one of -d or -o may be used)
      -d                  Output directory
      -v  -verbose             Enable verbose output
      -h  --help  -?           Print this message
      -version                 Print version information
      -jni                     Generate JNI-style header file (default)
      -force                   Always write output files
      -classpath         Path from which to load classes
      -bootclasspath     Path from which to load bootstrap classes
     are specified with their fully qualified names
    (for example, java.lang.Object).
    
    C:\Users\Internship>cd D:\Dev\workspace-android\HelloNDK
    
    C:\Users\Internship>D:
    
    D:\Dev\workspace-android\HelloNDK>javah.exe -classpath bin -d jni org.pielot.hel
    londk.HelloNDK
    Error: Could not find class file for 'org.pielot.hellondk.HelloNDK'.

    please help !

    Reply 
    1. martinPost authorMarch 9, 2012 at 11:20

      Dear Danh Hung,

      it appears that javah.exe cannot find HelloNDK.class in the /bin folder. Can you please verify that the source code has been compiled to .class files?

      Martin

      Reply 
      1. danh_hungMarch 9, 2012 at 15:15

        thank for your reply!
        i do it step by step as above. in folder D:\Dev\workspace- android\HelloNDK\bin has:

        classes\org\pielot\hellondk\HelloNDK.class
         classes\org\pielot\hellondk\R$attr.class
         classes\org\pielot\hellondk\R$drawable.class
         classes\org\pielot\hellondk\R$layout.class
         classes\org\pielot\hellondk\R$string.class
         classes\org\pielot\hellondk\R.class
         res\...\*.png
         classes.dex
         HelloNDK.apk
         resources.ap_

        so something wrong ?

        Reply 
        1. martinPost authorMarch 9, 2012 at 15:55

          I believe the “classes” folder is the problem. Since the CLASSPATH is …\bin, “classes” will be treated as package, as if it was classes.org.pielot.hellondk.HelloNDK.

          Either you remove “classes”: …\bin\org\… not …\bin\classes\org… .

          or you add “classes” to the classpath by using the option “-classpath bin/classes” instead of “-classpath bin”.

          Reply 
          1. danh_hungMarch 9, 2012 at 18:29

            thank u very much! however i don’t solve my new problem. i user this code:

            D:\Dev\workspace-android\HelloNDK>javah.exe -classpath "D:\Dev\android-sdk-window\platforms\android-10\android.jar"  bin/classes -d jni org.pielot.hellondk.HelloNDK
            
            then this is result:
            Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class
             name: bin/classes
                    at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:177)
                    at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:68)
                    at com.sun.tools.javah.JavahTask.run(JavahTask.java:509)
                    at com.sun.tools.javah.JavahTask.run(JavahTask.java:335)
                    at com.sun.tools.javah.Main.main(Main.java:46)

            i’m searching on google!

          2. martinPost authorMarch 12, 2012 at 11:51

            I saw this exception once. The java code had been compiled using standard java, while it should have been compiled with the Android SDK. Did you maybe use javac.exe to compile the java classes?

          3. OdiriussJune 4, 2012 at 02:22

            I had the same problem as danh_hung. After some digging in my project folder i notice that there is a java file with the name of the class i should be using in the src folder. After trying with that file it all ran smoothly. So the command in cmd in my case looked like this:

            javah.exe -classpath src -d jni org.pielot.hel
            londk.HelloNDK

            I hope it helps someone… Thanks for the tutorial! Cheers

  5. KO KO LAYJuly 17, 2012 at 13:48

    Thaks a lot.

    Reply 
  6. kirtiDecember 5, 2012 at 14:35

    Android NDK: NDK Application ‘local’ targets unknown ABI(s): armeabi
    D:/Software/ndk/build/core/setup-app.mk:63: *** Android NDK: Aborting . Stop.
    Android NDK: Please fix the APP_ABI definition in D:/Software/ndk/build/core/default-application.mk . hay everything is works fine but when i ran the make.bat file it shows the above error.

    Reply 
  7. IndranilJanuary 7, 2013 at 21:08

    I am facing a problem. When I execute the make.bat I get

    Android NDK: Could not find application project directory !
    Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.

    In cygwin.bat I have already set NDK_PROJECT_PATH but still this serror. Could you please give some pointers?

    Reply 
  8. srivathsaJanuary 9, 2013 at 14:26

    i got upto “compiling the native code”. i stucked at “loading and testing”.. i am not getting as mentioned in the logcat and my emulator is showing “unfortunately HelloNDK has stopped”..
    can u please solve this issue?

    Reply 
  9. Manish KadabaMarch 13, 2013 at 19:08

    I am getting an error like this..please help me out here

    E:\Android\HelloNDk>javah.exe -classpath bin/classes -d jni org.pielot.hellondk.
    HelloNDK
    Error: cannot access android.app.Activity
    class file for android.app.Activity not found

    Reply 
    1. Leonardo CarnassaleJuly 25, 2013 at 11:44

      try
      javah -classpath ./bin/classes;%ANDROID_SDK%/platforms/android-17/android.jar -d jni org.pielot.hellondk.HelloNDK

      Replace android-17 with your favorite platform

      Reply 
  10. TutorialManApril 9, 2013 at 18:19

    Worked for me, thanks. Next step is ffmpeg on android….

    Reply 
  11. Pingback: easiest way to know Android NDK install is good - How-To Video

  12. JarmezJune 13, 2013 at 23:48

    Hi,

    Just am following this. I already have Cygwin installed and all up-to date and the installer does no help to install particular single packages to an already existing install. It only allows for full installation + the extra packages.

    So I found this whilst Googling. To install individual packages to your existing Cygwin installation you go to command prompt in windows. CD to your cygwin installation root directory (where the installer setup exe is located) and run this:

    setup.exe -n -q -s -P

    E.g
    setup.exe -n -q -s http://mirror.internode.on.net/pub/cygwin/ -P bash,curl,git,make

    This will update your existing cygwin with the latest bash,curl,git and make packages.

    Reply 

 转自: http://pielot.org/2010/12/09/using-cygwin-with-the-android-ndk-on-windows/

你可能感兴趣的:(android)