Embedding C++ Code in Android Java Applications - Embedded.com

Embedding C++ Code in Android Java Applications

Development of Android applications is typically done in the Java programming language and Java Android applications are compiled to bytecode prior to execution, rather than native code, so that they may be run on any compatible Java Virtual Machine (JVM) regardless of the underlying architecture of the device.

The cross-platform compatibility of Java comes at the cost of performance: a device executing a Java application must also deal with the overhead required to run an instance of JVM in addition to executing the application itself.

C and C++ allow developers direct access to low-level features of the underlying operating system, giving skilled programmers the opportunity to write highly optimized code. Since Java runs within a virtual machine, access to such low-level functions is abstracted.

With the introduction of the Android Native Developer Kit (NDK) in 2009, developers were given the ability to include compiled ARM code in their Android applications.

The NDK allows code written in C and C++ to be compiled to ARM machine code and called from within the rest of the Java application. In this way, critical sections of code may be precompiled to theoretically improve performance.

Native code segments are then called by using the Java Native Interface (JNI), which itself adds processing overhead and increases the complexity of the application. For this reason, developers must weigh the potential gains in performance against the drawbacks and difficulties of using the JNI and NDK before making the decision to implement sections of their applications in C or C++.

This work examines the potential performance increases that can be achieved by embedding natively-compiled C++ code in Java Android applications, the cost of its implementation and the circumstances under which the added complexity of embedding native code is worth these costs.

This report explores both the theoretical performance gains possible with embedded native code and actual results by testing performance gains and program complexity in a fully functional Android application as well as in simulated test applications.

The results found indicate that while un-optimized Java code generally performs significantly worse than similar implementations in C++, Android versions 2.1 and higher, which include a just-in-time Java compiler, are able to optimize Java code and provide performance roughly equal to or even exceeding that of C++ code.

Nonetheless, specialized C++ libraries exist that are able to outperform even optimized Java code. This shows that depending on the application, significant performance gains can be obtained through use of C++ code in Android programs, though this is not always the case.

To read this external content in full, download the complete paper from the author archives on line .

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.