Search code examples
android-ndkbackground-process

Creating a native background application using ndk


How to create more than 1 background applications in a single Android application using Android NDK ?

Need help in creating few background applications using NDK. My C application makes use of few standalone applications which run in the background (as services on windows). So my question is how can I create these standalone applications on Andriod platform?

I'm aware that these have to be services in Android applications, but services are Java code & NDK doesn't support C services & all my application code is in C. So how can I port these standalone applications onto Android along with my main application which uses these standalone helper applications. Should they be made separate libraries ?

If they are made separate libraries, then how can I ensure that they keep running in the background? I'm not sure, so any guidance will be very helpful. I don't want to root the device.


Solution

  • Android is very different from Windows, porting Windows services may be a significant challenge.

    First of all, the IPC protocols are not the same, therefore you may need modifications to simply have your code compile.

    The second issue is the background execution model of Android. On the newer versions, since Marshmallow, Google is improving the battery life by proactively killing all background services.

    So, if you depend on these processes to be running always, you must keep your app in the foreground.

    This said, you can build and run a command line executable for Android without wrapping it into an Android (Java) application or Service.

    You can run them from the android shell (at least for testing), or use Runtime.exec() from Java. If you root the device, you can even start these executables on boot from init.rc, just as if it were a regular Linux box.