Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't an Android app be written in C/C++ because you "simply prefer to program in C/C++"? [closed]

Tags:

java

c++

android

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.

like image 794
Logan Besecker Avatar asked Dec 08 '12 01:12

Logan Besecker


People also ask

Can Android apps be written in C?

You can add C and C++ code to your Android project by placing the code into a cpp directory in your project module. When you build your project, this code is compiled into a native library that Gradle can package with your app.

Can we make mobile apps using C?

You can build apps for Android, iOS, and Windows devices by using Visual Studio. As you design your app, use tools in Visual Studio to easily add connected services such as Microsoft 365, Azure App Service, and Application Insights. Build your apps by using C# and the . NET Framework, HTML and JavaScript, or C++.

What is the best app for C programming in Android?

SoloLearn. SoloLearn is one of the most popular apps to learn C++, Java, Python, SQL, CSS, HTML, C# etc. and has a largest collection of FREE code learning content.


2 Answers

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.

like image 178
devunwired Avatar answered Sep 17 '22 06:09

devunwired


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.

like image 30
Isaac Avatar answered Sep 18 '22 06:09

Isaac