Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will perform better on Android? An app written with Java or C++

Will apps targeting Android devices written with C++ on Qt perform better than apps written with Java using Android SDK?

like image 428
Dinal24 Avatar asked Jul 13 '14 04:07

Dinal24


People also ask

Is C good for Android development?

Google offers the native development kit (NDK) that uses native languages like C and C++ for Android development. But you can't build an entire Android app with C or C++. You need to learn Java.

Is Android written in C or Java?

The official language for Android development is Java. Large parts of Android are written in Java and its APIs are designed to be called primarily from Java. It is possible to develop C and C++ app using the Android Native Development Kit (NDK), however it isn't something that Google promotes.

Is Java better for Android?

Java is best suited for creating Android apps since it is platform-independent, and as a result, Java apps work on any platform. Java has its own runtime environment, Java Runtime Environment, as well as an API. A major portion of Android apps are based on Java, which is one of the most popular languages on GitHub.

Which language is best for mobile app development?

Java, Python, C++, Kotlin, and Rust are popular app development languages ranked among the world's top 10 most preferred languages in 2022.

Why is Java preferred for developing an Android application?

Java is the first choice of android app developers because of ease of use, robustness, security features, and cross-platform development capabilities.


2 Answers

It seems all other answers are disputes about C++ and Java, so I will try to share my experience and knowledge about Java and Qt programming in Android.

First of all, Android SDK implemented in Java and thus developer should use Java as default.

.

More Performance and more control

But there is need to more performance in some cases. Games is an example. Image processing is another use case example.
In some situations you need to use C++ for better control over HW. (e.g. use Accelerometer with higher frequency)

Android uses an specific virtual machine named Dalvik (until ver.5). Any application which needs UI, needs Dalvik. So C++ applications (games and Qt e.g.) needs to involve Dalvik.

Also there is no C++ library to utilising services and OS features directly from C++ (such as contact book). Therefore you need to use Java alongside C++. For this purpose you need to write some Java codes in .java file and you need to use JNI for communicate C++ and Java. JNI is another problematic thing which you need to learn.
So developing Android application using C++ is very more difficult than regular Java apps.
.

My Qt experience on Android

I have written some Android applications by Qt\QtQuick without QtQuick Controls. QtQuick is AWESOME for UI development but you need to doing some stuff such as managing load and unload UI manually. It is not a problem, since there is some features in QtQuick for this purpose like Loader.
Implementing general section of the application (non-specific to Android section) in Desktop is very productive and fast and interesting (both C++ and QtQuick).
But implementing Android specific section in JNI needs more time and care.

.

Qt on Android - Cons and Pros

  • APK package is large (~8mb most cases overhead)
  • Need to publish multiple APK package (ARM, x86) (Google play lets you to doing it very nice without end user notice)
  • App launch time is higher than normal apps
  • Need to develop using combination of C++ and Java through JNI
  • More difficulties to debugging
  • Fewer documentation
  • Extremely fast UI
  • If you plan to publish your app in other platforms like iOS, you need just to reimplement OS-specific section. Thus your time/cost will goes low.
  • High performance back end processing and overall app performance
  • Better memory management
  • Same quality in Android 2.2 (e.g.) till 7.0! (Some features of Android SDK is just in higher version and thus you can not uses that features if you plan to support lower versions of Android. like AnimationFramework. Using Qt you don't have this problem)
  • Utilising HW accelerated UI in earlier Android versions as well as higher ones
  • Source code protection against un-ubfuscation
  • Good separation about View and Model/Controller (helping to professional coding)
  • Easy and fast UI development
  • Easy and nice UI effects (OpenGLES & GLSL) (also naturally integrated to other components of UI)

.

Suggestion

IF (you are not a Qt experienced developer) => Use Java

IF (you need just some more performance for some processing) => Java + C++(NDK)

IF (you are a Qt experienced developer) AND ((you are beginner in Java) OR (you planned to porting your app into other platforms)) => Use Qt with C++

like image 115
S.M.Mousavi Avatar answered Oct 30 '22 08:10

S.M.Mousavi


Lets break this into two parts:

1)Will C++ perform better than Java?

Yes. Of course it will. It always does, so long as you don't make to many JNI calls back into Java. That's why people (including Google) use C++ where performance is of the essence.

2)Will QT perform better than using JNI to go back to Java or than using the raw calls available to C++ in the NDK?

Assuming you write both kinds of code optimally, the order is probably raw NDK calls > Java calls > QT calls. The QT library is just going to be making the exact same function calls you would, it just puts a different abstraction layer on it. That layer will add overhead. Chances are that the overhead is minimal most of the time. If you're an experienced QT programmer, you will likely get something coded faster using QT. If you're an experienced Android programmer, you'll get it done faster without it. QT will increase the odds of bugs, because any bugs in the QT layer itself will show through in your code.

like image 44
Gabe Sechan Avatar answered Oct 30 '22 08:10

Gabe Sechan