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检查不过编译会报错
- 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();
}
- 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文件 就可正常编过。