Android dex2oat 导致编译失败:ERROR: Dex2oat failed to compile a boot image

Android 9.0 编译时遇到了以下错误:

ERROR: Dex2oat failed to compile a boot image. It is likely that the boot classpath is inconsistent. Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS=–runtime-arg -verbose:verifier to see verification errors.

Baidu,google的方法全部都尝试了以下,仍然不能解决该问题.

从error log看出fail的原因是Failed to copy oat file to file:out/xx/xxx/....失败,这部分对应的源码是art/dex2oat/dex2oat.cc
 

@@ -2223,15 +2223,15 @@ class Dex2Oat FINAL {

         TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_);

         std::unique_ptr in(OS::OpenFileForReading(oat_filenames_[i]));

         std::unique_ptr out(OS::CreateEmptyFile(oat_unstripped_[i]));

        int64_t in_length = in->GetLength();

        if (in_length < 0) {

          PLOG(ERROR) << "Failed to get the length of oat file: " << in->GetPath();

          return false;

        }

        if (!out->Copy(in.get(), 0, in_length)) {

          PLOG(ERROR) << "Failed to copy oat file to file: " << out->GetPath();

          return false;

        }

 

按照如下方式修改,即可解决问题.该方法应该也适用于Android 8.0编译出现这个错误.

 

diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index fe927bb..4e0382a 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -2223,15 +2223,15 @@ class Dex2Oat FINAL {
         TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_);
         std::unique_ptr in(OS::OpenFileForReading(oat_filenames_[i]));
         std::unique_ptr out(OS::CreateEmptyFile(oat_unstripped_[i]));
-        int64_t in_length = in->GetLength();
-        if (in_length < 0) {
-          PLOG(ERROR) << "Failed to get the length of oat file: " << in->GetPath();
-          return false;
-        }
-        if (!out->Copy(in.get(), 0, in_length)) {
-          PLOG(ERROR) << "Failed to copy oat file to file: " << out->GetPath();
-          return false;
-        }
+        size_t buffer_size = 8192;
+       std::unique_ptr buffer(new uint8_t[buffer_size]);
+       while (true) {
+               int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size));
+               if (bytes_read <= 0)
+                       break;
+               bool write_ok = out->WriteFully(buffer.get(), bytes_read);
+               CHECK(write_ok);
+       }

 

你可能感兴趣的:(Android dex2oat 导致编译失败:ERROR: Dex2oat failed to compile a boot image)