今天新增一个JNI的时候遇到一个调用用addService的时候发生异常的问题:
首先是在SystemService.java添加一个自己定义的binder service,代码如下:
try {
Slog.i(TAG, "Xxx Service");
ServiceManager.addService("xxx", new XxxService());
} catch (Throwable e) {
reportWtf("starting Xxx Service", e);
}
结果发现执行到这的时候发生异常: SystemServer: BOOT FAILURE starting Xxx Service。
查看系统log发现:
…………
01-03 02:26:15.040 3907 3907 I SystemServiceManager: Starting com.android.server.midi.MidiService$Lifecycle
01-03 02:26:15.054 3907 3907 I SystemServiceManager: Starting com.android.server.usb.UsbService$Lifecycle
01-03 02:26:15.171 3907 3907 I SystemServiceManager: Starting com.android.server.twilight.TwilightService
01-03 02:26:15.201 3907 3907 I SystemServiceManager: Starting com.android.server.job.JobSchedulerService
01-03 02:26:15.249 3907 3907 I SystemServiceManager: Starting com.android.server.backup.BackupManagerService$Lifecycle
01-03 02:26:15.327 3907 3907 I SystemServiceManager: Starting com.android.server.appwidget.AppWidgetService
01-03 02:26:15.422 3907 3907 I SystemServiceManager: Starting com.android.server.GestureLauncherService
01-03 02:26:15.433 261 261 E ServiceManager: add_service('xxx',54) uid=1000 - PERMISSION DENIED
01-03 02:26:15.436 3907 3907 E SystemServer: at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:150)
01-03 02:26:15.436 3907 3907 E SystemServer: at android.os.ServiceManager.addService(ServiceManager.java:72)
01-03 02:26:15.485 3907 3907 I SystemServiceManager: Starting com.android.server.dreams.DreamManagerService
01-03 02:26:15.504 3907 3907 I SystemServiceManager: Starting com.android.server.print.PrintManagerService
01-03 02:26:15.517 3907 3907 I SystemServiceManager: Starting com.android.server.restrictions.RestrictionsManagerService
01-03 02:26:15.523 3907 3907 I SystemServiceManager: Starting com.android.server.media.MediaSessionService
01-03 02:26:15.558 3907 3907 I SystemServiceManager: Starting com.android.server.trust.TrustManagerService
01-03 02:26:15.581 3907 3907 I SystemServiceManager: Starting com.android.server.fingerprint.FingerprintService
01-03 02:26:15.624 3907 3907 I SystemServiceManager: Starting com.android.server.pm.LauncherAppsService
01-03 02:26:15.632 3907 3907 I SystemServiceManager: Starting com.android.server.media.projection.MediaProjectionManagerService
01-03 02:26:15.687 3907 3907 I SystemServiceManager: Starting com.android.server.MmsServiceBroker
………………
解决方案:
1. 在/external/sepolicy/service.te中仿写添加:
type xxx_service, system_api_service, system_server_service, service_manager_type;2. 在
/external/sepolicy/service_contexts下仿写添加:
xxx u:object_r:xxx_service:s0
添加完成编译,问题便可解决。