Android Environment 获取的路径问题
1. 以获取 /System 路径为例
/**
* Return root of the "system" partition holding the core Android OS.
* Always present and mounted read-only.
*/
public static @NonNull File getRootDirectory() {
return DIR_ANDROID_ROOT;
}
// ENV_ANDROID_ROOT 的值为 "ANDROID_ROOT"
// private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
static File getDirectory(String variableName, String defaultPath) {
// 获取环境变量 ANDROID_ROOT 对应的值
String path = System.getenv(variableName);
return path == null ? new File(defaultPath) : new File(path);
}
1.1 获取环境变量 ANDROID_ROOT 对应的值
环境变量可以简单理解为可以在进程中获取的键值对。
2. 其他路径也是类似
private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
private static final String DIR_ANDROID_DATA_PATH = getDirectoryPath(ENV_ANDROID_DATA, "/data");
private static final File DIR_ANDROID_DATA = new File(DIR_ANDROID_DATA_PATH);
private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
private static final File DIR_METADATA = new File("/metadata");
private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
private static final File DIR_SYSTEM_EXT_ROOT = getDirectory(ENV_SYSTEM_EXT_ROOT,
"/system_ext");
private static final File DIR_APEX_ROOT = getDirectory(ENV_APEX_ROOT,
"/apex");