Android NDK - write only in C/C++

10,816

Since Android 2.3 (API Level 9) there is the NativeActivity which allows one to code an Android app in C++ only. There is also an example for this in the NDK package.

A quote from the NDK Overview:

When to Develop in Native Code

The NDK will not benefit most applications. As a developer, you need to balance its benefits against its drawbacks; notably, using native code does not result in an automatic performance increase, but always increases application complexity. In general, you should only use native code if it is essential to your application, not just because you prefer to program in C/C++.

Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on. Simply re-coding a method to run in C usually does not result in a large performance increase. When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need. The NDK can, however, can be an effective way to reuse a large corpus of existing C/C++ code.

The Android framework provides two ways to use native code:

  • Write your application using the Android framework and use JNI to access the APIs provided by the Android NDK. This technique allows you to take advantage of the convenience of the Android framework, but still allows you to write native code when necessary. You can install applications that use native code through the JNI on devices that run Android 1.5 or later.

  • Write a native activity, which allows you to implement the lifecycle callbacks in native code. The Android SDK provides the NativeActivity class, which is a convenience class that notifies your native code of any activity lifecycle callbacks (onCreate(), onPause(), onResume(), etc). You can implement the callbacks in your native code to handle these events when they occur. Applications that use native activities must be run on Android 2.3 (API Level 9) or later.

You cannot access features such as Services and Content Providers natively, so if you want to use them or any other framework API, you can still write JNI code to do so.

I would take C/C++ when porting code and possibly when developing cross platform games.

Share:
10,816
Aristarhys
Author by

Aristarhys

Sad man behind a screen. Why you siting in cubicle alone? You are not.

Updated on June 26, 2022

Comments

  • Aristarhys
    Aristarhys almost 2 years

    Is there a possible way to write a whole NDK app with C/C++ without a Java "starter" class like in hello-jni sample project (HelloJni.java) - somehow create a HelloJni.c which will do the same?

  • Fake Name
    Fake Name over 6 years
    but always increases application complexity Wow, citation majorly needed, android docs writer.