ANDROID11 添加系统服务记录

1:添加aidl文件

frameworks\base\core\java\android\os

下创建myservices文件夹

添加 ITestManager.aidl,TestManager.java

package android.os.myservices;

interface ITestManager {
    void testMethod();

}
package android.os.myservices;

import android.util.Slog;
import android.os.RemoteException;
import android.annotation.SystemService;
import android.content.Context;

public class TestManager {

    private final ITestManager mService;

    public TestManager(ITestManager mService) {
        //这里把ITestManager传进来,可以看看系统其它service,都是这样写的
        this.mService = mService;
    }

    public void testMethod() {
        try {
            mService.testMethod();
            Slog.i("add_service_test", "TestManager testMethod:");
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }
//    public native String stringFromJNI();
}

将.aidl文件添加到frameworks/base/Android.bp

说明:android.bp文件中默认把core/java/目录下的aidl文件添加到编译文件中,所以这一步不需要操作.

filegroup {
    name: "framework-core-sources",
    srcs: [
        "core/java/**/*.java",
        "core/java/**/*.aidl",
    ],
    path: "core/java",
}

由于Android 11对语法检测比较严格,所以针对我们新增的文件先加入忽略:

// TODO(b/145644363): move this to under StubLibraries.bp or ApiDocs.bp
metalava_framework_docs_args = "--manifest $(location core/res/AndroidManifest.xml) " +
    "--ignore-classes-on-classpath " +
    "--hide-package com.android.server " +
    "--error UnhiddenSystemApi " +
    "--hide RequiresPermission " +
    "--hide CallbackInterface " +
    "--hide MissingPermission --hide BroadcastBehavior " +
    "--hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol " +
    "--hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo " +
    "--force-convert-to-warning-nullability-annotations +*:-android.*:+android.icu.*:-dalvik.* " +
    "--api-lint-ignore-prefix android.icu. " +
    "--api-lint-ignore-prefix java. " +
    "--api-lint-ignore-prefix android.os. " +   //新增这一行 
    "--api-lint-ignore-prefix android.app. " +  //新增这一行  
    "--api-lint-ignore-prefix junit. " +
    "--api-lint-ignore-prefix org. "

添加server文件 

frameworks\base\services\core\java\com\android\server

TestService.java
package com.android.server;

import android.content.Context;
import android.util.Log;
import android.os.myservices.ITestManager;

//这里的ITestManager.Stub是固定写法
public class TestService extends ITestManager.Stub {
    private final Context mContext;

    public TestService(Context context) {
        super();
        mContext = context;
    }

    public void testMethod() {
        // 测试方法,为了测试执行情况,在这里加log
        Log.i("add_service_test", "TestService getString ");
    }
}

注册服务

frameworks\base\core\java\android\app\SystemServiceRegistry.java

        registerService("test_service",TestManager.class,
                new CachedServiceFetcher<TestManager>(){
            @Override
            public TestManager createService(ContextImpl ctx) {
                IBinder b = ServiceManager.getService("test_service");
                Log.i("add_service_test","SystemServiceRegistry registerService method");
                return new TestManager(ITestManager.Stub.asInterface(b));
        }});

添加服务frameworks\base\services\java\com\android\server\SystemServer

            // add test service
            t.traceBegin("TestService");
            ServiceManager.addService("test_service", new TestService(context));
            t.traceEnd();

添加selinux权限

Android 11的 selinux 规则是放在 system/sepolicy 目录下的。注意需要修改所有的service.te和service_contexts文件

diff --git a/system/sepolicy/prebuilts/api/30.0/private/compat/26.0/26.0.ignore.cil b/system/sepolicy/prebuilts/api/30.0/private/compat/26.0/26.0.ignore.cil
index b395855afc..eeb43b3591 100755
--- a/system/sepolicy/prebuilts/api/30.0/private/compat/26.0/26.0.ignore.cil
+++ b/system/sepolicy/prebuilts/api/30.0/private/compat/26.0/26.0.ignore.cil
@@ -202,6 +202,7 @@
     vendor_init
     vendor_shell
     vendor_socket_hook_prop
+    test_service
     vndk_prop
     vold_metadata_file
     vold_prepare_subdirs
diff --git a/system/sepolicy/prebuilts/api/30.0/private/compat/27.0/27.0.ignore.cil b/system/sepolicy/prebuilts/api/30.0/private/compat/27.0/27.0.ignore.cil
index cb500c9e0d..ebece3b94d 100755
--- a/system/sepolicy/prebuilts/api/30.0/private/compat/27.0/27.0.ignore.cil
+++ b/system/sepolicy/prebuilts/api/30.0/private/compat/27.0/27.0.ignore.cil
@@ -180,6 +180,7 @@
     vendor_security_patch_level_prop
     vendor_shell
     vendor_socket_hook_prop
+    test_service
     vndk_prop
     vold_metadata_file
     vold_prepare_subdirs
diff --git a/system/sepolicy/prebuilts/api/30.0/private/compat/28.0/28.0.ignore.cil b/system/sepolicy/prebuilts/api/30.0/private/compat/28.0/28.0.ignore.cil
index d24d12d257..f79ee064fe 100755
--- a/system/sepolicy/prebuilts/api/30.0/private/compat/28.0/28.0.ignore.cil
+++ b/system/sepolicy/prebuilts/api/30.0/private/compat/28.0/28.0.ignore.cil
@@ -153,6 +153,7 @@
     vendor_misc_writer
     vendor_misc_writer_exec
     vendor_socket_hook_prop
+    test_service
     vendor_task_profiles_file
     vndk_prop
     vrflinger_vsync_service
diff --git a/system/sepolicy/prebuilts/api/30.0/private/compat/29.0/29.0.ignore.cil b/system/sepolicy/prebuilts/api/30.0/private/compat/29.0/29.0.ignore.cil
index e4aaef5f48..3e9130758c 100755
--- a/system/sepolicy/prebuilts/api/30.0/private/compat/29.0/29.0.ignore.cil
+++ b/system/sepolicy/prebuilts/api/30.0/private/compat/29.0/29.0.ignore.cil
@@ -129,4 +129,5 @@
     vendor_service_contexts_file
     vendor_socket_hook_prop
     vendor_socket_hook_prop
+    test_service
     virtual_ab_prop))
diff --git a/system/sepolicy/prebuilts/api/30.0/private/service_contexts b/system/sepolicy/prebuilts/api/30.0/private/service_contexts
index 5c6f1a4766..10d4315e36 100755
--- a/system/sepolicy/prebuilts/api/30.0/private/service_contexts
+++ b/system/sepolicy/prebuilts/api/30.0/private/service_contexts
@@ -150,6 +150,7 @@ network_stack                             u:object_r:network_stack_service:s0
 network_management                        u:object_r:network_management_service:s0
 network_score                             u:object_r:network_score_service:s0
 network_time_update_service               u:object_r:network_time_update_service:s0
+test_service               u:object_r:test_service:s0
 nfc                                       u:object_r:nfc_service:s0
 notification                              u:object_r:notification_service:s0
 oem_lock                                  u:object_r:oem_lock_service:s0
diff --git a/system/sepolicy/prebuilts/api/30.0/public/service.te b/system/sepolicy/prebuilts/api/30.0/public/service.te
index f27772eabb..be4c8fd6ec 100755
--- a/system/sepolicy/prebuilts/api/30.0/public/service.te
+++ b/system/sepolicy/prebuilts/api/30.0/public/service.te
@@ -134,6 +134,7 @@ type netstats_service, app_api_service, ephemeral_app_api_service, system_server
 type network_management_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type network_score_service, system_api_service, system_server_service, service_manager_type;
 type network_stack_service, system_server_service, service_manager_type;
+type test_service, system_server_service, service_manager_type;
 type network_time_update_service, system_server_service, service_manager_type;
 type notification_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type oem_lock_service, system_api_service, system_server_service, service_manager_type;
diff --git a/system/sepolicy/private/compat/26.0/26.0.ignore.cil b/system/sepolicy/private/compat/26.0/26.0.ignore.cil
index b395855afc..eeb43b3591 100755
--- a/system/sepolicy/private/compat/26.0/26.0.ignore.cil
+++ b/system/sepolicy/private/compat/26.0/26.0.ignore.cil
@@ -202,6 +202,7 @@
     vendor_init
     vendor_shell
     vendor_socket_hook_prop
+    test_service
     vndk_prop
     vold_metadata_file
     vold_prepare_subdirs
diff --git a/system/sepolicy/private/compat/27.0/27.0.ignore.cil b/system/sepolicy/private/compat/27.0/27.0.ignore.cil
index cb500c9e0d..ebece3b94d 100755
--- a/system/sepolicy/private/compat/27.0/27.0.ignore.cil
+++ b/system/sepolicy/private/compat/27.0/27.0.ignore.cil
@@ -180,6 +180,7 @@
     vendor_security_patch_level_prop
     vendor_shell
     vendor_socket_hook_prop
+    test_service
     vndk_prop
     vold_metadata_file
     vold_prepare_subdirs
diff --git a/system/sepolicy/private/compat/28.0/28.0.ignore.cil b/system/sepolicy/private/compat/28.0/28.0.ignore.cil
index d24d12d257..f79ee064fe 100755
--- a/system/sepolicy/private/compat/28.0/28.0.ignore.cil
+++ b/system/sepolicy/private/compat/28.0/28.0.ignore.cil
@@ -153,6 +153,7 @@
     vendor_misc_writer
     vendor_misc_writer_exec
     vendor_socket_hook_prop
+    test_service
     vendor_task_profiles_file
     vndk_prop
     vrflinger_vsync_service
diff --git a/system/sepolicy/private/compat/29.0/29.0.ignore.cil b/system/sepolicy/private/compat/29.0/29.0.ignore.cil
index e4aaef5f48..3e9130758c 100755
--- a/system/sepolicy/private/compat/29.0/29.0.ignore.cil
+++ b/system/sepolicy/private/compat/29.0/29.0.ignore.cil
@@ -129,4 +129,5 @@
     vendor_service_contexts_file
     vendor_socket_hook_prop
     vendor_socket_hook_prop
+    test_service
     virtual_ab_prop))
diff --git a/system/sepolicy/private/service_contexts b/system/sepolicy/private/service_contexts
index 5c6f1a4766..10d4315e36 100755
--- a/system/sepolicy/private/service_contexts
+++ b/system/sepolicy/private/service_contexts
@@ -150,6 +150,7 @@ network_stack                             u:object_r:network_stack_service:s0
 network_management                        u:object_r:network_management_service:s0
 network_score                             u:object_r:network_score_service:s0
 network_time_update_service               u:object_r:network_time_update_service:s0
+test_service               u:object_r:test_service:s0
 nfc                                       u:object_r:nfc_service:s0
 notification                              u:object_r:notification_service:s0
 oem_lock                                  u:object_r:oem_lock_service:s0
diff --git a/system/sepolicy/public/service.te b/system/sepolicy/public/service.te
index f27772eabb..be4c8fd6ec 100755
--- a/system/sepolicy/public/service.te
+++ b/system/sepolicy/public/service.te
@@ -134,6 +134,7 @@ type netstats_service, app_api_service, ephemeral_app_api_service, system_server
 type network_management_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type network_score_service, system_api_service, system_server_service, service_manager_type;
 type network_stack_service, system_server_service, service_manager_type;
+type test_service, system_server_service, service_manager_type;
 type network_time_update_service, system_server_service, service_manager_type;
 type notification_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type oem_lock_service, system_api_service, system_server_service, service_manager_type;

编译framework.jar给app使用

Android11 全编译后无法生成framework.jar的解决方案_凯文的内存的博客-优快云博客

make javac-check-framework

注册后, 如果你在App里面通过 getSystemService(SystemEventManager.SERVICE); 获取Manager并调用接口, 会发现又会出错, 又是Selinux权限问题:

2023-03-29 10:11:21.511 259-259/? E/SELinux: avc:  denied  { find } for pid=8238 uid=10185 name=test_service scontext=u:r:untrusted_app:s0:c185,c256,c512,c768 tcontext=u:object_r:test_service:s0 tclass=service_manager permissive=0

diff --git a/system/sepolicy/prebuilts/api/30.0/private/untrusted_app.te b/system/sepolicy/prebuilts/api/30.0/private/untrusted_app.te
index 6e7a99cd8c..a3865011d2 100755
--- a/system/sepolicy/prebuilts/api/30.0/private/untrusted_app.te
+++ b/system/sepolicy/prebuilts/api/30.0/private/untrusted_app.te
@@ -14,3 +14,5 @@ app_domain(untrusted_app)
 untrusted_app_domain(untrusted_app)
 net_domain(untrusted_app)
 bluetooth_domain(untrusted_app)
+
+allow untrusted_app test_service:service_manager find;
diff --git a/system/sepolicy/prebuilts/api/30.0/public/untrusted_app.te b/system/sepolicy/prebuilts/api/30.0/public/untrusted_app.te
index 43fe19a034..48b5251cdd 100755
--- a/system/sepolicy/prebuilts/api/30.0/public/untrusted_app.te
+++ b/system/sepolicy/prebuilts/api/30.0/public/untrusted_app.te
@@ -28,3 +28,5 @@ type untrusted_app_27, domain;
 # This file defines the rules for untrusted apps running with
 # targetSdkVersion <= 25.
 type untrusted_app_25, domain;
+
+allow untrusted_app test_service:service_manager find;
diff --git a/system/sepolicy/private/untrusted_app.te b/system/sepolicy/private/untrusted_app.te
index 6e7a99cd8c..a3865011d2 100755
--- a/system/sepolicy/private/untrusted_app.te
+++ b/system/sepolicy/private/untrusted_app.te
@@ -14,3 +14,5 @@ app_domain(untrusted_app)
 untrusted_app_domain(untrusted_app)
 net_domain(untrusted_app)
 bluetooth_domain(untrusted_app)
+
+allow untrusted_app test_service:service_manager find;
diff --git a/system/sepolicy/public/untrusted_app.te b/system/sepolicy/public/untrusted_app.te
index 43fe19a034..48b5251cdd 100755
--- a/system/sepolicy/public/untrusted_app.te
+++ b/system/sepolicy/public/untrusted_app.te
@@ -28,3 +28,5 @@ type untrusted_app_27, domain;
 # This file defines the rules for untrusted apps running with
 # targetSdkVersion <= 25.
 type untrusted_app_25, domain;
+
+allow untrusted_app test_service:service_manager find;

参考Android 11系统服务的添加(java 层) - 简书

参考

添加JNI

TestService.java添加native 函数
public native void testJNI();

frameworks\base\services\core\jni\com_android_server_TestService.cpp

//
// Created by caron_wy on 2023/3/29.
//

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "com_android_server_TestService"
#include "utils/Log.h"

#include "jni.h"
#include <nativehelper/JNIHelp.h>
#include "android_runtime/AndroidRuntime.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

namespace android
{

static void android_server_TestService_testJNI(JNIEnv *env, jobject /* thiz */)
{
        ALOGE("android_server_TestService_testJNI");
}


static const JNINativeMethod method_table[] = {
    { "testJNI","()V",(void*)android_server_TestService_testJNI },
};

int register_android_server_TestService(JNIEnv *env)
{
    jclass clazz = env->FindClass("com/android/server/TestService");
    if (clazz == NULL) {
        ALOGE("Can't find com/android/server/TestService");
        return -1;
    }

    return jniRegisterNativeMethods(env, "com/android/server/TestService",
            method_table, NELEM(method_table));
}


/*JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *jniEnv{nullptr};
    if (vm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6) != JNI_OK) {
        diagnosis_assert(!"JNI version error!");
        return JNI_EVERSION;
    }

    register_android_server_TestService(jniEnv);
    return JNI_VERSION_1_6;
}*/

};

com_android_server_TestService.cpp添加编译

--- a/frameworks/base/services/core/jni/Android.bp
+++ b/frameworks/base/services/core/jni/Android.bp
@@ -46,6 +46,7 @@ cc_library_static {
         "com_android_server_storage_AppFuseBridge.cpp",
         "com_android_server_SystemServer.cpp",
         "com_android_server_TestNetworkService.cpp",
+        "com_android_server_TestService.cpp",
         "com_android_server_tv_TvUinputBridge.cpp",
         "com_android_server_tv_TvInputHal.cpp",
         "com_android_server_vr_VrManagerService.cpp",
diff --git a/frameworks/base/services/core/jni/onload.cpp b/frameworks/base/services/core/jni/onload.cpp
index e5d2a83479..a4485db89c 100755
--- a/frameworks/base/services/core/jni/onload.cpp
+++ b/frameworks/base/services/core/jni/onload.cpp
@@ -42,6 +42,7 @@ int register_android_server_VibratorService(JNIEnv* env);
 int register_android_server_location_GnssLocationProvider(JNIEnv* env);
 int register_android_server_connectivity_Vpn(JNIEnv* env);
 int register_android_server_TestNetworkService(JNIEnv* env);
+int register_android_server_TestService(JNIEnv *env);
 int register_android_server_devicepolicy_CryptoTestHelper(JNIEnv*);
 int register_android_server_hdmi_HdmiCecController(JNIEnv* env);
 int register_android_server_tv_TvUinputBridge(JNIEnv* env);
@@ -96,6 +97,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
     register_android_server_location_GnssLocationProvider(env);
     register_android_server_connectivity_Vpn(env);
     register_android_server_TestNetworkService(env);
+    register_android_server_TestService(env);
     register_android_server_devicepolicy_CryptoTestHelper(env);
     register_android_server_ConsumerIrService(env);
     register_android_server_BatteryStatsService(env);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值