简体   繁体   English

Android和C ++:必要吗?

[英]Android and C++: Necessary?

Just checking out Android development very superficially, and it seems that most everyone is working in Java. 只是非常简单地检查Android开发,似乎大多数人都在使用Java。 Yet 然而

Android includes a set of C/C++ libraries used by various components of the Android system. Android包含一系列由Android系统的各个组件使用的C / C ++库。 These capabilities are exposed to developers through the Android application framework. 这些功能通过Android应用程序框架向开发人员公开。

does this mean that, in Android applications, Java is used and C++ is used: 这是否意味着,在Android应用程序中,使用Java并使用C ++:

  • sometimes? 有时?
  • a lot? 很多?
  • almost never? 几乎从不?
  • Never: you cannot use it for applications? 从不:你不能将它用于应用程序?

Sometimes. 有时。 As least as possible, only for time-sensitive code, and even then it might not be a good solution. 尽可能地,仅对时间敏感的代码,即使这样,它可能不是一个好的解决方案。 The standard way to make Android apps is Java. 制作Android应用程序的标准方法是Java。
This is because the Java code will truly be cross-platform and will work virtually across all devices. 这是因为Java代码将真正实现跨平台,并且几乎可以在所有设备上运行。 While the ndk is only supported for ARM processors as far as I know, and it's not entirely encouraged unless absolutely necessary. 虽然据我所知,ndk只支持ARM处理器,除非绝对必要,否则不会完全鼓励它。

A lot of people abuse the NDK to avoid using Java or the SDK. 很多人滥用NDK来避免使用Java或SDK。 This is wrong. 这是错的。

You can use c++ in your android app via the NDK. 您可以通过NDK在Android应用程序中使用c ++。 It must be used in conjuction with the Java-based SDK, and is designed only to be used for performance-critical pieces of code. 它必须与基于Java的SDK结合使用,并且仅用于性能关键的代码片段。 It has more limited functionality than the SDK (can't display UI, etc.). 它具有比SDK更有限的功能(无法显示UI等)。

Reasins for using, from the docs: 从文档中使用Reasins:

The NDK will not benefit most applications. NDK不会使大多数应用程序受益。 As a developer, you will need to balance its benefits against its drawbacks; 作为开发人员,您需要平衡其优势与缺点; notably, using native code does not result in an automatic performance increase, but does always increase application complexity. 值得注意的是,使用本机代码不会导致自动性能提升,但总会增加应用程序的复杂性。 Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on. NDK的典型优选候选者是独立的,CPU密集型操作,不分配大量内存,例如信号处理,物理模拟等。 Simply re-coding a method to run in C usually does not result in a large performance increase. 简单地重新编码在C中运行的方法通常不会导致大的性能提升。 The NDK can, however, can be an effective way to reuse a large corpus of existing C/C++ code. 但是,NDK可以是重用大量现有C / C ++代码的有效方法。

Thus, the majority of users will not use any C++ code. 因此,大多数用户不会使用任何C ++代码。

您可以分叉Android平台并添加更多用C ++或其他语言编写的本机代码,但在Android平台上运行的代码使用谷歌自己的p代码格式,该格式是通过转换Java类文件创建的。

Underneath the covers most of the OS is C/C++. 在封面下,大部分操作系统都是C / C ++。

http://developer.android.com/guide/basics/what-is-android.html http://developer.android.com/guide/basics/what-is-android.html
Anything in Green/Or red is written in C/C++. 绿色/红色的任何东西都是用C / C ++编写的。
Not sure about the Application Framework. 不确定应用程序框架。

What is exposed to the application developer is done through the Java SDK (the first SDK provided). 通过Java SDK(提供的第一个SDK)完成了向应用程序开发人员公开的内容。 Though now a more limited (from what I have been told (I don;t have direct experience)) C++ SDK is available. 虽然现在更有限(从我被告知(我没有直接经验))C ++ SDK可用。

I suppose you could technically write everything in C++ but I have not seen any of documentation about how to access any of these components so in effect they are hidden behind the SDK and unless somebody reverse engineers how to get at them you will be stuck with what the SDK you use exposes. 我想你可以在技术上用C ++编写所有东西,但我还没有看到任何关于如何访问这些组件的文档,所以实际上它们隐藏在SDK后面,除非有人逆向工程师如何搞定它们你将被困在什么您使用的SDK公开。

In my case, I use NDK to compile a shared, cross-platform C++ app core, and Java for the UI. 就我而言,我使用NDK编译共享的跨平台C ++应用程序核心,并为UI编译Java。 The said core compiles and runs equally well on WinMobile and iPhone. 所述核心在WinMobile和iPhone上编译并运行良好。 The code division is about 60% core, 40% UI. 代码划分大约60%核心,40%UI。

Official Google's position is that developers must use Java as a main development platform, and use C++/NDK for time- and performance-sensitive operations. Google的官方立场是开发人员必须使用Java作为主要开发平台,并使用C ++ / NDK进行时间和性能敏感的操作。 Also, the only supported APIs are Java ones. 此外,唯一支持的API是Java。 Ie Google gives you only Java API and supports only Java API. 即Google仅为您提供Java API并且仅支持Java API。 Whatever you can find on the device in native world (any shared libraries, libc etc), can and may be changed by Google or device vendors at any time without notice and so you are discouraged to use your findings. 无论您在本地设备上找到什么(任何共享库,libc等),Google或设备供应商都可以随时更改,恕不另行通知,因此您不鼓励使用您的发现。

Practically standard libc functions work and probably will work all the time, but you should not rely on any device-specific libraries or lesser-known native libraries. 实际标准的libc函数可以工作,并且可能一直有效,但是您不应该依赖任何特定于设备的库或鲜为人知的本机库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM