Search code examples
androiddexandroid-runtimedex2oat

Is it possible turn all dex in one apk to oat when installing apk?


I found this code in Android:

bool DexFile::OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
                          std::string* error_msg, std::vector<const DexFile*>* dex_files) {
    ZipOpenErrorCode error_code;
    std::unique_ptr<const DexFile> dex_file(Open(zip_archive, kClassesDex, location, error_msg,
                                               &error_code));
    if (dex_file.get() == nullptr) {
      return false;
    } else {
      // Had at least classes.dex.
      dex_files->push_back(dex_file.release());

      // Now try some more.
      size_t i = 2;
      while (i &lt 100) {
      // We could try to avoid std::string allocations by working on a char array directly. As we
      // do not expect a lot of iterations, this seems too involved and brittle.
      std::string name = StringPrintf("classes%zu.dex", i);
      std::string fake_location = location + ":" + name;
      std::unique_ptr<const DexFile> next_dex_file(Open(zip_archive, name.c_str(), fake_location,
                                                        error_msg, &error_code));
      if (next_dex_file.get() == nullptr) {
        if (error_code != ZipOpenErrorCode::kEntryNotFound) {
          LOG(WARNING) << error_msg;
        }
        break;
      } else {
        dex_files->push_back(next_dex_file.release());
      }

      i++;
    }

    return true;

from https://android.googlesource.com/platform/art/+/master/runtime/dex_file.cc master

Is that means if APK has multi dex in root dir, dex2oat will turn all DEX to OAT when using ART ?

How I can turn all dex to oat when apk installing ?


Solution

  • Is that means if APK has multi dex in root dir, dex2oat will turn all DEX to OAT when using ART?

    Yes. oat file structure "contains" many dex files:

    oat file structure

    Picture taken from Paul Sabanal's slides.

    And now, how I can turn all dex to oat when apk installing.

    This is done automatically during the installation.