在初识Android 属性一文中提到,系统会默认加载以下文件
c
/system/etc/prop.default
/system/build.prop
/system_ext/build.prop
/vendor/default.prop
/vendor/build.prop
/odm/etc/build.prop
/product/build.prop
/factory/factory.prop
要弄清楚我们应该在哪里添加系统属性,就要知道这些文件是怎么生成的。以/system/build.prop文件为例,来分析下其生成过程
属性文件的生成逻辑集中在build\make\core\Makefile文件中
c
# -----------------------------------------------------------------
# build.prop
INSTALLED_BUILD_PROP_TARGET := $(TARGET_OUT)/build.prop
ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_BUILD_PROP_TARGET)
FINAL_BUILD_PROPERTIES := \
$(call collapse-pairs, $(ADDITIONAL_BUILD_PROPERTIES)) //1
FINAL_BUILD_PROPERTIES := $(call uniq-pairs-by-first-component, \
$(FINAL_BUILD_PROPERTIES),=)
//省略
ifdef TARGET_SYSTEM_PROP //2
system_prop_file := $(TARGET_SYSTEM_PROP)
else
system_prop_file := $(wildcard $(TARGET_DEVICE_DIR)/system.prop)
endif
$(intermediate_system_build_prop): $(BUILDINFO_SH) $(BUILDINFO_COMMON_SH) $(INTERNAL_BUILD_ID_MAKEFILE) $(BUILD_SYSTEM)/version_defaults.mk $(system_prop_file) $(INSTALLED_ANDROID_INFO_TXT_TARGET) $(API_FINGERPRINT) $(POST_PROCESS_PROPS) //3
@echo Target buildinfo: $@
@mkdir -p $(dir $@)
$(hide) echo > $@
$(hide) PRODUCT_BRAND="$(PRODUCT_SYSTEM_BRAND)" \
PRODUCT_MANUFACTURER="$(PRODUCT_SYSTEM_MANUFACTURER)" \
PRODUCT_MODEL="$(PRODUCT_SYSTEM_MODEL)" \
PRODUCT_NAME="$(PRODUCT_SYSTEM_NAME)" \
PRODUCT_DEVICE="$(PRODUCT_SYSTEM_DEVICE)" \
$(call generate-common-build-props-with-product-vars-set,system,$@) //4
$(hide) TARGET_BUILD_TYPE="$(TARGET_BUILD_VARIANT)" \
TARGET_BUILD_FLAVOR="$(TARGET_BUILD_FLAVOR)" \
TARGET_DEVICE="$(TARGET_DEVICE)" \
PRODUCT_DEFAULT_LOCALE="$(call get-default-product-locale,$(PRODUCT_LOCALES))" \
PRODUCT_DEFAULT_WIFI_CHANNELS="$(PRODUCT_DEFAULT_WIFI_CHANNELS)" \
PRIVATE_BUILD_DESC="$(PRIVATE_BUILD_DESC)" \
BUILD_ID="$(BUILD_ID)" \
BUILD_DISPLAY_VERSION="$(BUILD_DISPLAY_VERSION)" \
BUILD_DISPLAY_ID="$(BUILD_DISPLAY_ID)" \
DATE="$(DATE_FROM_FILE)" \
BUILD_USERNAME="$(BUILD_USERNAME)" \
BUILD_HOSTNAME="$(BUILD_HOSTNAME)" \
BUILD_NUMBER="$(BUILD_NUMBER_FROM_FILE)" \
BOARD_BUILD_SYSTEM_ROOT_IMAGE="$(BOARD_BUILD_SYSTEM_ROOT_IMAGE)" \
PLATFORM_VERSION="$(PLATFORM_VERSION)" \
PLATFORM_VERSION_LAST_STABLE="$(PLATFORM_VERSION_LAST_STABLE)" \
PLATFORM_SECURITY_PATCH="$(PLATFORM_SECURITY_PATCH)" \
PLATFORM_BASE_OS="$(PLATFORM_BASE_OS)" \
PLATFORM_SDK_VERSION="$(PLATFORM_SDK_VERSION)" \
PLATFORM_PREVIEW_SDK_VERSION="$(PLATFORM_PREVIEW_SDK_VERSION)" \
PLATFORM_PREVIEW_SDK_FINGERPRINT="$$(cat $(API_FINGERPRINT))" \
PLATFORM_VERSION_CODENAME="$(PLATFORM_VERSION_CODENAME)" \
PLATFORM_VERSION_ALL_CODENAMES="$(PLATFORM_VERSION_ALL_CODENAMES)" \
PLATFORM_MIN_SUPPORTED_TARGET_SDK_VERSION="$(PLATFORM_MIN_SUPPORTED_TARGET_SDK_VERSION)" \
BUILD_VERSION_TAGS="$(BUILD_VERSION_TAGS)" \
$(if $(OEM_THUMBPRINT_PROPERTIES),BUILD_THUMBPRINT="$(BUILD_THUMBPRINT_FROM_FILE)") \
TARGET_CPU_ABI_LIST="$(TARGET_CPU_ABI_LIST)" \
TARGET_CPU_ABI_LIST_32_BIT="$(TARGET_CPU_ABI_LIST_32_BIT)" \
TARGET_CPU_ABI_LIST_64_BIT="$(TARGET_CPU_ABI_LIST_64_BIT)" \
TARGET_CPU_ABI="$(TARGET_CPU_ABI)" \
TARGET_CPU_ABI2="$(TARGET_CPU_ABI2)" \
bash $(BUILDINFO_SH) >> $@ //5
$(hide) $(foreach file,$(system_prop_file), \//6
if [ -f "$(file)" ]; then \
echo Target buildinfo from: "$(file)"; \
echo "" >> $@; \
echo "#" >> $@; \
echo "# from $(file)" >> $@; \
echo "#" >> $@; \
cat $(file) >> $@; \
echo "# end of $(file)" >> $@; \
fi;)
$(if $(FINAL_BUILD_PROPERTIES), \ //7
$(hide) echo >> $@; \
echo "#" >> $@; \
echo "# ADDITIONAL_BUILD_PROPERTIES" >> $@; \
echo "#" >> $@; )
$(hide) $(foreach line,$(FINAL_BUILD_PROPERTIES), \
echo "$(line)" >> $@;)
$(hide) $(POST_PROCESS_PROPS) $@ $(PRODUCT_SYSTEM_PROPERTY_BLACKLIST) //8
build_desc :=
INSTALLED_RECOVERYIMAGE_TARGET :=
ifdef BUILDING_RECOVERY_IMAGE
ifneq ($(BOARD_USES_RECOVERY_AS_BOOT),true)
INSTALLED_RECOVERYIMAGE_TARGET := $(PRODUCT_OUT)/recovery.img
endif
endif
$(INSTALLED_BUILD_PROP_TARGET): $(intermediate_system_build_prop)
@echo "Target build info: $@"
$(hide) grep -v 'ro.product.first_api_level' $(intermediate_system_build_prop) > $@ //9
注释1处,将ADDITIONAL_BUILD_PROPERTIES定义的属性赋给FINAL_BUILD_PROPERTIES变量。注释2处,给system_prop_file 变量赋值,如果定义过TARGET_SYSTEM_PROP ,则system_prop_file 的值为TARGET_SYSTEM_PROP 定义的值,否则其值为TARGET_DEVICE_DIR)/system.prop。注释3处创建中间文件,用于生成最终的system.prop文件。注释4处将build/make/tools/buildinfo_common.sh文件中定义的属性写入中间文件 。注释5处,将build/make/tools/buildinfo.sh中定义的属性写入中间文件。注释6处,将前面定义的system_prop_file 中的属性写入中间文件。注释7处,将前面定义的FINAL_BUILD_PROPERTIES定义的属性写入中间文件。注释8处,删除PRODUCT_SYSTEM_PROPERTY_BLACKLIST定义的属性。注释9处,将中间文件写入到system.prop并剔除ro.product.first_api_level属性。
由上可以看出,system/build.prop 文件的属性内容来源于:
- build/make/tools/buildinfo_common.sh文件 (传入的参数是system)
- build/make/tools/buildinfo.sh文件
- system_prop_file:默认路径是TARGET_DEVICE_DIR)/system.prop,也可以自定义文件路径(定义TARGET_SYSTEM_PROP)
- FINAL_BUILD_PROPERTIES:来自ADDITIONAL_BUILD_PROPERTIES
按同样的方法分析
vendor/build.prop文件的属性内容来源于:
- vendor_prop_files:默认路径是TARGET_DEVICE_DIR)/vendor.prop,也可以自定义文件路径(定义TARGET_VENDOR_PROP)
- PRODUCT_PROPERTY_OVERRIDES
- build/make/tools/buildinfo_common.sh文件 (传入的参数是vendor)
product/build.prop文件的属性内容来源于:
- product_prop_files:默认路径是TARGET_DEVICE_DIR)/product.prop,也可以自定义文件路径(定义TARGET_PRODUCT_PROP)
- PRODUCT_PRODUCT_PROPERTIES
- ADDITIONAL_PRODUCT_PROPERTIES
- build/make/tools/buildinfo_common.sh文件 (传入的参数是product)
- 如果定义了PRODUCT_OEM_PROPERTIES,则将其prop文件导入进来
odm/etc/build.prop文件的属性内容来源于:
- odm_prop_files:默认路径是TARGET_DEVICE_DIR)/odm.prop,也可以自定义文件路径(定义TARGET_ODM_PROP)
- PRODUCT_ODM_PROPERTIES
- build/make/tools/buildinfo_common.sh文件 (传入的参数是odm)
知道了各个属性文件的的内容来自哪里,添加自定义的系统属性就比较简单了。比如如果我们想将自定义的属性写入到system/build.prop文件中,则可以有以下两种方法
1,新建属性文件xxx.prop并定义TARGET_SYSTEM_PROP,把属性写到xxx.prop里面
c
TARGET_SYSTEM_PROP += device/xxx/xxx/xxx.prop
2,在mk中定义ADDITIONAL_BUILD_PROPERTIES
c
ADDITIONAL_BUILD_PROPERTIES += my.test=a