AOSP – Adding a Native Daemon

Android is a great OS for many devices and not mobile only. You can even use Android on devices without display (headless). The most important thing about Android is the fact that it is open source and you can build everything from source.

Android structure:

The lower layer is Linux kernel with some changes (little)

The Native Layer is built above the kernel and hosts the C library, other system libraries, and many daemons

The Application Framework layer hosts all the Android services used by Android Applications

In this post, we will build a Native Daemon in the native layer, make it run on system startup and using system property and build application to communicate with our daemon.


Create a directory in device/generic/goldfish/bin/mylogger

Add mylogger.c

Add file


If we add the service definition to init.rc file without a security label we will get:

service mylogger does not have a SELinux domain defined

So we define the security label for it:

We need to define the system property for SE Android. Add the following line to property_contexts file:


Writing Client Application

We can write a client application using Android Studio. Simply add a button and write a click handler, use a thread to send a UDP message to the daemon:

Add INTERNET permission request to AndroidManifest.xml file

To set the system property we need to write a system application with C++ code:

To use property_set function you need to add libcutils to

The last thing to do is add an allow rule to system_app to set the property:

Add the following rule to system_app.te

Now setting the system property is triggering start and stop and using the client application we can send messages to the daemon. To see the result cat the file data1:


Tagged , ,

7 thoughts on “AOSP – Adding a Native Daemon

  1. How will the system property be set? From Settings UI?

  2. “A client app calling a Native daemon’s Socket” –
    Is this possible WHEN trebLe is enabled?

  3. …to be more specific in my previous question:
    Is it possible for a vendor service created socket be accessed by a system PRIV app or an installed app, when treble is enabled?

  4. Thanks for this simple yet informative guide.
    I am having same question as Rayen. Wondering how treble will affect this design, along with the changes in Android P.

  5. Where setService method is called from?

  6. Hello!

    Thanks for the example
    Where should the init.rc and the policy go?

  7. Hi, how can I develop and debug my service project (ie mylogger.c) with a IDEsuch as Eclipse or VS Code?

Leave a Reply

Your email address will not be published.