靜態與動態apk overlay 機制
靜態Overlay 只對有原始碼的apk起作用,可以修改與增加一般apk資源,對frameworks 資源只能修改,無法新增。
1. Android平臺Overlay機制
Android overlay 機制允許在不修改packages中apk的情況下,來自定義 framework和package中的資原始檔,實現資源的定製。來達到顯示不同的UI得目的(如MIUI)。
以下幾類能夠通過該機制定義:
(1),Configurations (string, bool, bool-array)
(2),Localization (string, string-array)
(3),UI Appearance (color, drawable, layout, style, theme, animation)
(4),Raw resources (audio, video, xml)
2. 為產品新增overlay目錄
有兩種不同的overaly目錄定義,來影響最終的效果:
PRODUCT_PACKAGE_OVERLAYS: 用於特定的產品
DEVICE_PACKAGE_OVERLAYS: 多種產品共用
如果這兩個目錄下包含同一資源,那麼 PRODUCT_PACKAGE_OVERLAYS 將覆蓋 DEVICE_PACKAGE_OVERLAYS 中的
1. 以Settings apk為例子
Android.mk 中資源目錄為
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res \
frameworks/support/v7/preference/res \
frameworks/support/v14/preference/res \
frameworks/support/v7/appcompat/res \
frameworks/support/v7/recyclerview/res
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/res_ext
所以LOCAL_RESOURCE_DIR 下的內容均可以定製。
現在要定製一下佈局
packages\apps\Settings\res\drawable\appwidget_button_center.xml
1:建立overlay 目錄
vendor/transsion/device/S31N/overlay/packages/apps/Settings/res/drawable
2:將對應定製佈局appwidget_button_center.xml放入對應overlay目錄即可
現在要定製packages\apps\Settings\res\values\strings.xml 下字串
將<string name="sim_card_2_ringtone">SIM2 ringtone</string> 定製為ovelay SIM2 ringtone
增加<string name="sim_card_2_new">new add</string>
1:建立overlay目錄
vendor/transsion/device/S31N/overlay/packages/apps/Settings/res/values
2:建立strings.xml檔案並新增一下內容
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="sim_card_2_ringtone">"ovelay SIM2 ringtone"</string>
<string name="sim_card_2_new">"new add"</string>
</resources>
動態overlay能夠定製第三方apk
以settings apk 為例子
Android.mk 檔案
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
#include files in res diretory
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_SDK_VERSION = current
LOCAL_CERTIFICATE := platform
LOCAL_PRIVILEGED_MODULE := true
#the name of target apk
LOCAL_PACKAGE_NAME := SettingsOverlay
include $(BUILD_PACKAGE)
AndroidManifest.xml 檔案
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.settings.overlay" >
<overlay
android:priority="1"
android:targetPackage="com.android.settings" />
</manifest>
第三方apk 編譯指令碼
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Module name should match apk name to be installed
LOCAL_MODULE := BoomPlayer
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
#LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := platform
#LOCAL_MODULE_PATH := $(TARGET_OUT)/vendor/operator/app
include $(BUILD_PREBUILT)
在overlay apk下定製,只需要保證 資源id 名字與三方apk 名字一樣即可,並將編譯生成的overlay apk push 到手機/vendor/overlay 或/system//vendor/overlay 下,
LOCAL_CERTIFICATE := platform 簽名需要一致。