有時候因為系統應用場景不同,需要的功能也不一樣。所以就會有很多不需要的服務或者進程存在,因此將這些不需要的東西裁減掉也是必須面對的問題。本文就是對裁剪服務部分的一些簡單整理。 ...
本文鏈接 http://wossoneri.github.io/2018/08/30/[Android][Framework]crop-SystemServer-and-SystemFeature/
SystemServer服務裁剪
有些系統,因為應用場景的不同,需要的服務也不一樣。比如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
/**
* Feature for {@link #getSystemAvailableFeatures} and {@link #hasSystemFeature}:
* The device supports printing.
*/
@SdkConstant(SdkConstantType.FEATURE)
public static final String FEATURE_PRINTING = "android.software.print";
/**
* Get a list of features that are available on the
* system.
*
* @return An array of FeatureInfo classes describing the features
* that are available on the system, or null if there are none(!!).
*/
public abstract FeatureInfo[] getSystemAvailableFeatures();
/**
* Check whether the given feature name is one of the available features as
* returned by {@link #getSystemAvailableFeatures()}. This tests for the
* presence of <em>any</em> version of the given feature name; use
* {@link #hasSystemFeature(String, int)} to check for a minimum version.
*
* @return Returns true if the devices supports the feature, else false.
*/
public abstract boolean hasSystemFeature(String name);
/**
* Check whether the given feature name and version is one of the available
* features as returned by {@link #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.
*
* @return Returns true if the devices supports the feature, else false.
*/
public abstract boolean hasSystemFeature(String name, int version);
都是抽象方法,我們去PMS查找對應的實現
public @NonNull ParceledListSlice<FeatureInfo> getSystemAvailableFeatures() {
synchronized (mPackages) {
final ArrayList<FeatureInfo> res = new ArrayList<>(mAvailableFeatures.values());
final FeatureInfo fi = new FeatureInfo();
fi.reqGlEsVersion = SystemProperties.getInt("ro.opengles.version",
FeatureInfo.GL_ES_VERSION_UNDEFINED);
res.add(fi);
return new ParceledListSlice<>(res);
}
}
@Override
public boolean hasSystemFeature(String name, int version) {
synchronized (mPackages) {
final FeatureInfo feat = mAvailableFeatures.get(name);
if (feat == null) {
return false;
} else {
return feat.version >= version;
}
}
}
這裡的邏輯都是通過mAvailableFeatures得到所有的feature,查找該成員變數的相關代碼
final ArrayMap<String, FeatureInfo> mAvailableFeatures;
SystemConfig systemConfig = SystemConfig.getInstance();
mGlobalGids = systemConfig.getGlobalGids();
mSystemPermissions = systemConfig.getSystemPermissions();
mAvailableFeatures = systemConfig.getAvailableFeatures();
瞭解到,首先獲取一個SystemConfig的單例,然後通過getAvailableFeatures方法獲取可用的feature。
// These are the features this devices supports that were read from the
// system configuration files.
final ArrayMap<String, FeatureInfo> mAvailableFeatures = new ArrayMap<>();
public ArrayMap<String, FeatureInfo> getAvailableFeatures() {
return mAvailableFeatures;
}
private void addFeature(String name, int version) {
FeatureInfo fi = mAvailableFeatures.get(name);
if (fi == null) {
fi = new FeatureInfo();
fi.name = name;
fi.version = version;
mAvailableFeatures.put(name, fi);
} else {
fi.version = Math.max(fi.version, version);
}
}
private void removeFeature(String name) {
if (mAvailableFeatures.remove(name) != null) {
Slog.d(TAG, "Removed unavailable feature " + name);
}
}
根據mAvailableFeatures的註釋,設備支持的feature是從配置文件里讀取出來的。調用讀取配置文件的地方是:
SystemConfig() {
// Read configuration from system
readPermissions(Environment.buildPath(
Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);
// Read configuration from the old permissions dir
readPermissions(Environment.buildPath(
Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);
// Allow ODM to customize system configs around libs, features and apps
int odmPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_APP_CONFIGS;
readPermissions(Environment.buildPath(
Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);
readPermissions(Environment.buildPath(
Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);
// Only allow OEM to customize features
readPermissions(Environment.buildPath(
Environment.getOemDirectory(), "etc", "sysconfig"), ALLOW_FEATURES);
readPermissions(Environment.buildPath(
Environment.getOemDirectory(), "etc", "permissions"), ALLOW_FEATURES);
}
到此就很明白了,它是讀取了幾個目錄:
- /system/etc/permission
- /system/etc/sysconfig
- /oem/etc/permission
- /oem/etc/sysconfig
- /odm/etc/permission
- /odm/etc/sysconfig
然後遍歷xml文件,進行解析處理。SystemFeature就是解析的Feature標簽。
最後再總結一下載入流程:
屏蔽SystemFeature
知道原理就好做了,在系統掃描的幾個目錄中使用grep命令查找控制印表機的字串,找到:
/system/etc/permission/handheld_core_hardware.xml
<!-- basic system services -->
<feature name="android.software.app_widgets" />
<feature name="android.software.connectionservice" />
<feature name="android.software.voice_recognizers" notLowRam="true" />
<feature name="android.software.backup" />
<feature name="android.software.home_screen" />
<feature name="android.software.input_methods" />
<feature name="android.software.print" /> <------這個就是列印特性
將其註釋掉就可以在手機進行測試了。
但是,我們還需要修改源碼,保證以後編譯系統這個值都是被屏蔽的。
查找MakeFile,找到如下:
PRODUCT_COPY_FILES := \ frameworks/native/data/etc/handheld_core_hardware.xml:system/etc/permissions/handheld_core_hardware.xml
這個文件在源碼中的位置是frameworks/native/data/etc/
。找到該源碼文件,將不要的Feature註釋掉,然後重新編譯源碼,啟動系統,一切正常!印表機相關的服務徹底被屏蔽掉了,系統啟動速度,資源消耗又變小了一點點。嗯,是很小的一點點,我們還可以把VR,紅外線等等很多服務裁剪掉,以適應不同應用場景下的精簡系統。