记录一个小问题-art解析dex错误的一个可能性

前言

最近两天开始接公司的热更新sdk,遇到了一个小问题;问题本身并不复杂,但是google了一圈没有能解决我们的问题,因此稍稍记录下;如果后面有人遇到了,希望能提供一种可能的方向;

现象

接热更新sdk时发现了一个现象,宿主包编release包时就会点击直接闪退;但是debug包就不会;这个应该是混淆有关系,但是前面已经接了一个渠道时没问题的,这就有点奇怪了;

报错栈如下

08-25 13:54:47.833 25875 25875 D AndroidRuntime: Shutting down VM
08-25 13:54:47.833 25875 25875 E AndroidRuntime: FATAL EXCEPTION: main
08-25 13:54:47.833 25875 25875 E AndroidRuntime: Process: com.oasis.sdk.huawei, PID: 25875
08-25 13:54:47.833 25875 25875 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate application com.bytedance.thanoshost.ThanosApplication: java.lang.ClassNotFoundException: Didn't find class "com.bytedance.thanoshost.ThanosApplication" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/lib/arm, /data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/base.apk!/lib/armeabi-v7a, /system/lib, /product/lib]]
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.LoadedApk.makeApplication(LoadedApk.java:1183)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6688)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.ActivityThread.access$2000(ActivityThread.java:273)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2020)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:112)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:216)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:7625)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
08-25 13:54:47.833 25875 25875 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.bytedance.thanoshost.ThanosApplication" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/lib/arm, /data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/base.apk!/lib/armeabi-v7a, /system/lib, /product/lib]]
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:50)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.Instrumentation.newApplication(Instrumentation.java:1127)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        at android.app.LoadedApk.makeApplication(LoadedApk.java:1175)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        ... 9 more
08-25 13:54:47.833 25875 25875 E AndroidRuntime:        Suppressed: java.io.IOException: Failed to open dex files from /data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/base.apk because: Failure to verify dex file '/data/app/com.oasis.sdk.huawei-vezpsDPHdzkNzr9l7V_5pQ==/base.apk': Out-of-order type_ids: 1d3b then 1d3b
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.DexFile.openDexFileNative(Native Method)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.DexFile.openDexFile(DexFile.java:354)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.DexFile.<init>(DexFile.java:101)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.DexFile.<init>(DexFile.java:75)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.DexPathList.<init>(DexPathList.java:164)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:74)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:84)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:801)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at android.app.LoadedApk.getClassLoader(LoadedApk.java:884)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at android.app.LoadedApk.getResources(LoadedApk.java:1135)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at android.app.ContextImpl.createAppContext(ContextImpl.java:2623)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6595)
08-25 13:54:47.833 25875 25875 E AndroidRuntime:                ... 8 more

google上搜了一圈,虽然有这个报错,但是说的解决方案却不生效,因此这个问题还得进一步分析;

分析

首先看这个报错的位置

bool DexFileVerifier::CheckInterTypeIdItem() {
  const DexFile::TypeId* item = reinterpret_cast<const DexFile::TypeId*>(ptr_);
  const char* descriptor = dex_file_->StringDataByIdx(item->descriptor_idx_);
  // Check that the descriptor is a valid type.
  if (!IsValidDescriptor(descriptor)) {
    LOG(ERROR) << StringPrintf("Invalid type descriptor: '%s'", descriptor);
    return false;
  }
  // Check ordering between items.
  if (previous_item_ != NULL) {
    const DexFile::TypeId* prev_item = reinterpret_cast<const DexFile::TypeId*>(previous_item_);
    if (prev_item->descriptor_idx_ >= item->descriptor_idx_) {
      LOG(ERROR) << StringPrintf("Out-of-order type_ids: %x then %x",
          prev_item->descriptor_idx_, item->descriptor_idx_);
      return false;
    }
  }
  ptr_ += sizeof(DexFile::TypeId);
  return true;
}

很明显了,这是art虚拟机解析dexFile的时候出现了问题,type id重复了,我们把crash的apk中的classes.dex拿出来看下(010Editor配合Dex.bt使用,F5生成解析结果)

dex type解析.png

可以看到有两个a.a.a.a的类型,然后看了下生成的mapping文件,果然有一个类被混淆成了a.a.a.a,那么改变混淆规则应该就可以解决这个问题

-repackageclasses 'g.huawei.thanos'

加上这行后重新编译,问题解决,果然成功进入了apk

总结

为Out-of-order这种报错提供一种可能性,其实还是很简单的一个问题,顺手记录下;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近项目apk方法数即将达到65536上限,虽然通过瘦身减少了一些方法数,但是随着更多sdk的接入,终究还是避免不...
    the_q阅读 16,573评论 6 39
  • 最近几周一直在研究如何为APK瘦身,折腾了很久,是时候写篇博客总结一下了,虽然已经准备了下周一要在客户端周会分享用...
    风清袖一阅读 1,091评论 1 10
  • 本文来自尚妆Android团队青峰发表于尚妆博客 APK瘦身探索 最近几周一直在研究如何为APK瘦身,折腾了很久,...
    尚妆产品技术刊读阅读 1,809评论 1 23
  • 读书笔记,写写画画记忆更深刻,如果还能梳理一下的话,那就更好了。 热修复技术介绍 探索之路 最开始,手淘是基于Xp...
    primLooper阅读 3,997评论 2 15
  • Tinker 热补丁接入过程中的坑!!! =============== Tinker 介绍 官方接入说明 gra...
    朱立志阅读 2,188评论 0 2