安装&在 Eclipse 中使用 Android NDK
我已经在 Eclipse (MAC OSX) 中运行 Android SDK 一段时间了。我已经下载了 NDK 并在 Eclipse 中安装了 C/C++ 工具,但是有人可以指导我使用 NDK 吗?例如,我是否只像平常一样创建一个 Android 项目并使用 NDK 构建它?
如果有人知道的话,真的可以做一个像样的教程。
编辑:好的,我现在已经安装了 NDK(我认为),但是有人知道如何使用它吗?我到目前为止(取自这里< /a>):
运行终端
cd ~/android-ndk-1.5_r1
make APP=hello-jni
为了运行 hello-jni 示例应用程序,但我在终端中收到错误消息:
Android NDK:APP 变量定义为 未知应用程序:hellojni
Android NDK:您可能想使用 以下之一:
构建/核心/main.mk:81: *** Android NDK:正在中止。停止。
有什么想法吗?
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?
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):
Run Terminal
cd ~/android-ndk-1.5_r1
make APP=hello-jni
In order to run the hello-jni sample application, but I get an error in terminal saying:
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.
Any ideas why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如我所描述的那样,从 Eclipse 中构建使用 NDK 的 Android 应用程序需要两个步骤。
首先,在终端内,您需要在项目上运行 NDK 构建脚本。 cd 进入项目目录的根目录,然后在该目录中执行 ndk-build 脚本。
例如:
执行此操作后,您应该会看到一些输出,这些输出导致在项目目录的 obj 目录中创建 *.SO 文件。
获得 *.SO 文件后,通过 Eclipse 使用 Android NDK 构建应用程序的最后一步是使用 Eclipse 构建它,就像构建任何其他应用程序一样,然后部署它进行测试。
如果您对 C/C++ 代码进行任何更改,则需要重复第一步并重新生成 *.SO 文件,然后再次从 Eclipse 中构建和部署应用程序。
我想指出的是,通过使用 Android NDK,您的 Android 应用程序仍然基于 Java。它们只是通过 Java 本机接口 与用 C/C++ 编写的代码进行通信。
最后,我不知道有任何 Eclipse 插件可以帮助 NDK 开发。我所知道的有关 NDK 的一切都是从官方 Android NDK 文档 中学习的。请随时发表评论,并让我知道是否有任何我可以在我的回复中澄清的内容。
As simply as I can describe it, building an Android app from within Eclipse that uses the NDK requires two steps.
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.
For example:
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.
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.
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.
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.
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.
自 ADT 版本 20 起,Eclipse 环境中引入了本机开发和调试支持。 http://tools.android.com/ centre/usingthendkplugin
developer.android.com 声明您还需要Cygwin。
http://developer.android.com/tools/sdk/ndk/index .html#Contents
所需的开发工具
Native development and debugging support came into Eclipse environment as of ADT version 20. http://tools.android.com/recent/usingthendkplugin
developer.android.com states you also need Cygwin.
http://developer.android.com/tools/sdk/ndk/index.html#Contents
Required development tools
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 特有的。因此,为了了解它,您可以使用 javah 和 javac 等命令行工具,深入学习面向 JNI 的教程,然后在了解基础知识后返回与 NDK 集成。 (有关这些 C 垫片外观的示例,请查看 NDK 中的 hello-jni 示例;那里的 C 源文件通常向您展示这些垫片的外观。使用 javah 生成这些垫片是正确的方法,您创建具有本机方法的 Java 类,使用 javah 处理它们,它会为您生成 C 标头,然后您编写遵循生成的函数原型的 C 函数。
注意:虽然 NDK 文档会让您从命令行手动构建,然后进入 Eclipse 来构建您的应用程序(当然,这是一系列费力的步骤,尤其是在您更改 C/C++ 代码时),但事实证明您可以轻松地与 Eclipse 集成,以便每次从 Eclipse 构建时都会运行 NDK。要了解具体方法,请阅读此处。
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).
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.
这是为了那些想要从 eclipse 中从头开始创建项目的其他人的利益:我按照本博客中提到的步骤进行操作,它工作正常: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/
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/
要尝试直接回答问题 - 您需要在项目文件夹中包含本机代码的文件夹中运行 ndk-build 。这将创建在 Eclipse 中 jni 下的文件资源管理器/资源树中找到的 .so 文件。如果代码中的语法正确,现在可以从 Java 代码中调用这些函数。
在安装和掌握 Android 开发工具和 NDK 时,我发现了许多帮助来源。我写了一篇博客文章分享我的经验并希望回馈帮助我到达那里的社区,这可能有助于理解我的答案:http://workingmatt.blogspot.co.uk/2013/03/set-up-android-sdk-and-ndk.html
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.
I found many sources of help when install and getting to grips with Android Developer Tools and the NDK. I wrote a blog post to 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