美文网首页
初识Art虚拟机的dex加载(一)

初识Art虚拟机的dex加载(一)

作者: 噜噜丶 | 来源:发表于2018-10-08 11:45 被阅读0次

    0x1序
    G:\AndroidCode\android-4.4.4_r1\art\runtime\native

    static jint DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
      ScopedUtfChars sourceName(env, javaSourceName);
      if (sourceName.c_str() == NULL) {
        return 0;
      }
      std::string dex_location(sourceName.c_str());  //Dex2Oat文件存放路径
      NullableScopedUtfChars outputName(env, javaOutputName);
      if (env->ExceptionCheck()) {
        return 0;
      }
      ScopedObjectAccess soa(env);
    
      uint32_t dex_location_checksum;
      if (!DexFile::GetChecksum(dex_location, &dex_location_checksum)) {  //校验dex的魔数
        LOG(WARNING) << "Failed to compute checksum: " << dex_location;
        ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
        soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/IOException;",
                                       "Unable to get checksum of dex file: %s", dex_location.c_str());
        return 0;
      }
      //拿到当前的class链接器
      ClassLinker* linker = Runtime::Current()->GetClassLinker();
      const DexFile* dex_file;
      if (outputName.c_str() == NULL) {
        dex_file = linker->FindDexFileInOatFileFromDexLocation(dex_location, dex_location_checksum);
      } else {
        std::string oat_location(outputName.c_str());
        dex_file = linker->FindOrCreateOatFileForDexLocation(dex_location, dex_location_checksum, oat_location);
      }
      if (dex_file == NULL) {
        LOG(WARNING) << "Failed to open dex file: " << dex_location;
        ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
        soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/io/IOException;",
                                       "Unable to open dex file: %s", dex_location.c_str());
        return 0;
      }
      return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
    }
    

    0x2序

    
    

    相关文章

      网友评论

          本文标题:初识Art虚拟机的dex加载(一)

          本文链接:https://www.haomeiwen.com/subject/tdidaftx.html