为什么 Android 应用程序不能用 C/C + + 编写,因为你“只是更喜欢用 C/C + + 编程”?

更新 (为了清晰和减少歧义) :

我要开始修改安卓应用程序了。我打算用 NDK 编写 C + + 语言的代码(因为我在 C + + 方面有更多的经验,而且比 Java 更喜欢它) ,但是在 Android NDK 页面中遇到了以下问题:

你应该只使用 NDK,如果它是必不可少的 App ー < strong > 永远不会,因为你只是喜欢用 C/C + + 编程。

我的印象是,你应该使用你喜欢的语言,只要它适合这份工作。有没有人能解释一下,为什么如此强烈地建议不要在 Android 开发中使用 C/C + + ?


原文:

我将开始修改移动应用程序,特别是我现在手机的操作系统 android,我想知道用 C + + (或者至少是核心,然后用 Java 包装)编写应用程序是否是一个可接受的选择。

一些背景知识,我是一个计算机科学专业的学生,已经学习了3门 C + + 课程(介绍,中级,面向对象编程,春季学习 STL 课程) ,只有1门 Java 课程(中级)。正因为如此,我更喜欢使用 C + + ,而不是 Java。我在 Android NDK 页面上看到了以下内容:

在 Android 上使用本机代码通常不会导致明显的 性能提高,但它总是增加应用程序的复杂性。 一般来说,你应该只使用 NDK,如果它是必不可少的 App ー < strong > 永远不会,因为你只是喜欢用 C/C + + 编程。

  • 我觉得你应该用合适的语言 和你熟悉的工作一样
  • 我可能想把应用程序移植到另一个移动平台上,比如 作为 iOS,它支持 C + + ,但不支持 java
  • 而 Java 是一种高级语言,因此应该进行开发 更快,我觉得发展会更慢,因为我会 重新学习几乎所有的东西(因为我只上过一节课 语言)

如果你能给我点建议,我会很感激的。

注: 很多关于这个问题的回答都是多年前的,很少有后续的回答提到 NDK 允许在 android 2.3和更新版本上开发完整的本地应用程序。

108326 次浏览

If you're only going to develop one app in your life, use the NDK.

If you're aiming at learning Android development with the intention of developing more than one application during your lifetime - and want to be able to properly support them all - you're very likely to do better in the long run if you learn Java and use Android's Java SDK instead.

The most important consideration is that the compiled Java code will run on all android devices unchanged, whereas the native code will need to be compiled for all target platforms.

The general intent for both Java and Android is that you write the majority if not all your app in Java and use native things only when there is no other option... so everything about writing the app will lend itself to doing so in Java.

You'll spare yourself a lot of aggravation in bridging between the native and Java worlds by writing in Java.

As well, you will do yourself a big favor if you take the plunge and learn Java. Not only will your Android app be the better for it, but you will expose yourself to a significantly different approach to OO and you will be a better programmer for it.

Add to that the fact that you will side-step a whole bunch of security risks by writing in Java.

In my mind, this is a no-brainer - use Java.

Think of it this way. You have the ability using the Java SDK to build a full working application that takes advantage of 100% of the APIs available to developers. There is nothing you can do with the NDK that cannot be done with the SDK (from an API perspective), the NDK just provides higher performance.

Now look at it in reverse. If you choose to write an application 100% in the NDK, you can still write a fully functional application, but you are limited in the number of framework APIs you can access. Not all of the Android framework can be accessed at the native layer; most APIs are Java only. That's not to say that all the APIs YOU may need aren't available in the NDK, but nowhere near ALL the APIs are exposed.

Beyond this, the NDK introduces platform-specific code which expands the size of your distribution. For every device architecture you intend to support, your native code must be built into .so files (one for armv5, armv7 and x86) all packaged up into the same APK. This duplication of executable code makes your app 3x the size (i.e. a "fat binary") unless you take on the task of building separate APKs for each architecture when you distribute the application. So the deployment process becomes a bit more work if you don't want your APK to grow in size significantly.

Again, while none of this is prohibits you from doing what you choose, it points out why Google describes Java as the "preferred" method for the majority of your code and the path of least resistance. I hope it sheds some light on why the documentation is worded the way it is.

I would say use java for main app. But if you have some c++ code you need to port or some library you need that is efficiently implemented in c++, then use ndk for those bits

I don't see any reason to not use C++ for normal android development , If you have extensive experience in working in C++ and with complex OS like windows or any other such, then you can grasp android quickly and is not as much complicated as the other OS are. while learning java or working without learning it would be more frustrating and complex !

The programmers at King use C++ for their game logic. And they seem to be doing fine judging by their turnover.

In my experience, C++ is for problem solvers and Java is for problem avoiders. I love either language, but C++ is quite rewarding when you write good code. However, it may just take several moments of wizardry to get there.

You could recommend C++ for Data scientists as well, who would normally get their job done by, say, Python or R. C++ can do the same with as good or not better performance, but it just takes being a genius in the language. That is why I'd never not recommend C++ to the one that wants to do it - I'd just give a heads up to the treat that they're in for.

I found this interesting article from: http://betanews.com/2014/07/22/why-c-is-the-perfect-choice-for-modern-app-development/

C++ was built specifically for platform independence and as such is found on every single operating system in existence. Your typical mobile user may know that Android apps are written Java and iOS apps in Objective-C, but what many don’t know is that there is more C/C++ code in memory on your devices than anything else. C/C++ drives much of the technology of small devices (like the kernel, which interacts with the hardware, as well as typical run time libraries) and the telecommunications networks that enable these devices. More importantly for a development team, is that there are C/C++ interfaces and libraries for anything you need to do on any device and platform. The Android NDK toolset is a great example of full C/C++ support that was added originally for game development teams to enable them to get the best possible performance out of the device by avoiding Java and the Android Java runtime Dalvik, the virtual machine on which Android Java code is executed on. It has been regularly improved to enable every Android service.