Dummy native binder android HelloWorld sample created to better understand the possibilities of creating pure native android services. The current status is that it certainly is possible to create native binder services under Android Scope: We are currently mostly interested in creating a native service started from init.rc and we would like this service to be pure native (no JVM). We want this service to act as much as possible like any other android service. We would also like it to be possible for the client of the service to not depend on Android internal API's. Limitations: The native service currently runs as system user and uses the ServiceManager.addService('name',binder) method. This method is not available to normal packages installed on a stock phone. For that limitation to be worked around it it probably is needed to let the android system (ActivityManager?) let create the IBinder object and pass it to the native On the following thread Dianne Hackborn states https://groups.google.com/d/topic/android-porting/yK17XZXkupI/discussion The Java runtime has wrappers around C++ IBinder. This is what Java's Binder is. You can write a JNI function that returns a Java Binder and in its implementation instantiate a C++ IBinder interface and return it. You will need to use this magic function in libandroid_runtime to do this: extern jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val); The header for this is currently in core/jni/android_util_Binder.h. It will be probably nececary to pass a Binder object created to the native code. An other problem might be related to the threading This code was taken from http://github.com/mcr/Android-HelloWorldService and modified to work on gingerbread. Reading the code: Best is to start reading libhelloworldservice/include/IHelloWorld.h comparing it to the code generated by aidl and follow with native implementations of that interface libhelloworldservice/src/helloworldservice.cpp libhelloworldservice/src/helloworldclient.cpp After that read the native "server" code helloworld/main_helloworldservice.cpp to see how the native code works to Find the usage read AndroidManifest.xml src/org/credil/helloworldservice/HelloWorldActivity.java and star fix code in src/org/credil/helloworldservice/HelloWorldService.java :P Building/playing around: For information about developing "inside" Android https://github.com/keesj/gomo/wiki/AndroidPlatformDevelopmentWorkflow Checkout this this branch into your android platform tree . build/envsetup.sh lunch sdk-eng make After that and goto the Android-HelloWorldService directory use mm/adb sync to build deploy Running: From the shell: # /system/bin/helloworldservice hello: fun hello: Using interfaces hello: Calling from Java now # /system/bin/helloworldclient To launch the Activity use the activity launcher. Native binder: /* Interfaces extend */ class IInterface extends RefBase + asBinder() + onAsBinder(); /* The native class implementation extends BnInterface * this is the instance that will be given to the service * manager * * BnInterface is the base implementation for a native (local) IInterface **/ class BnInterface<INTERFACE> extends BBinder + queryLocalInterface(descriptor) /** * The BBinder class is the standard implementation of IBinder for a IInterface object **/ class BBinder extends IBinder + getInterfaceDescriptor() + attachObject(id ...) /* housekeeping of clients? */ + findObject(id ...) + detachObject(id ...); /* remote stub implemntation extends */ class BpInterface<INTERFACE> extends BpRefBase + onAsBinder(); /* BpRefBase defines a ref counted class having a method to get * a binder object */ class BpRefBase extends RefBase + remote() IBinder*
chenguangshen/Android-HelloWorldService
This is a skeleton application that creates an Android service in C++, accessing it via Java/Binder.
C++