java - 在 Android 中使用 Native 的好处

标签 java android c++ android-ndk

<分区>

所以我开始了一个为期一个学期的项目,我的团队决定使用 Android 作为目标平台。这特别激起了我对一个问题的好奇心。

我认为在大多数开发案例中,Java 和虚拟机是最好的开发范例。我的问题是:在什么情况下可以使用 native 软件开发工具包 (C/C++) 而不是 Java?

根据 Android 文档,有几点需要牢记:

  • “您应该了解 NDK 不会使大多数应用受益。作为开发人员,您需要权衡其优势与劣势。值得注意的是,在 Android 上使用 native 代码通常不会带来明显的性能提升,但它总是会增加您的应用程序的复杂性。”

  • “一般来说,只有当 NDK 对您的应用必不可少时,您才应该使用它——绝不是因为您只是喜欢用 C/C++ 编程。”

  • “NDK 的典型候选者是独立的、CPU 密集型操作,不会分配太多内存,例如信号处理、物理模拟等。”

这是一组非常广泛的陈述(尤其是最后一个)。据我所知,如今使用大多数 JVM,通过 JIT 编译执行字节码的速度几乎与直接编译为机器代码一样快。因此,我正在寻找能够在更深层次上阐明的多个回复,更具体地说,我们何时决定使用原生语言进行 Android 开发,这些情况的一些很好的例子是什么?

最佳答案

来自英特尔开发人员专区,NDK Android* 应用程序移植方法Performance Implications and the Cost of JNI

Specifically, using native code in an Android* application doesn't guarantee a performance boost! Commonly, performance gains can be appreciated in cases such as when the native code involves CPU-centric operations (such as heavy – duty usage of SSE instructions), but at other times, when for example, the application at hand is simply providing a complex web interface to the user, the use of native code through JNI may hinder performance. There is no "written rule" as to when the NDK should and shouldn't be used...

SSE是原始 MMX 指令集的扩展,为 3D 渲染、游戏、图像处理、语音和手写识别、科学计算等精确的复杂算术过程引入,因此 NDK 在这种情况下应该很有用。

NDK支持以下指令集

  • ARMv5TE,包括 Thumb-1 指令
  • ARMv7-A,包括 Thumb-2 和 VFPv3-D16 指令,可选择支持 NEON/VFPv3-D32 指令
  • x86 指令
  • MIPS 指令

NEON技术支持很有趣,并得出相同的结论:

can accelerate multimedia and signal processing algorithms such as video encode/decode, 2D/3D graphics, gaming, audio and speech processing, image processing, telephony, and sound synthesis by at least 3x the performance of ARMv5 and at least 2x the performance of ARMv6 SIMD.

关于java - 在 Android 中使用 Native 的好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21166130/

相关文章:

java - 获取二进制文件和文本信息

java - 我怎样才能捕捉到 NoSuchMethodException?

android - 无效的空头购买代币

c++ - 尝试从 OpenGL 创建 OpenCL 上下文时出现无效共享组错误

c++ - Rust,如何使用 DLL 中的全局变量? C++ 等效项需要 __declspec(dllimport)

c# - 从 C# 引用 64 位 dll 时应用程序退出(无异常)

java - 导出为可运行Jar文件,java执行错误

java - 从 appengine 验证 UserService

android - 从小部件创建时如何避免一个 Activity 的多个实例?

android - Google Android USB 驱动程序和 ADB