Android 12 (S) 新加系统服务

1.创建AIDL文件

path:frameworks/base/core/java/android/app/testmanager/ITestManager.aidl


package android.app.testmanager;

interface ITestManager{
        String getTestMsg();
        void setTestMsg(in String msg);

}

2.Context.java添加服务名称

diff --git a/frameworks/base/core/java/android/content/Context.java b/frameworks/base/core/java/android/content/Context.java
index c3ec094..5a3889c 100644
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -3711,6 +3711,7 @@ public abstract class Context {
             //@hide: SPEECH_RECOGNITION_SERVICE,
             UWB_SERVICE,
             MEDIA_METRICS_SERVICE,
+            TESTMANAGER_SERVICE,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ServiceName {}
@@ -4051,6 +4052,19 @@ public abstract class Context {
      */
     public static final String ALARM_SERVICE = "alarm";
 
+
+    
+    /**
+     * Use with {@link #getSystemService(String)} to retrieve a
+     * {@link android.app.testnmanager.TestManager} for receiving intents at a
+     * time of your choosing.
+     *
+     * @see #getSystemService(String)
+     * @see android.app.testnmanager.TestManager
+     */
+    public static final String TESTMANAGER_SERVICE = "testmanager";
+
+
     /**
      * Use with {@link #getSystemService(String)} to retrieve a
      * {@link android.app.NotificationManager} for informing the user of

3.新建TestManager.java和TestManagerService.java

path:frameworks/base/services/core/java/com/android/server/TestManagerService.java


package com.android.server;


import android.app.testmanager.ITestManager;
import android.os.RemoteException;



public class TestManagerService extends ITestManager.Stub {
        static final String TAG = "TestManagerService";
        private String message="test";

        @Override
        public String getTestMsg() throws RemoteException {
                return message;
        }

        @Override
        public void setTestMsg(String msg) throws RemoteException{
                message=msg;
        }



}
path:frameworks/base/core/java/android/app/testmanager/TestManager.java


package android.app.testmanager;

import android.annotation.SystemService;
import android.compat.annotation.UnsupportedAppUsage;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.testmanager.ITestManager;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ServiceManager.ServiceNotFoundException;
import android.util.Singleton;
import android.util.Log;



@SystemService(Context.TESTMANAGER_SERVICE)
public class TestManager {
    private static String TAG = "TestManager";
    private ITestManager mService;
    private static TestManager sInstance;



    /**
    *@hide
    */
    public  TestManager(ITestManager service){
        mService=service;
        
    }
    
    
    /**
    *@hide
    */
    @NonNull
    @UnsupportedAppUsage
    public static TestManager getInstance() {
        synchronized (TestManager.class) {
        if (sInstance == null) {

            try {
                IBinder b = ServiceManager.getServiceOrThrow(Context.TESTMANAGER_SERVICE);
                sInstance = new TestManager(ITestManager.Stub
                        .asInterface(ServiceManager.getServiceOrThrow(Context.TESTMANAGER_SERVICE)));
            } catch (ServiceNotFoundException e) {
                throw new IllegalStateException(e);
            }

        }
        return sInstance;
    }
    }

    public String getTestMsg(){
        try{
           return mService.getTestMsg();
        } catch (RemoteException e){
           throw e.rethrowFromSystemServer();
        }
    
    }

     public void setTestMsg(String msg){
                try{
                   mService.setTestMsg(msg);
                } catch (RemoteException e){
                   throw e.rethrowFromSystemServer();
                }
        
        }

    
}     

添加服务到ServiceManager中

path:frameworks/base/services/java/com/android/server/SystemServer.java


private void startOtherServices(){
    ...
    t.traceBegin("StartTestManagerService");
    try {
         ServiceManager.addService(Context.TESTMANAGER_SERVICE,
                 new TestManagerService());
    } catch (Throwable e) {
        Slog.e(TAG, "Failure starting TestManagerService", e);
    }
    t.traceEnd();
    ...

}

将服务添加到ServiceManager中,后续可以根据服务名字获取到这个服务

注册服务

path:frameworks/base/core/java/android/app/SystemServiceRegistry.java

diff --git a/frameworks/base/core/java/android/app/SystemServiceRegistry.java b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
index 1202811..6536c70 100644
--- a/frameworks/base/core/java/android/app/SystemServiceRegistry.java
+++ b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
@@ -33,6 +33,8 @@ import android.app.people.PeopleManager;
 import android.app.prediction.AppPredictionManager;
+import android.app.testmanager.TestManager;
 import android.app.role.RoleFrameworkInitializer;
 import android.app.search.SearchUiManager;
 import android.app.slice.SliceManager;
@@ -329,6 +331,16 @@ public final class SystemServiceRegistry {
        ...
+
+       registerService(Context.TESTMANAGER_SERVICE, TestManager.class,
+                new CachedServiceFetcher<TestManager>() {
+            @Override
+            public TestManager createService(ContextImpl ctx) throws ServiceNotFoundException {
+                return TestManager.getInstance();
+            }});
+
         
         registerService(Context.AUDIO_SERVICE, AudioManager.class,
                 new CachedServiceFetcher<AudioManager>() {

Selinux 权限添加

diff --git a/system/sepolicy/prebuilts/api/32.0/private/service_contexts b/system/sepolicy/prebuilts/api/32.0/private/service_contexts
index 7b0f7c9..6fc2770 100644
--- a/system/sepolicy/prebuilts/api/32.0/private/service_contexts
+++ b/system/sepolicy/prebuilts/api/32.0/private/service_contexts
@@ -267,6 +267,7 @@ task                                      u:object_r:task_service:s0
 telecom                                   u:object_r:telecom_service:s0
 telephony.registry                        u:object_r:registry_service:s0
 telephony_ims                             u:object_r:radio_service:s0
+testmanager                               u:object_r:test_manager_service:s0
 testharness                               u:object_r:testharness_service:s0
 tethering                                 u:object_r:tethering_service:s0
 textclassification                        u:object_r:textclassification_service:s0
diff --git a/system/sepolicy/prebuilts/api/32.0/public/service.te b/system/sepolicy/prebuilts/api/32.0/public/service.te
index 3591867..393c70d 100644
--- a/system/sepolicy/prebuilts/api/32.0/public/service.te
+++ b/system/sepolicy/prebuilts/api/32.0/public/service.te
@@ -210,6 +210,7 @@ type textclassification_service, app_api_service, ephemeral_app_api_service, sys
 type textservices_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type texttospeech_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type telecom_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
+type test_manager_service, app_api_service, system_server_service, service_manager_type;
 type thermal_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type timedetector_service, app_api_service, system_server_service, service_manager_type;
 type timezone_service, system_server_service, service_manager_type;
diff --git a/system/sepolicy/private/service_contexts b/system/sepolicy/private/service_contexts
index 7b0f7c9..6fc2770 100755
--- a/system/sepolicy/private/service_contexts
+++ b/system/sepolicy/private/service_contexts
@@ -267,6 +267,7 @@ task                                      u:object_r:task_service:s0
 telecom                                   u:object_r:telecom_service:s0
 telephony.registry                        u:object_r:registry_service:s0
 telephony_ims                             u:object_r:radio_service:s0
+testmanager                               u:object_r:test_manager_service:s0
 testharness                               u:object_r:testharness_service:s0
 tethering                                 u:object_r:tethering_service:s0
 textclassification                        u:object_r:textclassification_service:s0
diff --git a/system/sepolicy/public/service.te b/system/sepolicy/public/service.te
index 3591867..393c70d 100755
--- a/system/sepolicy/public/service.te
+++ b/system/sepolicy/public/service.te
@@ -210,6 +210,7 @@ type textclassification_service, app_api_service, ephemeral_app_api_service, sys
 type textservices_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type texttospeech_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type telecom_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
+type test_manager_service, app_api_service, system_server_service, service_manager_type;
 type thermal_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
 type timedetector_service, app_api_service, system_server_service, service_manager_type;
 type timezone_service, system_server_service, service_manager_type;

注意需要修改所有的service.te和service_contexts文件,可以参考network_time_update_service 来修改

解决报错

Android 11 以后谷歌强制开启lint检查,lint检查不过编译会报错

  1. frameworks/base/core/java/android/app/testmanager/TestManager.java:58: error: Missing nullability on method getTestMsg return [MissingNullability] lint检查 提示“MissingNullability”

解决方案:加上@Nullable 注解 表明该方法可以返回空

        @Nullable // 加上 @Nullable 表明方法可以返回空
        public String getTestMsg(){
                try{
                   return mService.getTestMsg();
                } catch (RemoteException e){
                   throw e.rethrowFromSystemServer();
                }

        }
        
          // string类型参数 加上@Nullable 表明可以传空参数
        public void setTestMsg(@Nullable String msg){
                try{
                   mService.setTestMsg(msg);
                } catch (RemoteException e){
                   throw e.rethrowFromSystemServer();
                }

      


  1. make update-api报错

out/srcjars/android/app/testmanager/ITestManager.java:165: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:165: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:17: error: Missing nullability on method `asBinder` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:22: error: Raw AIDL interfaces must not be exposed: Stub extends Binder [RawAidl]
out/srcjars/android/app/testmanager/ITestManager.java:30: error: Missing nullability on method `asInterface` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:34: error: Missing nullability on parameter `obj` in method `asInterface` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:45: error: Missing nullability on method `asBinder` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:49: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:49: error: Missing nullability on parameter `data` in method `onTransact` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:49: error: Missing nullability on parameter `reply` in method `onTransact` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:147: error: Missing nullability on parameter `impl` in method `setDefaultImpl` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:160: error: Missing nullability on method `getDefaultImpl` return [MissingNullability]Error: metalava detected the following problems:
out/srcjars/android/app/testmanager/ITestManager.java:165: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:165: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:166: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:10: error: Missing nullability on method `getTestMsg` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Methods calling system APIs should rethrow `RemoteException` as `RuntimeException` (but do not list it in the throws clause) [RethrowRemoteException]
out/srcjars/android/app/testmanager/ITestManager.java:14: error: Missing nullability on parameter `msg` in method `setTestMsg` [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:17: error: Missing nullability on method `asBinder` return [MissingNullability]
out/srcjars/android/app/testmanager/ITestManager.java:22: error: Raw AIDL interfaces must not be exposed: Stub extends Binder [RawAidl]
8 more error(s) omitted. Search the log for 'error:' to find all of them.
************************************************************
Your API changes are triggering API Lint warnings or errors.
To make these errors go away, fix the code according to the
error and/or warning messages above.

If it is not possible to do so, there are workarounds:

1. You can suppress the errors with @SuppressLint("<id>")
2. You can add a baseline file of existing lint failures
   to the build rule of api-stubs-docs-non-updatable.
************************************************************

解决方案:1.按照提示将报错的地方修改正确 该判空判空 等等
2.让lint检查忽略掉自己的模块 在framework/base 下的Android.bp忽略掉代码检查

metalava_framework_docs_args = "
"--api-lint-ignore-prefix android.mymodule. " //其中 android.mymodule是包名的前缀。

3.以上方法对于添加了AIDL以后 out目录自动生成的文件如: out/srcjars/android/app/testmanager/ITestManager.java 不生效
暂时解决方案:
1.cp out/soong/.intermediates/frameworks/base/api-stubs-docs-non-updatable/android_common/metalava/api-stubs-docs-non-updatable_api.txt frameworks/base/core/api/current.txt
2.将current.txt 中新增的部分对比同步添加到prebuilts/sdk/****/public/api/android.txt
此处** 为prebuilts中SDK最新的版本号,如我代码中最新的是32 所以更新最新的目录即可

diff --git a/prebuilts/sdk/32/public/api/android.txt b/prebuilts/sdk/32/public/api/android.txt
old mode 100644
new mode 100755
index e6f796b..4b4be9b
--- a/prebuilts/sdk/32/public/api/android.txt
+++ b/prebuilts/sdk/32/public/api/android.txt
@@ -8870,6 +8870,37 @@ package android.app.slice {
 
 }
 
+package android.app.testmanager {
+
+  public interface ITestManager extends android.os.IInterface {
+    method public String getTestMsg() throws android.os.RemoteException;
+    method public void setTestMsg(String) throws android.os.RemoteException;
+    field public static final String DESCRIPTOR = "android.app.testmanager.ITestManager";
+  }
+
+  public static class ITestManager.Default implements android.app.testmanager.ITestManager {
+    ctor public ITestManager.Default();
+    method public android.os.IBinder asBinder();
+    method public String getTestMsg() throws android.os.RemoteException;
+    method public void setTestMsg(String) throws android.os.RemoteException;
+  }
+
+  public abstract static class ITestManager.Stub extends android.os.Binder implements android.app.testmanager.ITestManager {
+    ctor public ITestManager.Stub();
+    method public android.os.IBinder asBinder();
+    method public static android.app.testmanager.ITestManager asInterface(android.os.IBinder);
+    method public static android.app.testmanager.ITestManager getDefaultImpl();
+    method public boolean onTransact(int, android.os.Parcel, android.os.Parcel, int) throws android.os.RemoteException;
+    method public static boolean setDefaultImpl(android.app.testmanager.ITestManager);
+  }
+
+  public class TestManager {
+    method @Nullable public String getTestMsg();
+    method public void setTestMsg(@Nullable String);
+  }
+
+}
+
 package android.app.usage {
 
   public final class ConfigurationStats implements android.os.Parcelable {
@@ -11288,6 +11319,7 @@ package android.content {
     field public static final String TELEPHONY_IMS_SERVICE = "telephony_ims";
     field public static final String TELEPHONY_SERVICE = "phone";
     field public static final String TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service";
+    field public static final String TESTMANAGER_SERVICE = "testmanager";
     field public static final String TEXT_CLASSIFICATION_SERVICE = "textclassification";
     field public static final String TEXT_SERVICES_MANAGER_SERVICE = "textservices";
     field public static final String TV_INPUT_SERVICE = "tv_input";

总结

经过以上步骤,系统服务就能正常起来了,添加过程中主要问题就是 强制lint检查,导致AIDL自动生成的文件报错,目前通过报错打印:--api-lint ./out/.intermediates/prebuilts/sdk/android.api.public.latest/gen/android.api.public.latest 初步发现是api
会和android.api.public.latest 比对,如果不一致就会报错,可能是谷歌不允许 随便乱添加东西到framework中。 这个文件 发现是根据prebuilts/sdk/****/public/api/android.txt生成的,名字根据路径来取的,所以我目前就直接修改了这个prebuilts 里面的文件 这样比对的样本就和自己添加的一致,lint检查也就能顺利通过
非正规手段,如有更好的办法欢迎留言。
如果想添加 setting.Global key 也是相同 更新一下prebuilts/sdk/**/public/api/android.txt文件 就可正常编过。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容