16

My basic task is to create a native service in android and then write a simple native program to test it. lets say I want to write a simple service which return me sum of two integers. I have to use binders to talk to it from the program, I have tried to google around but I can't find a precise example. I need to know how to create a native service and find it in the program and if needed in Java also.

3 Answers 3

14

If you're creating a normal Android application using the NDK, you can't use Binder because it's not part of the NDK APIs.

Look in the NDK docs/STABLE-APIS.html for the full list of stable APIs, and this thread for an explicit comment by David Turner (the NDK maintainer) that Binder is not a supported API.

https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/1QmVRrNckfM

Your options are:

  • Use some other form of IPC in native code - for example a UNIX domain socket
  • Do it in Java, using the normal Service and AIDL facilities of the Android SDK. If you wish to combine this with native code you may be able to call up to Java from native code using JNI.
  • (Not recommended) Copy the relevant libraries and headers from an Android Open-Source Project; build into your NDK project; and use the APIs. However this is not officially supported and is extremely likely to break your application in future releases because Google are under no obligation to maintain compatibility in such libraries (and frequently do not). It's also very difficult, since you need to find some way to register the service such that the client can find it.
Sign up to request clarification or add additional context in comments.

Comments

13

The solution that I found is to use the Binders in native and use the

defaultServiceManager()->addService( String16("TestService"),new CalcService()); 

and then use binders and use following on client side.

sp<IServiceManager> sm = defaultServiceManager(); sp<IBinder> binder = sm->getService(String16("TestService")); 

I found examples here on how to do this: https://github.com/gburca/BinderDemo/blob/master/binder.cpp

3 Comments

I cant find libbinder.so for linking? How did you resolve that?
@myCodeHurts: The libbinder.so is available while building AOSP. I dont think its available in NDK.
its is not helping in android11.Getting message like : assertion failed at file external/BinderDemo/binder.cpp, line 222, function getDemoServ:
5

After studying and coding @ Android NDK, I found The binder API is NOT available in Android NDK. And even if you use android open source for invoking the binder api, maybe you will get permission denied because of the binder security checking.

Here if I want to add a service to System service, I need a system level user group. The detail codes you can find https://github.com/qianjigui/android_system_service_example. It contains C and Java level's client and service, but you need the system permission.

1 Comment

Regarding permissions, if your Android version uses SELinux, this might be helpful: source.android.com/security/selinux/implement Remember to use /dev/binder or /dev/vndbinder depending on your version too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.