Android源码中注册服务一般有两种,一种是通过Java实现,另外一种是通过C++实现;本文介绍Native实现方式,Java实现方式请移步 [Android源码添加自定义系统服务 - Java],网上基本上也都是通过Java来自定义实现的。
创建对应目录结构
在./frameworks目录下是没有vendor目录的,vendor目录是新建用于调试的;可以在其它目录下创建测试目录。
aw@m:~/works/android/aosp/aosp11/frameworks/vendor$ tree .
.
└── service
├── aidl
│ └── android
│ └── sample
│ └── ISample.aidl
├── Android.bp
├── include
│ ├── aidl
│ │ ├── BnSample.h
│ │ ├── BpSample.h
│ │ └── ISample.h
│ └── SampleService.h
├── ISample.cpp
├── SampleService.cpp
└── test
├── SampleClient.cpp
└── SampleServer.cpp
7 directories, 10 files
aw@m:~/works/android/aosp/aosp11/frameworks/vendor$
创建AIDL文件,生成BnSample.cpp、BpSample.cpp、ISample.h、ISample.cpp文件
1. 在./frameworks/目录下,创建vendor/service两级目录
2. 在service目录下创建aidl目录,并创建ISample.aidl文件,路径图如上图
aw@m:~/works/android/aosp/aosp11/frameworks/vendor/service/aidl/android/sample$ pwd
/home/aw/works/android/aosp/aosp11/frameworks/vendor/service/aidl/android/sample
aw@m:~/works/android/aosp/aosp11/frameworks/vendor/service/aidl/android/sample$ ls -lhia
总用量 12K
33951777 drwxrwxr-x 2 aw aw 4.0K 11月 20 19:29 .
33951776 drwxrwxr-x 3 aw aw 4.0K 11月 20 19:28 ..
33951779 -rw-rw-r-- 1 aw aw 98 11月 20 19:29 ISample.aidl
aw@m:~/works/android/aosp/aosp11/frameworks/vendor/service/aidl/android/sample$
package android.sample;
interface ISample {
void doSomething(int n, out List<String> output);
}
3. 生成对应的BnSample.cpp、BpSample.cpp、ISample.h、ISample.cpp,文件生成方式有两种:
- 通过Android/SDK下的aidl指令 [推荐]
$: aidl --lang=cpp ./aidl/IAudioSample.aidl -o . -h ./include/
- 通过系统编译生成 [Android官方Soong编译指南,Soong编译-官方ReadMe]
在service目录下创建Android.bp文件,Android.bp
cc_library_shared {
name: "libsampleservice",
aidl: {
export_aidl_headers: true,
local_include_dirs: ["aidl"],
include_dirs: [
"frameworks/vendor/service/aidl",
],
},
srcs: [
":libsample_aidl",
],
shared_libs: [
"libbinder",
"libcutils",
"liblog",
"libutils",
],
export_shared_lib_headers: ["libbinder"],
local_include_dirs: [
"include",
"include/aidl",
"aidl",
],
include_dirs: [
"frameworks/native/include",
"system/core/base/include",
],
header_libs: [
"libbase_headers",
],
cflags: [
"-Werror",
"-Wno-error=deprecated-declarations",
],
}
filegroup {
name: "libsample_aidl",
srcs: [
"aidl/android/sample/ISample.aidl",
],
path: "aidl",
}
在./framworks/vendor下执行mm [Android编译命令],将会在out/soong/.intermediates/frameworks/vendor/service/libsampleservice下生成对应文件
aw@m:~/works/android/aosp/aosp11/out/soong/.intermediates/frameworks/vendor/service/libsampleservice$ ls
android_arm64_armv8-a_shared android_arm_armv8-a_shared
aw@m:~/works/android/aosp/aosp11/out/soong/.intermediates/frameworks/vendor/service/libsampleservice$ tree .
.
├── android_arm64_armv8-a_shared
│ ├── gen
│ │ └── aidl
│ │ ├── android
│ │ │ └── sample
│ │ │ ├── BnSample.h
│ │ │ ├── BpSample.h
│ │ │ └── ISample.h
│ │ └── frameworks
│ │ └── vendor
│ │ └── service
│ │ └── aidl
│ │ └── android
│ │ └── sample
│ │ ├── ISample.cpp
│ │ └── ISample.cpp.d
│ ├── libsampleservice.so
│ ├── libsampleservice.so.d
│ ├── libsampleservice.so.toc
│ ├── libsampleservice.so.toc.d
│ └── unstripped
│ ├── libsampleservice.so
│ └── libsampleservice.so.rsp
└── android_arm_armv8-a_shared
├── gen
│ └── aidl
│ ├── android
│ │ └── sample
│ │ ├── BnSample.h
│ │ ├── BpSample.h
│ │ └── ISample.h
│ └── frameworks
│ └── vendor
│ └── service
│ └── aidl
│ └── android
│ └── sample
│ ├── ISample.cpp
│ └── ISample.cpp.d
├── libsampleservice.so
├── libsampleservice.so.d
├── libsampleservice.so.toc
├── libsampleservice.so.toc.d
└── unstripped
├── libsampleservice.so
└── libsampleservice.so.rsp
取出对应BnSample.h、BpSample.h、ISample.h、ISample.cpp文件
BnSample.h
#ifndef AIDL_GENERATED_ANDROID_SAMPLE_BN_SAMPLE_H_
#define AIDL_GENERATED_ANDROID_SAMPLE_BN_SAMPLE_H_
#include <binder/IInterface.h>
#include <android/sample/ISample.h>
namespace android {
namespace sample {
class BnSample : public ::android::BnInterface<ISample> {
public:
explicit BnSample();
::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) override;
}; // class BnSample
} // namespace sample
} // namespace android
#endif // AIDL_GENERATED_ANDROID_SAMPLE_BN_SAMPLE_H_
BpSample.h
#ifndef AIDL_GENERATED_ANDROID_SAMPLE_BP_SAMPLE_H_
#define AIDL_GENERATED_ANDROID_SAMPLE_BP_SAMPLE_H_
#include <binder/IBinder.h>
#include <binder/IInterface.h>
#include <utils/Errors.h>
#include <android/sample/ISample.h>
namespace android {
namespace sample {
class BpSample : public ::android::BpInterface<ISample> {
public:
explicit BpSample(const ::android::sp<::android::IBinder>& _aidl_impl);
virtual ~BpSample() = default;
::android::binder::Status doSomething(int32_t n, ::std::vector<::android::String16>* output) override;
}; // class BpSample
} // namespace sample
} // namespace android
#endif // AIDL_GENERATED_ANDROID_SAMPLE_BP_SAMPLE_H_
ISample.h
#ifndef AIDL_GENERATED_ANDROID_SAMPLE_I_SAMPLE_H_
#define AIDL_GENERATED_ANDROID_SAMPLE_I_SAMPLE_H_
#include <binder/IBinder.h>
#include <binder/IInterface.h>
#include <binder/Status.h>
#include <cstdint>
#include <utils/String16.h>
#include <utils/StrongPointer.h>
#include <vector>
namespace android {
namespace sample {
class ISample : public ::android::IInterface {
public:
DECLARE_META_INTERFACE(Sample)
virtual ::android::binder::Status doSomething(int32_t n, ::std::vector<::android::String16>* output) = 0;
}; // class ISample
class ISampleDefault : public ISample {
public:
::android::IBinder* onAsBinder() override {
return nullptr;
}
::android::binder::Status doSomething(int32_t, ::std::vector<::android::String16>*) override {
return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);
}
}; // class ISampleDefault
} // namespace sample
} // namespace android
#endif // AIDL_GENERATED_ANDROID_SAMPLE_I_SAMPLE_H_
ISample.cpp
#include <aidl/ISample.h>
#include <aidl/BpSample.h>
namespace aidl {
DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(Sample, "aidl.ISample")
} // namespace aidl
#include <aidl/BpSample.h>
#include <aidl/BnSample.h>
#include <binder/Parcel.h>
#include <android-base/macros.h>
namespace aidl {
BpSample::BpSample(const ::android::sp<::android::IBinder>& _aidl_impl)
: BpInterface<ISample>(_aidl_impl){
}
::android::binder::Status BpSample::doSomething(int32_t n, ::std::vector<::android::String16>* output) {
::android::Parcel _aidl_data;
_aidl_data.markForBinder(remoteStrong());
::android::Parcel _aidl_reply;
::android::status_t _aidl_ret_status = ::android::OK;
::android::binder::Status _aidl_status;
_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_ret_status = _aidl_data.writeInt32(n);
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_ret_status = remote()->transact(BnSample::TRANSACTION_doSomething, _aidl_data, &_aidl_reply, 0);
if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && ISample::getDefaultImpl())) {
return ISample::getDefaultImpl()->doSomething(n, output);
}
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
if (!_aidl_status.isOk()) {
return _aidl_status;
}
_aidl_ret_status = _aidl_reply.readString16Vector(output);
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_error:
_aidl_status.setFromStatusT(_aidl_ret_status);
return _aidl_status;
}
} // namespace aidl
#include <aidl/BnSample.h>
#include <binder/Parcel.h>
#include <binder/Stability.h>
namespace aidl {
BnSample::BnSample()
{
::android::internal::Stability::markCompilationUnit(this);
}
::android::status_t BnSample::onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) {
::android::status_t _aidl_ret_status = ::android::OK;
switch (_aidl_code) {
case BnSample::TRANSACTION_doSomething:
{
int32_t in_n;
::std::vector<::android::String16> out_output;
if (!(_aidl_data.checkInterface(this))) {
_aidl_ret_status = ::android::BAD_TYPE;
break;
}
_aidl_ret_status = _aidl_data.readInt32(&in_n);
if (((_aidl_ret_status) != (::android::OK))) {
break;
}
::android::binder::Status _aidl_status(doSomething(in_n, &out_output));
_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
if (((_aidl_ret_status) != (::android::OK))) {
break;
}
if (!_aidl_status.isOk()) {
break;
}
_aidl_ret_status = _aidl_reply->writeString16Vector(out_output);
if (((_aidl_ret_status) != (::android::OK))) {
break;
}
}
break;
default:
{
_aidl_ret_status = ::android::BBinder::onTransact(_aidl_code, _aidl_data, _aidl_reply, _aidl_flags);
}
break;
}
if (_aidl_ret_status == ::android::UNEXPECTED_NULL) {
_aidl_ret_status = ::android::binder::Status::fromExceptionCode(::android::binder::Status::EX_NULL_POINTER).writeToParcel(_aidl_reply);
}
return _aidl_ret_status;
}
} // namespace aidl
创建自定义Service
1. 创建SampleService.h
#ifndef SAMPLE_SERVICE_H
#define SAMPLE_SERVICE_H
#include <BnSample.h>
#include <vector>
namespace android {
class SampleService : public android::sample::BnSample {
public:
SampleService();
~SampleService();
virtual ::android::binder::Status doSomething(int32_t n, ::std::vector<::android::String16>* output);
};
}
#endif
2. 创建SampleService.cpp
#include <SampleService.h>
using namespace android;
SampleService::SampleService() {}
SampleService::~SampleService() {}
::android::binder::Status SampleService::doSomething(int32_t n, ::std::vector<::android::String16>* output) {
for (int i = 0; i < n; i++) {
output->push_back(String16("Hello"));
}
return ::android::binder::Status::ok();
}
3. 修改Android.bp,在srcs中补充了SampleService.cpp源文件
cc_library_shared {
name: "libsampleservice",
aidl: {
export_aidl_headers: true,
local_include_dirs: ["aidl"],
include_dirs: [
"frameworks/vendor/service/aidl",
],
},
srcs: [
":libsample_aidl",
"SampleService.cpp",
],
shared_libs: [
"libbinder",
"libcutils",
"liblog",
"libutils",
],
export_shared_lib_headers: ["libbinder"],
local_include_dirs: [
"include",
"include/aidl",
"aidl",
],
include_dirs: [
"frameworks/native/include",
"system/core/base/include",
],
header_libs: [
"libbase_headers",
],
cflags: [
"-Werror",
"-Wno-error=deprecated-declarations",
],
}
注册服务
1. 在./framworks/vendor/service/test下创建SampleServer.cpp
#include <sys/types.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <cutils/log.h>
#include <SampleService.h>
using namespace android;
int main(int argc, char** argv) {
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
ALOGI("ServiceManager: %p", sm.get());
sm->addService(String16("SampleService"), new SampleService());
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
return 0;
}
客户端,调用服务
1. 在./framworks/vendor/service/test下创建SampleClient.cpp
#include <binder/IServiceManager.h>
#include <binder/Parcel.h>
#include <utils/Errors.h>
#include <stdio.h>
#include <String16.h>
#include <String8.h>
#include <vector>
#include <iostream>
#include <SampleService.h>
using namespace android;
int main () {
sp<android::sample::ISample> sample_srv = interface_cast<android::sample::ISample>(defaultServiceManager()->getService(String16("SampleService")));
std::vector<String16> hellos;
sample_srv->doSomething(3, &hellos);
for (String16 s:hellos ) {
std::cout << String8(s.string()).string() << std::endl;
}
return 0;
}
2. 修改Android.bp
cc_library_shared {
name: "libsampleservice",
aidl: {
export_aidl_headers: true,
local_include_dirs: ["aidl"],
include_dirs: [
"frameworks/vendor/service/aidl",
],
},
srcs: [
":libsample_aidl",
"SampleService.cpp",
],
shared_libs: [
"libbinder",
"libcutils",
"liblog",
"libutils",
],
export_shared_lib_headers: ["libbinder"],
local_include_dirs: [
"include",
"include/aidl",
"aidl",
],
include_dirs: [
"frameworks/native/include",
"system/core/base/include",
],
header_libs: [
"libbase_headers",
],
cflags: [
"-Werror",
"-Wno-error=deprecated-declarations",
],
}
filegroup {
name: "libsample_aidl",
srcs: [
"aidl/android/sample/ISample.aidl",
],
path: "aidl",
}
// Server
cc_binary {
name: "sampleService",
srcs: [
"./test/SampleServer.cpp",
],
local_include_dirs: [
"include",
"include/aidl",
],
include_dirs: [
"frameworks/native/include",
"system/core/base/include",
],
header_libs: [
"libbase_headers",
],
cflags: [
"-Werror",
"-Wno-error=deprecated-declarations",
"-Wall",
"-Wno-unused-parameter",
],
shared_libs: [
"libbinder",
"libcutils",
"liblog",
"libutils",
"libsampleservice",
],
}
// Client
cc_binary {
name: "sampleClient",
srcs: [
"./test/SampleClient.cpp",
],
local_include_dirs: [
"include",
"include/aidl",
],
include_dirs: [
"frameworks/native/include",
"system/core/base/include",
"system/core/include/utils",
],
header_libs: [
"libbase_headers",
],
cflags: [
"-Werror",
"-Wno-error=deprecated-declarations",
"-Wall",
"-Wno-unused-parameter",
],
shared_libs: [
"libbinder",
"libcutils",
"liblog",
"libutils",
"libsampleservice",
],
}
编译
我已经编译过,所以不会有什么生成日志,贴张图让大家知道在哪里执行mm指令 [Android编译命令]
aw@m:~/works/android/aosp/aosp11/frameworks/vendor$ mm
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=11
TARGET_PRODUCT=aosp_bonito
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv8-a
TARGET_2ND_CPU_VARIANT=generic
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-5.11.0-40-generic-x86_64-Ubuntu-20.04.3-LTS
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=RQ3A.210905.001
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=device/google/bonito hardware/google/av hardware/google/camera hardware/google/interfaces hardware/google/pixel hardware/qcom/sdm845 vendor/google/camera vendor/qcom/sdm845 vendor/google/interfaces vendor/qcom/bonito/proprietary
============================================
ninja: no work to do.
#### build completed successfully (2 seconds) ####
编译结果
aw@m:~/works/android/aosp/aosp11/out/soong/.intermediates/frameworks/vendor$ tree .
.
└── service
├── libsampleservice
│ ├── android_arm64_armv8-a_shared
│ │ ├── gen
│ │ │ └── aidl
│ │ │ ├── android
│ │ │ │ └── sample
│ │ │ │ ├── BnSample.h
│ │ │ │ ├── BpSample.h
│ │ │ │ └── ISample.h
│ │ │ └── frameworks
│ │ │ └── vendor
│ │ │ └── service
│ │ │ └── aidl
│ │ │ └── android
│ │ │ └── sample
│ │ │ ├── ISample.cpp
│ │ │ └── ISample.cpp.d
│ │ ├── libsampleservice.so
│ │ ├── libsampleservice.so.d
│ │ ├── libsampleservice.so.toc
│ │ ├── libsampleservice.so.toc.d
│ │ ├── obj
│ │ │ └── frameworks
│ │ │ └── vendor
│ │ │ └── service
│ │ │ ├── SampleService.o
│ │ │ └── SampleService.o.d
│ │ └── unstripped
│ │ ├── libsampleservice.so
│ │ └── libsampleservice.so.rsp
│ └── android_arm_armv8-a_shared
│ ├── gen
│ │ └── aidl
│ │ ├── android
│ │ │ └── sample
│ │ │ ├── BnSample.h
│ │ │ ├── BpSample.h
│ │ │ └── ISample.h
│ │ └── frameworks
│ │ └── vendor
│ │ └── service
│ │ └── aidl
│ │ └── android
│ │ └── sample
│ │ ├── ISample.cpp
│ │ └── ISample.cpp.d
│ ├── libsampleservice.so
│ ├── libsampleservice.so.d
│ ├── libsampleservice.so.toc
│ ├── libsampleservice.so.toc.d
│ ├── obj
│ │ └── frameworks
│ │ └── vendor
│ │ └── service
│ │ ├── SampleService.o
│ │ └── SampleService.o.d
│ └── unstripped
│ ├── libsampleservice.so
│ └── libsampleservice.so.rsp
├── sampleClient
│ └── android_arm64_armv8-a
│ ├── obj
│ │ └── frameworks
│ │ └── vendor
│ │ └── service
│ │ └── test
│ │ ├── SampleClient.o
│ │ └── SampleClient.o.d
│ ├── sampleClient
│ ├── sampleClient.d
│ └── unstripped
│ ├── sampleClient
│ └── sampleClient.rsp
└── sampleService
└── android_arm64_armv8-a
├── obj
│ └── frameworks
│ └── vendor
│ └── service
│ └── test
│ ├── SampleServer.o
│ └── SampleServer.o.d
├── sampleService
├── sampleService.d
└── unstripped
├── sampleService
└── sampleService.rsp
50 directories, 38 files
测试验证
将libsampleservice.so放到手机 [已root] 的system/lib64和system/lib目录下,重启设备
bonito:/system/lib64 # ls -lhia libsampleservice.so
86 -rwxrwxrwx 1 root root 33K 2021-11-20 20:05 libsampleservice.so
bonito:/system/lib64 # chmod 777 libsampleservice.so
bonito:/system/lib64 # exit
aw@m:~/Downloads$ adb reboot
将sampleService和sampleClient可执行文件放到/data/local/tmp目录下,执行测试,输出三遍Hello表示成功了
bonito:/data/local/tmp # ls
lldb-server localLogcat perfd sampleClient sampleService
bonito:/data/local/tmp # ./sampleService &
[1] 9389
bonito:/data/local/tmp # ./sampleClient
Hello
Hello
Hello
bonito:/data/local/tmp #