O 版本设置界面中内核信息
Q 版本设置界面中内核信息
frameworks\base\packages\SettingsLib\src\com\android\settingslib\DeviceInfoUtils.java
@@ -88,11 +88,15 @@ public class DeviceInfoUtils {
return context.getString(R.string.status_unavailable);
}
+ String user = android.os.SystemProperties.get("ro.build.user", "");
+ String host = android.os.SystemProperties.get("ro.build.host", "");
+
// Example output:
// 4.9.29-g958411d
// #1 Wed Jun 7 00:06:03 CST 2017
return new StringBuilder().append(uname.release)
.append("\n")
+ .append(user + "@" + host)
.append(m.group(1))
.append(" ")
.append(m.group(2)).toString();
内核版本信息对应 /proc/version 文件内容
可以通过adb shell, cat /proc/version 进行查看,我们在系统设置中看到的信息是经过裁剪后的,完整的如下
10.0 对应内核信息
cat /proc/version
Linux version 4.9.190 (nobody@android-build) (Android (5484270 based on r353983c) clang version 9.0.3 (https://android.googlesource.com/toolchain/clang 745b335211bb9eadfa6aa6301f84715cee4b37c5) (https://android.googlesource.com/toolchain/llvm 60cf23e54e46c807513f7a36d0a7b777920b5881) (based on LLVM 9.0.3svn)) #3 SMP PREEMPT Fri Jul 10 17:13:42 CST 2020
8.0 对应内核信息
cat /proc/version
Linux version 3.18.79 (ntu@ubuntu) (gcc version 6.3.1 20170109 (Linaro GCC 6.3-2017.02) ) #1 SMP PREEMPT Sun Jun 28 10:45:30 CST 2020
可以看到10.0 中不包含linux编译用户名信息,我们来跟踪下源码
Setting 中对应内核信息 Preference 获取值的地方
vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\deviceinfo\firmwareversion\KernelVersionPreferenceController.java
import com.android.settingslib.DeviceInfoUtils;
public class KernelVersionPreferenceController extends BasePreferenceController {
public KernelVersionPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public CharSequence getSummary() {
return DeviceInfoUtils.getFormattedKernelVersion(mContext);
}
}
通过 DeviceInfoUtils.getFormattedKernelVersion(mContext) 获取文本
10.0 对应 DeviceInfoUtils
frameworks\base\packages\SettingsLib\src\com\android\settingslib\DeviceInfoUtils.java
public static String getFormattedKernelVersion(Context context) {
return formatKernelVersion(context, Os.uname());
}
@VisibleForTesting
static String formatKernelVersion(Context context, StructUtsname uname) {
if (uname == null) {
return context.getString(R.string.status_unavailable);
}
// Example:
// 4.9.29-g958411d
// #1 SMP PREEMPT Wed Jun 7 00:06:03 CST 2017
final String VERSION_REGEX =
"(#\\d+) " + /* group 1: "#1" */
"(?:.*?)?" + /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
"((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 2: "Thu Jun 28 11:02:39 PDT 2012" */
Matcher m = Pattern.compile(VERSION_REGEX).matcher(uname.version);
if (!m.matches()) {
Log.e(TAG, "Regex did not match on uname version " + uname.version);
return context.getString(R.string.status_unavailable);
}
String user = android.os.SystemProperties.get("ro.build.user", "");
String host = android.os.SystemProperties.get("ro.build.host", "");
// Example output:
// 4.9.29-g958411d
// #1 Wed Jun 7 00:06:03 CST 2017
return new StringBuilder().append(uname.release)
.append("\n")
.append(user + "@" + host)
.append(m.group(1))
.append(" ")
.append(m.group(2)).toString();
}
8.0 对应 DeviceInfoUtils
frameworks\base\packages\SettingsLib\src\com\android\settingslib\DeviceInfoUtils.java
private static final String FILENAME_PROC_VERSION = "/proc/version";
public static String getFormattedKernelVersion() {
try {
return formatKernelVersion(readLine(FILENAME_PROC_VERSION));
} catch (IOException e) {
Log.e(TAG, "IO Exception when getting kernel version for Device Info screen",
e);
return "Unavailable";
}
}
public static String formatKernelVersion(String rawKernelVersion) {
// Example (see tests for more):
// Linux version 4.9.29-g958411d (android-build@xyz) (Android clang version 3.8.256229 \
// (based on LLVM 3.8.256229)) #1 SMP PREEMPT Wed Jun 7 00:06:03 CST 2017
// Linux version 4.9.29-geb63318482a7 (android-build@xyz) (gcc version 4.9.x 20150123 \
// (prerelease) (GCC) ) #1 SMP PREEMPT Thu Jun 1 03:41:57 UTC 2017
final String PROC_VERSION_REGEX =
"Linux version (\\S+) " + /* group 1: "3.0.31-g6fb96c9" */
"\\((\\S+?)\\) " + /* group 2: "([email protected]) " */
"\\((.+?)\\) " + /* group 3: kernel toolchain version information */
"(#\\d+) " + /* group 4: "#1" */
"(?:.*?)?" + /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
"((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 5: "Thu Jun 28 11:02:39 PDT 2012" */
Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
if (!m.matches()) {
Log.e(TAG, "Regex did not match on /proc/version: " + rawKernelVersion);
return "Unavailable";
} else if (m.groupCount() < 4) {
Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount()
+ " groups");
return "Unavailable";
}
return m.group(1) + " ("+ m.group(3) + ")\n" + // 3.0.31-g6fb96c9 (toolchain version)
m.group(2) + " " + m.group(4) + "\n" + // [email protected] #1
m.group(5); // Thu Jun 28 11:02:39 PDT 2012
}
那么我们只需要在 formatKernelVersion() 中返回值做点手脚即可,增加编译用户信息,那么问题来了,如何知道是谁编译的系统呢?
答案就在 buil.prop 中,此文件包含了系统全面信息,从cpu 架构到版本号各种信息,当然里面也有我们需要的编译用户姓名。
out\target\product\k62v1_64_bsp\system\build.prop
ro.build.version.release=10
ro.build.version.security_patch=2020-02-05
ro.build.version.base_os=
ro.build.version.min_supported_target_sdk=23
ro.build.date=Sat Jul 11 09:13:46 CST 2020
ro.build.date.utc=1594430026
ro.build.type=user
ro.build.user=ntu
ro.build.host=ubuntu
可以看到 ro.build.user 和 ro.build.host 正是我们所需要的,通过 SystemProperties api 读取即可
那这两个字段是在那里写入的呢?答案就在 buildinfo.sh 中
build\tools\buildinfo.sh
echo "ro.build.type=$TARGET_BUILD_TYPE"
echo "ro.build.user=$BUILD_USERNAME"
echo "ro.build.host=$BUILD_HOSTNAME"
那 BUILD_USERNAME 和 BUILD_HOSTNAME 又是谁赋值的呢?答案如下
./soong/ui/build/kati.go: if _, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok {
./soong/ui/build/kati.go: cmd.Environment.Set("BUILD_USERNAME", u.Username)
./make/tools/buildinfo.sh:echo "ro.build.user=$BUILD_USERNAME"
./make/Changes.md:make variable `BUILD_USERNAME` for now.
./make/core/main.mk: FILE_NAME_TAG := eng.$(BUILD_USERNAME)
./make/core/config.mk:$(KATI_deprecated_var USER,Use BUILD_USERNAME instead. See $(CHANGES_URL)#USER)
./make/core/version_defaults.mk: BUILD_NUMBER := eng.$(shell echo $${BUILD_USERNAME:0:6}).$(shell $(DATE) +%Y%m%d.%H%M%S)
./soong/ui/build/kati.go: if _, ok := cmd.Environment.Get("BUILD_HOSTNAME"); !ok {
./soong/ui/build/kati.go: cmd.Environment.Set("BUILD_HOSTNAME", hostname)
./make/tools/buildinfo.sh:echo "ro.build.host=$BUILD_HOSTNAME"
./make/Changes.md:of `android-build`. The real value is available as `BUILD_HOSTNAME`.