Android NDK 的限制?
我有一个关于 Android 平台上本机代码的限制的问题。
基本上我已经用本机 C 代码开发了一个库,它使用 UDP 套接字进行 SIP/RTP 并使用 OpenAL 进行音频录制/播放 - 基本上是整个应用程序。 这个想法是尽可能多地使用本机 C 代码而不是 Java 代码。我想这样做是因为我也将在其他平台上使用它。
我的问题很简单 - 是否可以只使用 Java 作为 GUI,然后用本机代码进行所有处理? 当我的本机代码尝试创建套接字、绑定它、录制音频、播放等时会发生什么 - 因为它是本机代码,我是否需要为其设置权限(例如应用程序访问麦克风等)或将它只是绕过了这个东西,因为它的本机代码? 本机代码可以在 Android 上像在 PC 上一样做几乎任何事情吗?
如果不清楚,抱歉;只是告诉我,我会尽力改进,
谢谢
I have a question about the limitations of what you can do in native code on the Android platform.
Basically I have developed a library in native C code that uses UDP sockets for SIP/RTP and uses OpenAL for audio recording/playback - basically the whole application.
The idea is to have as much as possible in native C code rather than Java code. I want to do this because I am going to use it on other platforms as well.
My question then is simply - is it possible to just use the Java for the GUI and then all processing in native code?
What will happen when my native code tries to create a socket, bind it, record audio, play it, etc - since it is in native code, do I need to setup permissions for it (such as application accessing microphone and whatnot) or will it just bypass this stuff since its native code?
Can native code do pretty much anything it wants on Android like on PCs?
Sorry if its unclear; just tell and I'll try to improve it
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在本机代码中执行几乎所有您想要的操作,但唯一真正支持的操作系统级功能是 OpenGL、OpenSL 和一些数字处理库(压缩、数学等)。
但是,您可以随时使用 JNI 来调用 Java 方法,因此您可以使用标准 Android API 进行网络(诸如
Socket
等类)。显然,由于调用是通过 Java API 进行的,因此所有正常的 Android 权限都适用(例如android.permission.INTERNET
)。编辑:正如评论中所述,属于 NDK 一部分的标准库确实支持套接字。
You can do pretty much anything you want in native code, but the only OS-level thing really supported is OpenGL, OpenSL, and some number-crunching libraries (compression, math, etc).
However, at any time you're free to use the JNI to call a Java method, so you could use the standard Android API for networking (classes like
Socket
, etc). Obviously, since the call is going through the Java API, all the normal Android permissions apply (likeandroid.permission.INTERNET
).EDIT: As elaborated in the comments, the standard libraries that are part of the NDK do have support for sockets.
您仍然需要您的应用程序具有权限。例如,如果清单中没有
android.permission.INTERNET
,您的本机套接字将无法工作。另一种选择是在 Java 层创建套接字并将其向下传递。下面是与本地套接字交互的示例,请参见方法
org_..._OpenSSLSocketImpl_connect()
:http://www.netmite.com/android/mydroid/dalvik/libcore/x-net/src/main/native/org_apache_harmony_xnet_provider_jsse_OpenSSLSocketImpl.cpp
You still need your application to have permissions. For example, your native sockets will not work without
android.permission.INTERNET
in the manifest.Another option is to create the socket at the Java layer and pass it down. Here's an example of interacting with the socket in native land, see the method
org_..._OpenSSLSocketImpl_connect()
:http://www.netmite.com/android/mydroid/dalvik/libcore/x-net/src/main/native/org_apache_harmony_xnet_provider_jsse_OpenSSLSocketImpl.cpp
原生 Android API 是一篇关于 NDK 的好文章。
是的。并且您需要为 AndroidManifest 设置适当的权限。
您需要使用 OpenSL ES API 在本机端录制和播放音频。这意味着您的应用程序应该适用于 Android 2.3 或更高版本。
或者,NVIDIA 提供了一个框架,使我们能够使用 C++ 开发 Android 事件、传感器、音频等,即使对于 Android 2.2 或更早版本也是如此。
Tegra 资源 - Android SDK 和 Tegra 资源NDK 示例应用程序和文档
The Native Android API is a nice article for NDK.
Yes. And you need to set appropriate permissions to your AndroidManifest.
You need to use OpenSL ES API for recording and playing audio in the native side. It means your application should be for Android 2.3 or later.
Or, NVIDIA provides a framework that allow we to be able to develop using C++ for Android events, sensors, audio and so on even though for Android 2.2 or earlier.
Tegra Resources - Android SDK & NDK sample applications and documentation
您可能想查看 csipsimple,这是使用 pjsip java android 应用程序中的 sip 库(用 C 编写)。
我还没有研究它如何进行套接字通信,但它应该为您提供一个更完整的示例来说明您正在尝试执行的操作。
You may like to check out csipsimple which is a example of using the pjsip sip library (which is written in C) in a java android application.
I haven't looked into how it does the sockets communication but it should give you a more complete example of what you are trying to do.