在 Eclipse 中安装和使用 Android NDK

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3469448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:18:50  来源:igfitidea点击:

Installing & using the Android NDK in Eclipse

androidandroid-ndkinstallation

提问by ingh.am

I've been running the Android SDK for a while now in Eclipse (MAC OSX). I've downloaded the NDK and installed the C/C++ tools in Eclipse, but could anyone guide me on using the NDK? For example, do I just create an Android project like normal and build it with the NDK instead?

我已经在 Eclipse (MAC OSX) 中运行了一段时间的 Android SDK。我已经下载了 NDK 并在 Eclipse 中安装了 C/C++ 工具,但是谁能指导我使用 NDK?例如,我是否只是像平常一样创建一个 Android 项目,然后使用 NDK 来构建它?

Really could do with a decent tutorial if anyone know of any.

如果有人知道的话,真的可以用一个体面的教程来做。

EDIT: OK so I have the NDK installed now (I think) but does anyone have any idea how to use it? I got as far as this (taken from here):

编辑:好的,所以我现在安装了 NDK(我认为)但是有人知道如何使用它吗?我做到了这一点(取自此处):

Run Terminal

cd ~/android-ndk-1.5_r1

make APP=hello-jni

运行终端

cd ~/android-ndk-1.5_r1

让 APP=hello-jni

In order to run the hello-jni sample application, but I get an error in terminal saying:

为了运行 hello-jni 示例应用程序,但我在终端中收到一条错误消息:

Android NDK: APP variable defined to unknown applications: hellojni
Android NDK: You might want to use one of the following:
build/core/main.mk:81: *** Android NDK: Aborting . Stop.

Android NDK:定义为未知应用程序的 APP 变量:hellojni
Android NDK:您可能想要使用以下之一:
build/core/main.mk:81: *** Android NDK: Aborting 。停止。

Any ideas why?

任何想法为什么?

回答by Rob S.

As simply as I can describe it, building an Android app from within Eclipse that uses the NDK requires two steps.

正如我所描述的那样,在 Eclipse 中构建一个使用 NDK 的 Android 应用程序需要两个步骤。

First, inside your terminal you need to run the NDK build script on your project. cd into the root of your project directory and then execute the ndk-build script within that directory.

首先,在终端中,您需要在项目上运行 NDK 构建脚本。cd 进入项目目录的根目录,然后在该目录中执行 ndk-build 脚本。

For example:

例如:

cd ~/workspace/hello-jni
./~/android-ndk-1.5_r1/ndk-build

After doing this, you should see some output that results in the creation of a *.SO file within the obj directory within your project directory.

执行此操作后,您应该会看到一些导致在项目目录的 obj 目录中创建 *.SO 文件的输出。

Once you have the *.SO file, the final step to building an application with the Android NDK through Eclipse is to build it with Eclipse like you would any other application and then deploy it for testing.

获得 *.SO 文件后,通过 Eclipse 使用 Android NDK 构建应用程序的最后一步是像构建任何其他应用程序一样使用 Eclipse 构建它,然后部署它以进行测试。

If you make any changes to the C/C++ code you'll need to repeat step one and regenerate your *.SO file before building and deploying your application from within Eclipse again.

如果您对 C/C++ 代码进行了任何更改,则在再次从 Eclipse 中构建和部署应用程序之前,您需要重复第一步并重新生成 *.SO 文件。

I would like to note that by using the Android NDK your android apps are still based upon Java. They're just communicating with code written in C/C++ by way of the Java Native Interface.

我想指出的是,通过使用 Android NDK,您的 Android 应用程序仍然基于 Java。它们只是通过Java Native Interface与用 C/C++ 编写的代码进行通信。

Finally, I am not aware of any Eclipse plugins that will aid with NDK development. Everything I know about the NDK I have learned the official Android NDK documentation. Please feel free to comment and let me know if there anything I can clear up in my response.

最后,我不知道有任何有助于 NDK 开发的 Eclipse 插件。我所知道的关于 NDK 的一切我都学习了官方的Android NDK 文档。请随时发表评论,如果有什么我可以在我的回复中澄清的,请告诉我。

回答by Hamid Fadishei

Native development and debugging support came into Eclipse environment as of ADT version 20. http://tools.android.com/recent/usingthendkplugin

自 ADT 版本 20 起,本机开发和调试支持进入 Eclipse 环境。http: //tools.android.com/recent/usingthendkplugin

  • Set path to NDK from Eclipse Preferences -> Android -> NDK
  • Right-click on your project and choose Android Tools -> Add Native Support
  • 从 Eclipse Preferences -> Android -> NDK 设置 NDK 的路径
  • 右键单击您的项目并选择 Android Tools -> Add Native Support

developer.android.com states you also need Cygwin.

developer.android.com 声明您还需要 Cygwin。

http://developer.android.com/tools/sdk/ndk/index.html#Contents

http://developer.android.com/tools/sdk/ndk/index.html#Contents

Required development tools

所需的开发工具

  • For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
  • A recent version of awk (either GNU Awk or Nawk) is also required.
  • For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.
  • 对于所有开发平台,都需要 GNU Make 3.81 或更高版本。早期版本的 GNU Make 可能有效,但尚未经过测试。
  • 还需要最新版本的 awk(GNU Awk 或 Nawk)。
  • 对于 Windows,需要 Cygwin 1.7 或更高版本。NDK 不适用于 Cygwin 1.5 安装。

回答by slogan621

The docs directory in the NDK has some pretty good information on how to use the NDK itself. Read the overview, Application.mk, and Android.mk HTML docs. You'll want to google for the Sun JNI PDF, download it, and learn what JNI is all about before you go any further. This is because simply compiling a bunch of C/C++ code into libraries with the NDK is only part of the process. You have to write native Java code that calls your C/C++, and you have to create wrapper functions in C/C++ that adhere to JNI conventions that the native Java code can invoke. JNI has been around a long time, it's not Android specific by any means. So, you can, to learn about it, go quite far following tutorials geared towards JNI, using command line tools like javah and javac, and then return to integrating with the NDK after you know the basics. (For an example of what these C shims look like, take a look at the hello-jni sample in the NDK; the C source file there shows you typically what the shims look like. Using javah to generate these shims is the way to go, you create Java classes that have native methods, process them with javah, and it generates the C headers for you, then you code up C functions that adhere to the generated function prototypes).

NDK 中的 docs 目录有一些关于如何使用 NDK 本身的很好的信息。阅读概述、Application.mk 和 Android.mk HTML 文档。在继续之前,您需要在 google 上搜索 Sun JNI PDF,下载它并了解 JNI 的全部内容。这是因为简单地使用 NDK 将一堆 C/C++ 代码编译成库只是过程的一部分。您必须编写调用 C/C++ 的本机 Java 代码,并且必须在 C/C++ 中创建符合本机 Java 代码可以调用的 JNI 约定的包装函数。JNI 已经存在很长时间了,无论如何它都不是 Android 特定的。因此,要了解它,您可以遵循面向 JNI 的教程,使用 javah 和 javac 等命令行工具走得更远,然后在了解基础知识后返回与 NDK 集成。

Note: while the NDK docs would have you manually building from command line and then going into Eclipse to build your app (a laborious sequence of steps, to be sure, especially if you are changing the C/C++ code), it turns out you can integrate easily with Eclipse so that the NDK is run each time you build from Eclipse. To see how, read here.

注意:虽然 NDK 文档会让你从命令行手动构建,然后进入 Eclipse 来构建你的应用程序(这是一个费力的步骤序列,可以肯定的是,特别是如果你正在更改 C/C++ 代码),事实证明你可以轻松地与 Eclipse 集成,以便每次从 Eclipse 构建时都运行 NDK。要了解如何操作,请阅读此处

回答by Viren

This is for benefit of others who want to create the project from scratch from within eclipse: I followed steps mentioned here in this blog here and it works fine: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/

这是为了其他想要从 eclipse 中从头开始创建项目的人的利益:我按照本博客中提到的步骤进行操作,效果很好:http: //mhandroid.wordpress.com/2011/01/23/using- eclipse-for-android-cc-development/

回答by WorkingMatt

To attempt to answer the question directly - you need to run ndk-build in the folder with the native code within your project folder. This creates the .so files found in the file explorer/resources tree under jni in Eclipse. These functions, if the syntax in the code is correct, can now be called from your java code.

要尝试直接回答问题 - 您需要在项目文件夹中使用本机代码的文件夹中运行 ndk-build。这将创建在 Eclipse 中 jni 下的文件资源管理器/资源树中找到的 .so 文件。如果代码中的语法正确,这些函数现在可以从您的 java 代码中调用。

I found many sources of help when install and getting to grips with Android Developer Tools and the NDK. I wrote a blog postto share my experiences and hopefully give back to the community that helped me get there which may help understand my answer: http://workingmatt.blogspot.co.uk/2013/03/set-up-android-sdk-and-ndk.html

在安装和掌握 Android Developer Tools 和 NDK 时,我找到了许多帮助来源。我写了一篇博文来分享我的经验,并希望回馈帮助我到达那里的社区,这可能有助于理解我的答案:http: //workingmatt.blogspot.co.uk/2013/03/set-up-android- sdk-and-ndk.html