Write Applications in C or C++ for Android

Write applications in C or C++ for Android?

For anyone coming to this via Google, note that starting from SDK 1.6 Android now has an official native SDK.

You can download the Android NDK (Native Development Kit) from here:
https://developer.android.com/ndk/downloads/index.html

Also there is an blog post about the NDK:

http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html

Android: Java, C or C++?

The article you link to has good information. It also links to http://developer.android.com/sdk/ndk/overview.html which says:

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.

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

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.

Android application only in C++

You can write code for Android in C++, if you have the appropriate programming tools. You'll need a cross-compiler and linker for the appropriate architecture. These are readily available in Google's Native Development Kit:

https://developer.android.com/ndk/

I don't know if these tools can be used with Visual Studio -- I suspect it will be fiddly to set up.

The problem with programming in C or C++ for Android is that it's hard to integrate the application with the regular launcher and, even if you can do that, it's hard to provide a regular Android graphical user interface. Android simply does not provide a C API for the user interface.

I write/port a lot of command-line stuff for Android, so the lack of a graphical user interface doesn't bother me. However, most developers provide the user interface in Java and, if parts of the app needs to be in C or C++, it gets integrated with the app in the form of a native library.

Android Studio and similar tools know how to manage a project that has a mixture of C or C++ and Java code. Using well-established Android tools for developing complex, mixed-code apps like this is way simpler than trying to figure it all out yourself using a hodge-podge of tools from different places.

Is it possible to write applications on android using only C?

It is possible, this is how daemons work on Android (think RILD for example). However you cannot gain access to the Android facilities (graphics, location, etc) as there is no API from C.

Note that in order to speak to the Android API, your process needs to be a child of zygote. If you spawn a process from an ADB shell, or from init you will not be a fork() of zygote and will not have direct access to the JVM.

Android NDK - write only in C/C++

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.



Related Topics



Leave a reply



Submit