下面的代码均是基于Android8.1分析。
系统设置--关于手机--版本号
1、在系统设置代码中搜索“版本号”,AndroidStudio中全局搜索快捷键“Ctrl+Shift+f”。
<string name="build_number" msgid="3075795840572241758">"版本号"</string>
全局搜索“build_number”,找到com.android.settings.deviceinfo.BuildNumberPreferenceController类。
private static final String KEY_BUILD_NUMBER = "build_number";
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final Preference preference = screen.findPreference(KEY_BUILD_NUMBER);
if (preference != null) {
try {
preference.setSummary(BidiFormatter.getInstance().unicodeWrap(Build.DISPLAY));
preference.setEnabled(true);
} catch (Exception e) {
preference.setSummary(R.string.device_info_default);
}
}
}
此时关键就是找到Build.DISPLAY。
这里是定义在android.os.Build类中,该类位于frameworks/base/core/java目录下,如下:
/** A build ID string meant for displaying to the user */public static final String DISPLAY = getString("ro.build.display.id");
private static String getString(String property) {
return SystemProperties.get(property, UNKNOWN);
}
我们看到了DISPLAY是Build类中的静态变量,表示“ro.build.display.id”的属性值。
2、此时我们就需要知道“ro.build.display.id”属性值是在哪里定义的。
这里我们在Android源码环境下全局搜索“ro.build.display.id”,命令:grep -r "ro.build.display.id" ./
其实只在build目录下搜索即可,build存放系统编译规则。
在build/make/tools/buildinfo.sh文件中搜索出:
echo "ro.build.display.id=$BUILD_DISPLAY_ID"
我们继续在build目录下,搜索“BUILD_DISPLAY_ID”,命令:grep -r "BUILD_DISPLAY_ID"。在文件build/make/core/MakeFile中搜索出:
# Display parameters shown under Settings -> About Phone
ifeq ($(TARGET_BUILD_VARIANT),user)
# User builds should show:
# release build number or branch.buld_number non-release builds
# Dev. branches should have DISPLAY_BUILD_NUMBER set
ifeq (true,$(DISPLAY_BUILD_NUMBER))
BUILD_DISPLAY_ID := $(BUILD_ID).$(BUILD_NUMBER_FROM_FILE) $(BUILD_KEYS)
else
BUILD_DISPLAY_ID := $(BUILD_ID) $(BUILD_KEYS)
endif
else
# Non-user builds should show detailed build information
BUILD_DISPLAY_ID := $(build_desc)
endif
BUILD_ID 定义在build/make/core/build_id.mk中,如下:
# BUILD_ID is usually used to specify the branch name
# (like "MAIN") or a branch name and a release candidate
# (like "CRB01"). It must be a single word, and is
# capitalized by convention.
export BUILD_ID=OPM1.171019.011
BUILD_NUMBER_FROM_FILE
定义在build/make/core/main.mk中,如下:
BUILD_NUMBER_FROM_FILE := $$(cat $(OUT_DIR)/build_number.txt)
这里OUT_DIR就是out目录, build_number.txt就是在out目录下生成的一个文件,内容如下:
eng.sgf.20180421.152052这里是eng版本。
BUILD_KEYS定义在build/make/core/MakeFile中,表示系统签名,test-keys只适用于开发阶段。如下:
# The "test-keys" tag marks builds signed with the old test keys,
# which are available in the SDK. "dev-keys" marks builds signed with
# non-default dev keys (usually private keys from a vendor directory).
# Both of these tags will be removed and replaced with "release-keys"
# when the target-files is signed in a post-build step.
ifeq ($(DEFAULT_SYSTEM_DEV_CERTIFICATE),build/target/product/security/testkey)
BUILD_KEYS := test-keys
else
BUILD_KEYS := dev-keys
endif
如果不是user版本,BUILD_DISPLAY_ID的取值就是$(build_desc)
# A human-readable string that descibes this build in detail.
build_desc := $(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT) $(PLATFORM_VERSION) $(BUILD_ID) $(BUILD_NUMBER_FROM_FILE) $(BUILD_VERSION_TAGS)
BUILD_NUMBER定义在build/make/core/version_default.mk文件中,如下:
ifndef BUILD_NUMBER
# BUILD_NUMBER should be set to the source control value that
# represents the current state of the source code. E.g., a
# perforce changelist number or a git hash. Can be an arbitrary string
# (to allow for source control that uses something other than numbers),
# but must be a single word and a valid file name.
#
# If no BUILD_NUMBER is set, create a useful "I am an engineering build
# from this date/time" value. Make it start with a non-digit so that
# anyone trying to parse it as an integer will probably get "0".
BUILD_NUMBER := eng.$(shell echo $${USER:0:6}).$(shell $(DATE) +%Y%m%d.%H%M%S)
endif
总结:想要修改版本号,可以通过两个部分修改。
1、修改Java文件,直接写死;
2、修改编译系统,在device中修改。
回贴