第一章提到PathClassLoader、DexClassLoader的区别仅仅在于构造方法中的optimizedDirectory是否为空这个问题,BaseDexClassLoader的构造方法如下:
public BaseDexClassLoader(String dexPath, File optimizedDirectory,
String librarySearchPath, ClassLoader parent) {
super(parent);
this.pathList = new DexPathList(this, dexPath, librarySearchPath, optimizedDirectory);
}
DexPathList主要基于三个参数来创建实例,其构造方法如下:
public DexPathList(ClassLoader definingContext, String dexPath,
String librarySearchPath, File optimizedDirectory) {
if (definingContext == null) {
throw new NullPointerException("definingContext == null");
}
if (dexPath == null) {
throw new NullPointerException("dexPath == null");
}
if (optimizedDirectory != null) {
if (!optimizedDirectory.exists()) {
throw new IllegalArgumentException(
"optimizedDirectory doesn't exist: "
+ optimizedDirectory);
}
if (!(optimizedDirectory.canRead()
&& optimizedDirectory.canWrite())) {
throw new IllegalArgumentException(
"optimizedDirectory not readable/writable: "
+ optimizedDirectory);
}
}
this.definingContext = definingContext;
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
// save dexPath for BaseDexClassLoader
//将dex文件封装成Element对象,也就是第一章里面数组的数据
this.dexElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory,
suppressedExceptions, definingContext);
// Native libraries may exist in both the system and
// application library paths, and we use this search order:
//
// 1. This class loader's library path for application libraries (librarySearchPath):
// 1.1. Native library directories
// 1.2. Path to libraries in apk-files
// 2. The VM's library path from the system property for system libraries
// also known as java.library.path
//
// This order was reversed prior to Gingerbread; see http://b/2933456.
this.nativeLibraryDirectories = splitPaths(librarySearchPath, false);
this.systemNativeLibraryDirectories =
splitPaths(System.getProperty("java.library.path"), true);
List<File> allNativeLibraryDirectories = new ArrayList<>(nativeLibraryDirectories);
allNativeLibraryDirectories.addAll(systemNativeLibraryDirectories);
this.nativeLibraryPathElements = makePathElements(allNativeLibraryDirectories,
suppressedExceptions,
definingContext);
if (suppressedExceptions.size() > 0) {
this.dexElementsSuppressedExceptions =
suppressedExceptions.toArray(new IOException[suppressedExceptions.size()]);
} else {
dexElementsSuppressedExceptions = null;
}
}
makeDexElements()方法通过查找dex文件的路径,然后将dex文件信息封装成Element,便于后续查找。makeDexElements()方法源码如下:
private static Element[] makeDexElements(List<File> files, File optimizedDirectory,
List<IOException> suppressedExceptions,
ClassLoader loader) {
return makeElements(files, optimizedDirectory, suppressedExceptions, false, loader);
}
private static Element[] makeElements(List<File> files, File optimizedDirectory,
List<IOException> suppressedExceptions,
boolean ignoreDexFiles,
ClassLoader loader) {
Element[] elements = new Element[files.size()];
int elementsPos = 0;
/*
* Open all files and load the (direct or contained) dex files
* up front.
*/
//file其实就是dex文件
for (File file : files) {
File zip = null;
File dir = new File("");
DexFile dex = null;
String path = file.getPath();
String name = file.getName();
if (path.contains(zipSeparator)) {
String split[] = path.split(zipSeparator, 2);
zip = new File(split[0]);
dir = new File(split[1]);
//可以是资源,native lib的路径
} else if (file.isDirectory()) {
// We support directories for looking up resources and native libraries.
// Looking up resources in directories is useful for running libcore tests.
elements[elementsPos++] = new Element(file, true, null, null);
} else if (file.isFile()) {
//检查是否是dex文件
if (!ignoreDexFiles && name.endsWith(DEX_SUFFIX)) {
// Raw dex file (not inside a zip/jar).
try {
//生成DexFile
dex = loadDexFile(file, optimizedDirectory, loader, elements);
} catch (IOException suppressed) {
System.logE("Unable to load dex file: " + file, suppressed);
suppressedExceptions.add(suppressed);
}
} else {
zip = file;
if (!ignoreDexFiles) {
try {
dex = loadDexFile(file, optimizedDirectory, loader, elements);
} catch (IOException suppressed) {
/*
* IOException might get thrown "legitimately" by the DexFile constructor if
* the zip file turns out to be resource-only (that is, no classes.dex file
* in it).
* Let dex == null and hang on to the exception to add to the tea-leaves for
* when findClass returns null.
*/
suppressedExceptions.add(suppressed);
}
}
}
} else {
System.logW("ClassLoader referenced unknown path: " + file);
}
if ((zip != null) || (dex != null)) {
elements[elementsPos++] = new Element(dir, false, zip, dex);
}
}
if (elementsPos != elements.length) {
elements = Arrays.copyOf(elements, elementsPos);
}
return elements;
}
loadDexFile接收了optimizedDirectory这个参数,并处理了optimizedDirectory这个参数。
private static DexFile loadDexFile(File file, File optimizedDirectory, ClassLoader loader,
Element[] elements)
throws IOException {
if (optimizedDirectory == null) {
return new DexFile(file, loader, elements);
} else {
String optimizedPath = optimizedPathFor(file, optimizedDirectory);
return DexFile.loadDex(file.getPath(), optimizedPath, 0, loader, elements);
}
}
DexFile的loadDex()方法也是调用了其构造方法,只是其outputName参数不为空,DexFile的构造方法最后都调用了openDexFile()方法,outputName参数也是透传给了openDexFile()。
private static Object openDexFile(String sourceName, String outputName, int flags,
ClassLoader loader, DexPathList.Element[] elements) throws IOException {
// Use absolute paths to enable the use of relative paths when testing on host.
return openDexFileNative(new File(sourceName).getAbsolutePath(),
(outputName == null)
? null
: new File(outputName).getAbsolutePath(),
flags,
loader,
elements);
}
openDexFileNative()方法是一个native方法。其native实现在dalvik_system_DexFile.cc中,源码如下:(jni相关知识,请自行脑补)
static jobject DexFile_openDexFileNative(JNIEnv* env,
jclass,
jstring javaSourceName,
jstring javaOutputName,
jint flags ATTRIBUTE_UNUSED,
jobject class_loader,
jobjectArray dex_elements) {
ScopedUtfChars sourceName(env, javaSourceName);
if (sourceName.c_str() == nullptr) {
return 0;
}
NullableScopedUtfChars outputName(env, javaOutputName);
if (env->ExceptionCheck()) {
return 0;
}
Runtime* const runtime = Runtime::Current();
ClassLinker* linker = runtime->GetClassLinker();
std::vector<std::unique_ptr<const DexFile>> dex_files;
std::vector<std::string> error_msgs;
const OatFile* oat_file = nullptr;
//获取DexFile列表
dex_files = runtime->GetOatFileManager().OpenDexFilesFromOat(sourceName.c_str(),
outputName.c_str(),
class_loader,
dex_elements,
/*out*/ &oat_file,
/*out*/ &error_msgs);
if (!dex_files.empty()) {
jlongArray array = ConvertDexFilesToJavaArray(env, oat_file, dex_files);
if (array == nullptr) {
ScopedObjectAccess soa(env);
for (auto& dex_file : dex_files) {
if (linker->FindDexCache(soa.Self(), *dex_file, true) != nullptr) {
dex_file.release();
}
}
}
return array;
} else {
ScopedObjectAccess soa(env);
CHECK(!error_msgs.empty());
// The most important message is at the end. So set up nesting by going forward, which will
// wrap the existing exception as a cause for the following one.
auto it = error_msgs.begin();
auto itEnd = error_msgs.end();
for ( ; it != itEnd; ++it) {
ThrowWrappedIOException("%s", it->c_str());
}
return nullptr;
}
}
接下来看一下OpenDexFilesFromOat()这个函数,其位于oat_file_manager.cc文件,源码如下:
std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
const char* dex_location,
const char* oat_location,
jobject class_loader,
jobjectArray dex_elements,
const OatFile** out_oat_file,
std::vector<std::string>* error_msgs) {
ScopedTrace trace(__FUNCTION__);
CHECK(dex_location != nullptr);
CHECK(error_msgs != nullptr);
// Verify we aren't holding the mutator lock, which could starve GC if we
// have to generate or relocate an oat file.
Thread* const self = Thread::Current();
Locks::mutator_lock_->AssertNotHeld(self);
Runtime* const runtime = Runtime::Current();
//初始化oat_file_assistant
OatFileAssistant oat_file_assistant(dex_location,
oat_location,
kRuntimeISA,
/*profile_changed*/false,
!runtime->IsAotCompiler());
// Lock the target oat location to avoid races generating and loading the
// oat file.
std::string error_msg;
if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
// Don't worry too much if this fails. If it does fail, it's unlikely we
// can generate an oat file anyway.
VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
}
const OatFile* source_oat_file = nullptr;
//dex文件是否已经oat化,oat文件是art虚拟机的ELF文件格式
if (!oat_file_assistant.IsUpToDate()) {
// Update the oat file on disk if we can. This may fail, but that's okay.
// Best effort is all that matters here.
//MakeUpToDate执行了dex文件的优化工作,即利用dex文件生成oat文件
switch (oat_file_assistant.MakeUpToDate(filter_, /*out*/ &error_msg)) {
case OatFileAssistant::kUpdateFailed:
LOG(WARNING) << error_msg;
break;
case OatFileAssistant::kUpdateNotAttempted:
// Avoid spamming the logs if we decided not to attempt making the oat
// file up to date.
VLOG(oat) << error_msg;
break;
case OatFileAssistant::kUpdateSucceeded:
// Nothing to do.
break;
}
}
// Get the oat file on disk.
//获取oat文件
std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
if (oat_file != nullptr) {
// Take the file only if it has no collisions, or we must take it because of preopting.
bool accept_oat_file =
!HasCollisions(oat_file.get(), class_loader, dex_elements, /*out*/ &error_msg);
if (!accept_oat_file) {
// Failed the collision check. Print warning.
if (Runtime::Current()->IsDexFileFallbackEnabled()) {
LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for "
<< dex_location;
} else {
LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
" load classes for " << dex_location;
}
LOG(WARNING) << error_msg;
// However, if the app was part of /system and preopted, there is no original dex file
// available. In that case grudgingly accept the oat file.
if (!oat_file_assistant.HasOriginalDexFiles()) {
accept_oat_file = true;
LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
<< "Allow oat file use. This is potentially dangerous.";
}
}
if (accept_oat_file) {
VLOG(class_linker) << "Registering " << oat_file->GetLocation();
//注册oat_file,其实就是将oat_file插入到oat_files_表中
source_oat_file = RegisterOatFile(std::move(oat_file));
*out_oat_file = source_oat_file;
}
}
std::vector<std::unique_ptr<const DexFile>> dex_files;
// Load the dex files from the oat file.
if (source_oat_file != nullptr) {
bool added_image_space = false;
if (source_oat_file->IsExecutable()) {
std::unique_ptr<gc::space::ImageSpace> image_space(
kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr);
if (image_space != nullptr) {
ScopedObjectAccess soa(self);
StackHandleScope<1> hs(self);
Handle<mirror::ClassLoader> h_loader(
hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)));
// Can not load app image without class loader.
if (h_loader.Get() != nullptr) {
std::string temp_error_msg;
// Add image space has a race condition since other threads could be reading from the
// spaces array.
{
ScopedThreadSuspension sts(self, kSuspended);
gc::ScopedGCCriticalSection gcs(self,
gc::kGcCauseAddRemoveAppImageSpace,
gc::kCollectorTypeAddRemoveAppImageSpace);
ScopedSuspendAll ssa("Add image space");
runtime->GetHeap()->AddSpace(image_space.get());
}
{
ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
h_loader,
dex_elements,
dex_location,
/*out*/&dex_files,
/*out*/&temp_error_msg);
}
if (added_image_space) {
// Successfully added image space to heap, release the map so that it does not get
// freed.
image_space.release();
} else {
LOG(INFO) << "Failed to add image file " << temp_error_msg;
dex_files.clear();
{
ScopedThreadSuspension sts(self, kSuspended);
gc::ScopedGCCriticalSection gcs(self,
gc::kGcCauseAddRemoveAppImageSpace,
gc::kCollectorTypeAddRemoveAppImageSpace);
ScopedSuspendAll ssa("Remove image space");
runtime->GetHeap()->RemoveSpace(image_space.get());
}
// Non-fatal, don't update error_msg.
}
}
}
}
if (!added_image_space) {
DCHECK(dex_files.empty());
dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
}
if (dex_files.empty()) {
error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
}
}
// Fall back to running out of the original dex file if we couldn't load any
// dex_files from the oat file.
if (dex_files.empty()) {
if (oat_file_assistant.HasOriginalDexFiles()) {
if (Runtime::Current()->IsDexFileFallbackEnabled()) {
if (!DexFile::Open(dex_location, dex_location, /*out*/ &error_msg, &dex_files)) {
LOG(WARNING) << error_msg;
error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
+ " because: " + error_msg);
}
} else {
error_msgs->push_back("Fallback mode disabled, skipping dex files.");
}
} else {
error_msgs->push_back("No original dex files found for dex location "
+ std::string(dex_location));
}
}
// TODO(calin): Consider optimizing this knowing that is useless to record the
// use of fully compiled apks.
Runtime::Current()->NotifyDexLoaded(dex_location);
return dex_files;
}
OatFileAssistant这个数据结构需要重点关注,如上使用了IsUpToDate()和MakeUpToDate()两个函数,OatFileAssistant的构造方法如下:
OatFileAssistant::OatFileAssistant(const char* dex_location,
const char* oat_location,
const InstructionSet isa,
bool profile_changed,
bool load_executable)
: isa_(isa), profile_changed_(profile_changed), load_executable_(load_executable) {
CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
dex_location_.assign(dex_location);
if (load_executable_ && isa != kRuntimeISA) {
LOG(WARNING) << "OatFileAssistant: Load executable specified, "
<< "but isa is not kRuntimeISA. Will not attempt to load executable.";
load_executable_ = false;
}
// If the user gave a target oat location, save that as the cached oat
// location now so we won't try to construct the default location later.
//oat_location不为空也就是BaseDexClassLoader构造方法中的optimizedDirectory不为空,PathClassLoader和DexClassLoader的区别也就在于此
if (oat_location != nullptr) {
cached_oat_file_name_ = std::string(oat_location);
cached_oat_file_name_attempted_ = true;//默认为false
cached_oat_file_name_found_ = true;
}
}
IsUpToDate()用于检查待加载的dex文件是否做过odex或者oat优化。如果是第一次加载文件,则IsUpToDate()返回false,其源码如下:
bool OatFileAssistant::IsUpToDate() {
return OatFileIsUpToDate() || OdexFileIsUpToDate();
}
bool OatFileAssistant::OatFileIsUpToDate() {
if (!oat_file_is_up_to_date_attempted_) {
oat_file_is_up_to_date_attempted_ = true;
const OatFile* oat_file = GetOatFile();
if (oat_file == nullptr) {
cached_oat_file_is_up_to_date_ = false;
} else {
cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file);
}
}
return cached_oat_file_is_up_to_date_;
}
bool OatFileAssistant::OdexFileIsUpToDate() {
if (!odex_file_is_up_to_date_attempted_) {
odex_file_is_up_to_date_attempted_ = true;
const OatFile* odex_file = GetOdexFile();
if (odex_file == nullptr) {
cached_odex_file_is_up_to_date_ = false;
} else {
cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file);
}
}
return cached_odex_file_is_up_to_date_;
}
MakeUpToDate()方法执行dex文件的优化过程,源码如下:
OatFileAssistant::MakeUpToDate(CompilerFilter::Filter target, std::string* error_msg) {
//GetDexOptNeeded()方法再次确认是否需要oat优化
switch (GetDexOptNeeded(target)) {
case kNoDexOptNeeded: return kUpdateSucceeded;
//对于首次加载的dex文件,都会走到这个case
case kDex2OatNeeded: return GenerateOatFile(target, error_msg);
case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg);
case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg);
}
UNREACHABLE();
}
接下来看看GenerateOatFile()是如何将dex文件夹优化成oat文件的
OatFileAssistant::GenerateOatFile(CompilerFilter::Filter target, std::string* error_msg) {
CHECK(error_msg != nullptr);
Runtime* runtime = Runtime::Current();
//检查是否开启了允许优化
if (!runtime->IsDex2OatEnabled()) {
*error_msg = "Generation of oat file for dex location " + dex_location_
+ " not attempted because dex2oat is disabled.";
return kUpdateNotAttempted;
}
//OatFileName()用于获取优化后的oat文件的存放路径。这个方法很重要
if (OatFileName() == nullptr) {
*error_msg = "Generation of oat file for dex location " + dex_location_
+ " not attempted because the oat file name could not be determined.";
return kUpdateNotAttempted;
}
const std::string& oat_file_name = *OatFileName();
// dex2oat ignores missing dex files and doesn't report an error.
// Check explicitly here so we can detect the error properly.
// TODO: Why does dex2oat behave that way?
//再次检查一下待加载的dex文件是否存在
if (!OS::FileExists(dex_location_.c_str())) {
*error_msg = "Dex location " + dex_location_ + " does not exists.";
return kUpdateNotAttempted;
}
std::unique_ptr<File> oat_file;
oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str()));
if (oat_file.get() == nullptr) {
*error_msg = "Generation of oat file " + oat_file_name
+ " not attempted because the oat file could not be created.";
return kUpdateNotAttempted;
}
//设置存放oat文件地址的可写权限
if (fchmod(oat_file->Fd(), 0644) != 0) {
*error_msg = "Generation of oat file " + oat_file_name
+ " not attempted because the oat file could not be made world readable.";
oat_file->Erase();
return kUpdateNotAttempted;
}
//配置参数
std::vector<std::string> args;
args.push_back("--dex-file=" + dex_location_);
args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
args.push_back("--oat-location=" + oat_file_name);
args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(target));
//执行dex到oat的优化,关于dex到oat的优化,后续会专门讲解
if (!Dex2Oat(args, error_msg)) {
// Manually delete the file. This ensures there is no garbage left over if
// the process unexpectedly died.
oat_file->Erase();
unlink(oat_file_name.c_str());
return kUpdateFailed;
}
if (oat_file->FlushCloseOrErase() != 0) {
*error_msg = "Unable to close oat file " + oat_file_name;
unlink(oat_file_name.c_str());
return kUpdateFailed;
}
// Mark that the oat file has changed and we should try to reload.
ClearOatFileCache();
return kUpdateSucceeded;
}
上面说了OatFileName()这个方法很重要,那么久先看看他的源码,
const std::string* OatFileAssistant::OatFileName() {
//cached_oat_file_name_attempted_这个变量在OatFileAssistant的构造方法中出现过,当oat_location(也就是optimizedDirectory)不为空时,cached_oat_file_name_attempted_为true,否则为false。
if (!cached_oat_file_name_attempted_) {
cached_oat_file_name_attempted_ = true;
// Compute the oat file name from the dex location.
// TODO: The oat file assistant should be the definitive place for
// determining the oat file name from the dex location, not
// GetDalvikCacheFilename.
//获取一个公共的缓存空间
std::string cache_dir = StringPrintf("%s%s",
DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_));
std::string error_msg;
//根据公共的缓存空间和dex文件的地址来共同确认一个oat文件的存放地址
cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(),
cache_dir.c_str(), &cached_oat_file_name_, &error_msg);
if (!cached_oat_file_name_found_) {
// If we can't determine the oat file name, we treat the oat file as
// inaccessible.
LOG(WARNING) << "Failed to determine oat file name for dex location "
<< dex_location_ << ": " << error_msg;
}
}
return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr;
}
OatFileName()这个方法具体是干了些啥呢?总结起来就是:如果BaseDexClassLoader的optimizedDirectory参数为空,就指定一个地址存放优化后的oat文件。这就是optimizedDirectory是否为空的唯一区别。
到这里很多人会有疑问,不是说PathClassLoader(optimizedDirectory为空)只能加载dex文件么,而且dex文件路径还必须是内部存储路径。这些所谓的限制条件,已经在Android 6.0之后删除了。所以,PathClassLoader和DexClassLoader的功能已经完全一样,无需区分。
网友评论