有些系统,因为应用场景的不同,需要的服务也不一样。比如Android Things,为了应对IOT的应用场景,它就裁剪掉了很多服务。下面介绍一下裁剪服务的方法。
关于服务,要提一下SystemServer,具体介绍见另一篇文章:http://wossoneri.github.io。SystemServer启动了系统的核心服务,除此之外,SystemServer还启动了很多其他服务,具体是在startOtherServices()方法中。我们要裁剪不需要的服务就可以从这里入手。
比如要关闭打印机服务:
可以直接把相关启动服务的代码注释掉:
//mSystemServiceManager.startService(PRINT_MANAGER_SERVICE_CLASS);
当然这样修改后,以后如果要再打开,还需要再次修改SystemServer,然后编译jar包,push到设备使其生效。
所以建议使用下面的改法:
首先定义boolean变量,从全局属性读取配置,
boolean disablePrinter = SystemProperties.getBoolean("config.disable_printer", false);
然后在启动服务的代码段添加对这个属性的判断:
if (!disablePrinter && mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
mSystemServiceManager.startService(PRINT_MANAGER_SERVICE_CLASS);
}
之后在MakeFile增加自定义的系统权限:
PRODUCT_PROPERTY_OVERRIDES += \
config.disable_printer=true
以后如果要打开这个服务,就把true变成false就可以了。
如果要调试,从修改设备的 /system/build.prop
然后重启即可。非常方便有木有!
如果要修改,删掉out目录下的build.prop,重新编译system(或者直接修改build.prop然后make snod),烧录启动系统之后,运行如下命令进行验证:
service check printer
这样就不会再启动不需要的服务了。
裁剪服务引发的问题
服务不是你不让它Start就完事儿了,系统那么大,总有一些地方会获取服务对象做一些调用处理。比如我们刚裁减掉了打印机服务,然后打开Settings就遇到了crash:
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.android.settings, PID: 3496
E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.Settings}: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.List android.print.IPrintManager.getPrintServices(int, int)' on a null object reference
E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
E AndroidRuntime: at android.app.ActivityThread.-wrap12(ActivityThread.java)
E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6119)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:790)
E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.List android.print.IPrintManager.getPrintServices(int, int)' on a null object reference
E AndroidRuntime: at android.print.PrintManager.getPrintServices(PrintManager.java:635)
E AndroidRuntime: at android.print.PrintServicesLoader.onStartLoading(PrintServicesLoader.java:88)
E AndroidRuntime: at android.content.Loader.startLoading(Loader.java:290)
E AndroidRuntime: at android.app.LoaderManagerImpl$LoaderInfo.start(LoaderManager.java:283)
E AndroidRuntime: at android.app.LoaderManagerImpl.installLoader(LoaderManager.java:579)
E AndroidRuntime: at android.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:566)
E AndroidRuntime: at android.app.LoaderManagerImpl.initLoader(LoaderManager.java:619)
E AndroidRuntime: at com.android.settings.search.DynamicIndexableContentMonitor.register(DynamicIndexableContentMonitor.java:136)
E AndroidRuntime: at com.android.settings.SettingsActivity.onStart(SettingsActivity.java:868)
E AndroidRuntime: at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
E AndroidRuntime: at android.app.Activity.performStart(Activity.java:6737)
E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
E AndroidRuntime: ... 9 more
通过堆栈信息,可以知道PrintManager.getPrintServices出现了空指针。这里也不用看代码就能猜到,因为我们开机没有启动打印服务,所以肯定get不到这个服务的。
然后考虑修改方案,增加非空保护是不是就可以了?Naive!我们的目的是裁剪打印服务,所以我们的修改点并不在这个服务本身,而是删除所以调用这个服务的地方。
所以堆栈显示的PrintManager,PrintServicesLoader什么的统统不要改,我们要看代码结构,看看是怎么调用进来的。通过阅读代码,了解到系统里有很多Loader类型的对象,其中一个子类就是PrintServicesLoader。然后这些Loader是由LoaderManager管理启动的。而LoaderManager在DynamicIndexableContentMonitor.java出现过一次初始化操作。
看下DynamicIndexableContentMonitor.java代码:
public void register(Activity activity, int loaderId) {
...
boolean hasFeaturePrinting = mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING);
...
if (hasFeaturePrinting) {
activity.getLoaderManager().initLoader(loaderId, null, this);
}
...
有木有发现一个熟悉的代码?
对,代码里再次出现了一个有关SystemFeature的判断!上一次出现时SystemServer启动服务前也做了相同的判断。
所以要裁剪掉打印机服务,我们只需要将FEATURE_PRINTING关闭即可。
通过修改SystemFeature判断后,在SystemServer里面的裁剪代码就可以不再添加了。但是有些服务的裁剪Android并没有添加系统特性的处理,所以还是建议使用我的方法进行裁剪。
SystemFeature加载流程
先看一看FEATURE_PRINTING
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#008000">/**
* Feature for {<span style="color:#808080">@link</span> #getSystemAvailableFeatures} and {<span style="color:#808080">@link</span> #hasSystemFeature}:
* The device supports printing.
*/</span>
<span style="color:#2b91af">@SdkConstant(SdkConstantType.FEATURE)</span>
<span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">final</span> <span style="color:#a31515">String</span> <span style="color:#008000">FEATURE_PRINTING</span> <span style="color:#ab5656">=</span> <span style="color:#a31515">"android.software.print"</span>;
<span style="color:#008000">/**
* Get a list of features that are available on the
* system.
*
* <span style="color:#808080">@return</span> An array of FeatureInfo classes describing the features
* that are available on the system, or null if there are none(!!).
*/</span>
<span style="color:#0000ff">public</span> <span style="color:#0000ff">abstract</span> FeatureInfo[] getSystemAvailableFeatures();
<span style="color:#008000">/**
* Check whether the given feature name is one of the available features as
* returned by {<span style="color:#808080">@link</span> #getSystemAvailableFeatures()}. This tests for the
* presence of <em>any</em> version of the given feature name; use
* {<span style="color:#808080">@link</span> #hasSystemFeature(String, int)} to check for a minimum version.
*
* <span style="color:#808080">@return</span> Returns true if the devices supports the feature, else false.
*/</span>
<span style="color:#0000ff">public</span> <span style="color:#0000ff">abstract</span> <span style="color:#a31515">boolean</span> <span style="color:#a31515">hasSystemFeature</span>(String name);
<span style="color:#008000">/**
* Check whether the given feature name and version is one of the available
* features as returned by {<span style="color:#808080">@link</span> #getSystemAvailableFeatures()}. Since
* features are defined to always be backwards compatible, this returns true
* if the available feature version is greater than or equal to the
* requested version.
*
* <span style="color:#808080">@return</span> Returns true if the devices supports the feature, else false.
*/</span>
<span style="color:#0000ff">public</span> <span style="color:#0000ff">abstract</span> <span style="color:#a31515">boolean</span> <span style="color:#a31515">hasSystemFeature</span>(String name, <span style="color:#a31515">int</span> version);
</code></span></span>
都是抽象方法,我们去PMS查找对应的实现
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#0000ff">public</span> <span style="color:#2b91af">@NonNull</span> ParceledListSlice<FeatureInfo> <span style="color:#a31515">getSystemAvailableFeatures</span>() {
<span style="color:#0000ff">synchronized</span> (mPackages) {
<span style="color:#0000ff">final</span> ArrayList<FeatureInfo> res = <span style="color:#0000ff">new</span> <span style="color:#a31515">ArrayList</span><>(mAvailableFeatures.values());
<span style="color:#0000ff">final</span> <span style="color:#a31515">FeatureInfo</span> <span style="color:#008000">fi</span> <span style="color:#ab5656">=</span> <span style="color:#0000ff">new</span> <span style="color:#a31515">FeatureInfo</span>();
fi.reqGlEsVersion = SystemProperties.getInt(<span style="color:#a31515">"ro.opengles.version"</span>,
FeatureInfo.GL_ES_VERSION_UNDEFINED);
res.add(fi);
<span style="color:#0000ff">return</span> <span style="color:#0000ff">new</span> <span style="color:#a31515">ParceledListSlice</span><>(res);
}
}
<span style="color:#2b91af">@Override</span>
<span style="color:#0000ff">public</span> <span style="color:#a31515">boolean</span> <span style="color:#a31515">hasSystemFeature</span>(String name, <span style="color:#a31515">int</span> version) {
<span style="color:#0000ff">synchronized</span> (mPackages) {
<span style="color:#0000ff">final</span> <span style="color:#a31515">FeatureInfo</span> <span style="color:#008000">feat</span> <span style="color:#ab5656">=</span> mAvailableFeatures.get(name);
<span style="color:#0000ff">if</span> (feat == <span style="color:#a31515">null</span>) {
<span style="color:#0000ff">return</span> <span style="color:#a31515">false</span>;
} <span style="color:#0000ff">else</span> {
<span style="color:#0000ff">return</span> feat.version >= version;
}
}
}
</code></span></span>
这里的逻辑都是通过mAvailableFeatures得到所有的feature,查找该成员变量的相关代码
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#0000ff">final</span> ArrayMap<String, FeatureInfo> mAvailableFeatures;
<span style="color:#a31515">SystemConfig</span> <span style="color:#008000">systemConfig</span> <span style="color:#ab5656">=</span> SystemConfig.getInstance();
mGlobalGids = systemConfig.getGlobalGids();
mSystemPermissions = systemConfig.getSystemPermissions();
mAvailableFeatures = systemConfig.getAvailableFeatures();
</code></span></span>
了解到,首先获取一个SystemConfig的单例,然后通过getAvailableFeatures方法获取可用的feature。
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#008000">// These are the features this devices supports that were read from the</span>
<span style="color:#008000">// system configuration files.</span>
<span style="color:#0000ff">final</span> ArrayMap<String, FeatureInfo> mAvailableFeatures = <span style="color:#0000ff">new</span> <span style="color:#a31515">ArrayMap</span><>();
<span style="color:#0000ff">public</span> ArrayMap<String, FeatureInfo> <span style="color:#a31515">getAvailableFeatures</span>() {
<span style="color:#0000ff">return</span> mAvailableFeatures;
}
<span style="color:#0000ff">private</span> <span style="color:#0000ff">void</span> <span style="color:#a31515">addFeature</span>(String name, <span style="color:#a31515">int</span> version) {
<span style="color:#a31515">FeatureInfo</span> <span style="color:#008000">fi</span> <span style="color:#ab5656">=</span> mAvailableFeatures.get(name);
<span style="color:#0000ff">if</span> (fi == <span style="color:#a31515">null</span>) {
fi = <span style="color:#0000ff">new</span> <span style="color:#a31515">FeatureInfo</span>();
fi.name = name;
fi.version = version;
mAvailableFeatures.put(name, fi);
} <span style="color:#0000ff">else</span> {
fi.version = Math.max(fi.version, version);
}
}
<span style="color:#0000ff">private</span> <span style="color:#0000ff">void</span> <span style="color:#a31515">removeFeature</span>(String name) {
<span style="color:#0000ff">if</span> (mAvailableFeatures.remove(name) != <span style="color:#a31515">null</span>) {
Slog.d(TAG, <span style="color:#a31515">"Removed unavailable feature "</span> + name);
}
}
</code></span></span>
根据mAvailableFeatures的注释,设备支持的feature是从配置文件里读取出来的。调用读取配置文件的地方是:
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-java">SystemConfig() {
<span style="color:#008000">// Read configuration from system</span>
readPermissions(Environment.buildPath(
Environment.getRootDirectory(), <span style="color:#a31515">"etc"</span>, <span style="color:#a31515">"sysconfig"</span>), ALLOW_ALL);
<span style="color:#008000">// Read configuration from the old permissions dir</span>
readPermissions(Environment.buildPath(
Environment.getRootDirectory(), <span style="color:#a31515">"etc"</span>, <span style="color:#a31515">"permissions"</span>), ALLOW_ALL);
<span style="color:#008000">// Allow ODM to customize system configs around libs, features and apps</span>
<span style="color:#a31515">int</span> <span style="color:#008000">odmPermissionFlag</span> <span style="color:#ab5656">=</span> ALLOW_LIBS | ALLOW_FEATURES | ALLOW_APP_CONFIGS;
readPermissions(Environment.buildPath(
Environment.getOdmDirectory(), <span style="color:#a31515">"etc"</span>, <span style="color:#a31515">"sysconfig"</span>), odmPermissionFlag);
readPermissions(Environment.buildPath(
Environment.getOdmDirectory(), <span style="color:#a31515">"etc"</span>, <span style="color:#a31515">"permissions"</span>), odmPermissionFlag);
<span style="color:#008000">// Only allow OEM to customize features</span>
readPermissions(Environment.buildPath(
Environment.getOemDirectory(), <span style="color:#a31515">"etc"</span>, <span style="color:#a31515">"sysconfig"</span>), ALLOW_FEATURES);
readPermissions(Environment.buildPath(
Environment.getOemDirectory(), <span style="color:#a31515">"etc"</span>, <span style="color:#a31515">"permissions"</span>), ALLOW_FEATURES);
}
</code></span></span>
到此就很明白了,它是读取了几个目录:
- /system/etc/permission
- /system/etc/sysconfig
- /oem/etc/permission
- /oem/etc/sysconfig
- /odm/etc/permission
- /odm/etc/sysconfig
然后遍历xml文件,进行解析处理。SystemFeature就是解析的Feature标签。
屏蔽SystemFeature
查找含有<feature name=“android.software.print” /> 的文件,
通过aosp/frameworks/base/core/java/com/android/server/SystemConfig.java的readAllPermissions方法可知,会在以下文件夹获取打印特性配置文件。
/system/etc/permission
/system/etc/sysconfig
/vendor/etc/permission
/vendor/etc/sysconfig
/oem/etc/permission
/oem/etc/sysconfig
/odm/etc/permission
/odm/etc/sysconfig
/product/etc/permission
/product/etc/sysconfig
/system_ext/etc/permission
/system_ext/etc/sysconfig
查找具体位置,是/vendor/etc/permissions/handheld_core_hardware.xml文件中设置
cd /vendor/etc
find ./ -type f -exec grep -rn "software.print" {} \;
再查找源文件,发现是在frameworks/native/data/etc/handheld_core_hardware.xml文件中,
注释掉 <feature name=“android.software.print” />
find ./ -name "*.mk" -exec grep -rn "handheld_core_hardware" {} \;
知道原理就好做了,在系统扫描的几个目录中使用grep命令查找控制打印机的字串,找到:
/system/etc/permission/handheld_core_hardware.xml
<span style="color:#000000"><span style="background-color:#ffffff"><code class="language-xml"> <span style="color:#008000"><!-- basic system services --></span>
<span style="color:#0000ff"><<span style="color:#0000ff">feature</span> <span style="color:#ff0000">name</span>=<span style="color:#a31515">"android.software.app_widgets"</span> /></span>
<span style="color:#0000ff"><<span style="color:#0000ff">feature</span> <span style="color:#ff0000">name</span>=<span style="color:#a31515">"android.software.connectionservice"</span> /></span>
<span style="color:#0000ff"><<span style="color:#0000ff">feature</span> <span style="color:#ff0000">name</span>=<span style="color:#a31515">"android.software.voice_recognizers"</span> <span style="color:#ff0000">notLowRam</span>=<span style="color:#a31515">"true"</span> /></span>
<span style="color:#0000ff"><<span style="color:#0000ff">feature</span> <span style="color:#ff0000">name</span>=<span style="color:#a31515">"android.software.backup"</span> /></span>
<span style="color:#0000ff"><<span style="color:#0000ff">feature</span> <span style="color:#ff0000">name</span>=<span style="color:#a31515">"android.software.home_screen"</span> /></span>
<span style="color:#0000ff"><<span style="color:#0000ff">feature</span> <span style="color:#ff0000">name</span>=<span style="color:#a31515">"android.software.input_methods"</span> /></span>
<span style="color:#0000ff"><<span style="color:#0000ff">feature</span> <span style="color:#ff0000">name</span>=<span style="color:#a31515">"android.software.print"</span> /></span> <------这个就是打印特性
</code></span></span>
将其注释掉就可以在手机进行测试了。
找到该源码文件,将不要的Feature注释掉,然后重新编译源码,启动系统,一切正常!打印机相关的服务彻底被屏蔽掉了,系统启动速度,资源消耗又变小了一点点。嗯,是很小的一点点,我们还可以把VR,红外线等等很多服务裁剪掉,以适应不同应用场景下的精简系统。