`
jacky-zhang
  • 浏览: 315933 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Using cygwin with the Android NDK on Windows

 
阅读更多
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. */
	@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();
}

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 __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” including
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:

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.
分享到:
评论

相关推荐

    windows下eclipse集成cygwin编译android_NDK

    ### Windows 下 Eclipse 集成 Cygwin 编译 Android NDK 在 Windows 环境下使用 Eclipse 开发 Android 应用时,如果需要利用 C 或 C++ 的功能,通常会涉及到 Android NDK 的使用。为了更好地整合开发流程,可以将 ...

    android NDK 安装必备 Cygwin

    Cygwin通过模拟Linux API来实现这一功能,使得许多依赖于Unix/Linux环境的工具能够在Windows上运行,包括Android NDK的构建工具链。 在安装Cygwin的过程中,我们需要确保选择正确的包,以支持NDK的构建需求。这些包...

    cygwin Android NDK 交叉编译工具

    3. **Cygwin在Android NDK中的作用**:通过Cygwin,开发者可以在Windows环境下配置和使用NDK,实现对Android的交叉编译,包括设置编译器、链接器和其他构建工具。 **三、Android NDK的配置与使用** 1. **下载和...

    Android NDK环境配置

    Android NDK环境配置是Android应用开发中的一个重要环节,它允许开发者使用C或C++编写高性能的原生代码,这些代码可以被编译成动态库并与Java应用一同打包成APK。NDK集成了交叉编译器,使得开发者能够针对不同的CPU...

    android-ndk-r20b-windows-x86_64.zip

    本文将深入探讨Android NDK R20B版本,特别是其在64位Windows环境下的应用,以及如何与cygwin、FFmpeg等工具协同工作,实现视频编辑功能的动态库编译。 Android NDK是Google提供的一个开源工具集,它允许开发者在不...

    windows+eclipse+cygwin+cdt+ndk配置

    在Windows环境下进行Android原生代码开发,通常会涉及到Eclipse、Cygwin、CDT和NDK这四个组件。下面将详细介绍它们的功能以及如何配置。 首先,Eclipse是一款广泛使用的集成开发环境(IDE),尤其在Java开发领域。...

    非常强大的eclipse中android ndk开发环境的配置说明

    ### 非常强大的Eclipse中Android NDK开发环境的配置说明 #### 一、概述 本文档将详细介绍如何在Eclipse中配置Android NDK开发环境,并实现C/C++代码的自动编译以及通过Eclipse使用Ant生成JNI所需的头文件。配置流程...

    android ndk 开发环境配置,不需要cygwin,可直接生成.h文件,直接编译库

    本文将详细介绍如何在Windows 64位操作系统环境下配置Android NDK开发环境,并实现无需借助Cygwin即可快速生成`.h`文件及直接编译库的过程。 #### 二、所需工具与环境 在开始之前,请确保您的开发环境中已安装以下...

    android NDk cygwin make安装包

    android NDk cygwin make安装包

    Android NDK Cygwin配置

    通过上述步骤,您不仅了解了Android NDK的基本概念及其重要性,还掌握了如何在Windows平台上搭建和配置基于Cygwin的NDK开发环境。这为后续利用C/C++进行Android原生应用开发奠定了坚实的基础。随着实践的深入,您将...

    windows下androidNDK环境配置

    Windows下Android NDK环境配置 一、什么是NDK? NDK(Native Development Kit)是一款提供了一系列工具帮助开发者快速开发C(或C++)动态库,并能自动将.so和Java应用一起打包成apk的开发工具。这些工具对开发者的...

    ECLIPSE下的androidndk开发

    - **Cygwin**:一个在Windows环境下提供Linux命令行环境的软件,用于执行NDK所需的命令行工具。 - **Eclipse**:选择安装了ADT(Android Developer Tools)的版本,这是Eclipse专门为Android开发设计的插件。 - **...

    Android NDK 开发教程二

    - **操作系统**:官方推荐使用 Ubuntu 系统进行开发,但在 Windows 上也可以通过 Cygwin 实现对 NDK 的支持。 - **版本要求**:NDK 仅适用于 Android 1.5 及以上版本。 ##### 3.2 使用示例 开发者可以通过以下步骤...

    cygwin安装程序+ndk环境搭建以及opengl在ndk环境中的使用说明

    总结,通过Cygwin和NDK,开发者可以在Windows环境下构建Android原生代码,利用OpenGL ES实现高效的图形处理。理解并熟练掌握这一流程,对于Android游戏开发或其他需要高性能计算的场景非常有益。在实际操作中,可能...

Global site tag (gtag.js) - Google Analytics