94

Updated(for clarity and to reduce ambiguity):

I'm going to start tinkering around with android apps. I was planning on writing the in C++ using the NDK (since I have more experience in C++ and prefer it to Java) but came across the following on the Android NDK page:

you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++.

I was under the impression that you should use the language that you prefer, as long as it fits the job. Could somebody explain why it is so heavily advised not to use C/C++ for android development?


Original:

I'm going to start tinkering around with mobile apps, specifically android which is the OS of my current phone, and I was wondering if writing the app in C++ (or at least the core, then wrapping in Java) was an acceptable option.

Some background, I'm a computer science major who has taken 3 C++ courses(intro, intermediate, OOP, and am taking an STL course in the spring) and only 1 Java course(intermediate). Because of this, I am more comfortable with C++ and prefer it to Java. I came across the following on the Android NDK page:

Using native code on Android generally does not result in a noticeable performance improvement, but it always increases your app complexity. In general, you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++.

  • I was under the impression that you should use the language the fits the job as well as one you're familiar with
  • I may want to port the application to another mobile platform, such as iOS, that supports C++ but not java
  • While Java is a high level language and thus should make development faster, I feel like development would be slower because I would have to relearn almost everything (since I have only taken one class on the language)

Any advice would be much appreciate.

ps: many of the answers on this subject are from years ago and there are very few follow up answers that mention the NDK allowing the development of full native apps on android 2.3 and newer.

Logan Besecker
  • 2,733
  • 4
  • 23
  • 21
  • 1
    "Acceptable" is an odd word – it's certainly _possible_, via [the NDK](http://developer.android.com/tools/sdk/ndk/index.html)... – ildjarn Dec 08 '12 at 02:00
  • 1
    You're probably better off biting the bullet and learning Java. The Android toolkit is really intended to be used from Java, and the time you would save from already knowing C++ would be spent and then some trying to figure out how to make all the Android stuff work from C++. Think of this as a learning opportunity :) – Jeremy Friesner Dec 08 '12 at 02:01
  • Personally I'd bite the bullet as Jeremy said and simply learn what you have to with Java. It actually shouldn't be *that* big a deal for you to learn it relatively fast. – OmniOwl Dec 08 '12 at 02:03
  • For your first point, C++ doesn't fit the job (developing for Android) nearly as well as Java does, regardless of familiarity. For your second point, despite Android being Linux, there's a huge difference between Android and desktop apps; lifecycles are different and you have to deal with activities instead of processes, which is a *completely different* way of developing compared to normal desktop development. For your third point, learning the Android SDK in C++ will take you significantly longer than it will to learn it in Java first (after learning Java), and then switch to C++. – Cornstalks Dec 08 '12 at 02:10
  • @ildjarn updated original post to focus on why it is advised not to use C or C++ for the sole reason that you prefer it – Logan Besecker Dec 08 '12 at 02:21
  • 2
    Too many people use the term "C/C++" as if there were a single language of that name. I hope you're aware that C and C++ are two different (though closely related) languages -- and it seems to be C++, not C, that you're asking about. – Keith Thompson Dec 08 '12 at 02:48
  • @KeithThompson Yes I am aware that C and C++ are two separate languages; I used the term because the NDK supports both C and C++. I too notice people using the term 'C/C++' as if it were a single language and find it odd. – Logan Besecker Dec 08 '12 at 02:53
  • why was this closed? Devunwired's answer was filled with many facts that provided much insight into the reasoning. The original vote to close was because of the original post(which I have left underneath my updated post) – Logan Besecker Dec 08 '12 at 03:18

7 Answers7

118

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.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • 1
    Thank you very much! The change in perspective was the big part I was missing. I wasn't aware that the NDK lacked some API. I also wasn't thinking about the size of the distribution; but it makes a lot of sense now that you have brought it to mind(specifically because c++ must include all the standard libraries whereas java's standard libraries are on on the devices already). Could you explain a little bit about the `.so` files? I'm aware that there needs to be a separate compilation of native code for each hardware architecture supported, so each `.so` files has a seperate compilation? – Logan Besecker Dec 08 '12 at 03:14
  • 52
    The restriction of the NDK is actually a pity. I would love to write high-performance code for mobile platforms, mainly because of battery life considerations. Native *matters* on mobile platforms. Why not support it better? #ᴅɪꜱʟɪᴋᴇ – Konrad Rudolph Dec 08 '12 at 12:32
  • 3
    @LoganBesecker The NDK toolchain will cross-compile your code into a `.so` for each architecture you've chosen to support (this is controlled by your makefiles), and all the files will be placed into the libs/ directory of your APK. When the APK is installed, only the appropriate `.so` will be selected, but they all still have to live in the initial APK. – devunwired Dec 08 '12 at 18:10
  • 1
    @KonradRudolph you can write parts of your apps with the ndk, Google does this for performance critical parts of Android. Except for very specific cases, c++ vs java is irrelevant in Android development, especially now that ART is coming. The right use of the APIs, especially those that consume battery (GPS, network) is where you need to be careful. – Teovald Jul 12 '14 at 12:13
  • @Teovald I don’t buy this, mainly [because of memory constraints](http://sealedabstract.com/rants/why-mobile-web-apps-are-slow/). – Konrad Rudolph Jul 12 '14 at 13:20
  • Is java really slower than c++? also, why did google choose to java as the main language of android? – FluorescentGreen5 Sep 11 '16 at 08:44
  • 1
    Java is slower. The JVM needs to load, plus byte-code is VERY efficient, but it is not "native", and hence has a small penalty -- depending on the application this may be noticeable. As for why did they choose Java, specifically because it runs in an interpreter: the garbage collector reduces memory problems and reduces cross-app impacts. Additionally, the Java language (while verbose) removes many of the developer introduced errors of C++. – Daniel Apr 08 '17 at 17:57
26

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.

Isaac
  • 16,458
  • 5
  • 57
  • 81
8

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.

JAkerblom
  • 162
  • 1
  • 2
  • 6
    Any time you spend solving problems that are unique to C++ is time you could have spent building the actual functionality of your app. – user45623 Oct 10 '19 at 00:22
4

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.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
  • 2
    "...and use native things only when there is no other option". When can there be no other option? Everything on Android can be achieved via Java! – CinCout Aug 27 '16 at 17:09
  • Nim compiles to both Objective C and C++, so you can embed its build outputs in both iPhone and Android apps. So far as I know, nothing compiles to both Java and Objective C. So there's one thing that you can't do with Java on Android. – MatrixManAtYrService Aug 09 '23 at 15:00
4

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.

Moussa D.
  • 59
  • 4
3

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

Dan
  • 31
  • 1
3

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 !

stng
  • 124
  • 10