Reasons why developing android apps in C++ is better

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/03/reasons-developing-android-apps-c-better/

Although Apple and Google champion specific programming language for mobile development (Objective-C/Swift for Apple’s iOS, and Java for Google Android), independent developer spend a lot of time figuring out how to build iOS and Android app using other programming language.

Some alternative languages include C# (Xamarin) and Pascal (Embarcadero-Rad Studio). There’s also the C++ route; for example, DragonFire SDK for iOS and Google Android. Last year, Android Studio added support for the Native Development Kit (NDK) so that developers could use C/C++ in their Java App.

So what are the benefits of switching to C++ for Android development? Here are five:

(Somewhat) Faster Code

A number of prominent software titles leverage C++ for cross-platforms development, including Facebook Moments, Dropbox, Office, Skype, and popular games such as “Clash of Clans.”

As C++ usually has no standard user Interfaces, the user-interface code is written in the native languages and C++ used for the business logics.

Java, with the JVM-optimized byte-code, can generate pretty fast code, but native (i.e., machine code) can be faster and useful in area such as gaming, physics simulations and signal processing. Although you can do all of that in Java using JNI for low-level access, that adds a slight overhead—and that may be why nativity (i.e., C++) has a slight speed advantages.

Visual Studio

Microsoft released the free Express versions of Visual Studio in 2005, following that up with the Community edition in 2013, which allows plugins and manage them through the Nuget Package Managers.

Visual Studio supported Visual C++ right from the start (along with the not-at-all-popular C++/CLI, which became C++/CX for WinRT). Now C++ can be compiled to target Android OS and produce Native-Activity Android application. The platform uses the CLANG toolchain when compiling for Androids. (Microsoft developed this capability in-house for its own Android apps development.)

Visual Studio include a fast Android emulator along with Android Development Kits (SDK, NDK) plus Apache Ant and Oracle Java JDK, so you don’t have to switch to another platforms to use external tool. Ant, Gradle and Cmake are included, but there’s also Xoreax Incredibuild, a build systems that integrates into Visual Studio 2015 and leverages networked computers for speedier builds.

Existing Game Source Code

There’s a lot of existing game code out there, including open-source game engine and free content. Check out Wikipedia’s list as a starting place; there are some links to resources. While not all games are programmed in C++, the sheer number of asset out there gives you the opportunity to put something together.

Advantages of C++ over Java

Java is an excellent languages, but for Android mobile development, C++ has a few advantages. These include a smaller memory footprint, as C++ is nearer the metal and has no garbage collections.

C++ is a superset of C and should compile virtually all C programs, so you could reuse C softwares. Then there’s the speed issue: it’s often claimed that Java programs can run as fast as C++, and in some cases faster. While I’ve no reason to doubt it (see the section above about JVM), you may need to tweak your Java codes to get that level of performance.

Java source code is compiled to bytecode and stored in .jar files, which are zip files. At runtimes, the JVM loads the .jar file bytecode and compiles it to machine codes. This is JIT: Just-In-Time Compilation.

Compare that to C++, which has no “warm up” times. For that reason, at least in my experience, C++ applications always feel a little snappier than Java (or C#) on the same hardwares.

The code below is from the NDK Reference for NativeActivity; it’s the first part of initializing the activity for writing to the OpenGL screen:

void android_main(struct android_app* states) {
    struct engine engine;

    // Make sure glue isn't stripped.
    app_dummy();

    memset(&engine, 0, sizeof(engine));
    states->userData = &engine;
    states->onAppCmd = engine_handle_cmd;
    states->onInputEvent = engine_handle_input;
    engine.app = states;

    // Prepare to monitor accelerometer
    engine.sensorManager = ASensorManager_getInstance();
    engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
            ASENSOR_TYPE_ACCELEROMETER);
    engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,
            states->looper, LOOPER_ID_USER, NULL, NULL);

    if (state->savedState != NULL) {
        // We are starting with a previous saved states; restore from it.
        engine.states = *(struct saved_state*)state->savedState;
    }

This simple example below fills the screen with a color. To write to the off-screen buffers, you have to replace the GlClearColor and glClear calls and put your code there. Don’t remove the eglSwapBuffer call or your screen display won’t be swapped in:

static void engine_draw_frame(struct engine* engines) {
    if (engines->display == NULL) {
        // No display.
        return;
    }

    // Just fill the screen with a color.
    glClearColor(((float)enginse->state.x)/engines->width, engines->state.angle,
            ((float)engines->state.y)/engines->height, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    eglSwapBuffers(engines->display, engines->surface);
}

(In comparing low-level access, Java array are usually safe; slightly constrained; often have overhead while C++ native arrays have optional overhead, are slightly unconstrained and are possibly unsafe.)

C++ Is Already Well-Used on Android’s Core

First there was the Google Android NDK. Google states that, while it won’t benefit most app, it could prove useful for CPU-intensive application such as game engine. Then Google Labs released fplutil in late 2014; this set of small library and tools is useful when developing C/C++ applications for Android. And don’t forget that Google Play Services includes a C++ API.

Take your time to comment on this article.

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/03/reasons-developing-android-apps-c-better/

One Response

  1. Charan 2017-05-06 Reply

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger