PS:I found a very useful article about cygwin with android ndk, so I copy it here to help others may have the same problems, the original address is http://pielot.org/2010/12/09/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 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/. Currently http://cygwin.com/setup.exe as direct link. 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.


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. */@Overridepublic 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();}

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.

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 -d jni org.pielot.hellondk.HelloNDK


When no error occurs, the compilation was successful. 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 __cplusplusextern "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” including
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := hellondkLOCAL_SRC_FILES := org_pielot_hellondk_HelloNDK.cinclude $(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:

ATTENTION, 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

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.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 说说 Android 中如何实现同时兼容手机与
  2. Android之SD卡插拔广播
  3. ViewDragHelper详解
  4. Android实现带动画效果的可点击展开TextV
  5. 【Android(安卓)开发教程】Fragments间的
  6. android:label标签
  7. Android采取ContentObserver方式自动获取
  8. Android(安卓)8.1 MTK6739修改文档
  9. Android 倒计时广告与广告轮播
  10. Android编译系统详解(二)——命令执行流