android内联优化导致Inlined method resolution crossed dex file boundary

最近App在android11上出现了一个诡异的native 崩溃,很不容易出现,但都是有个特点就是安装App后过一段时间才会出现,杀进程没用,覆盖安装同一个apk,崩溃立刻消失,日志为如下:

1243 08-02 15:44:16.921 18134 18134 F xx.xx:p: entrypoint_utils-inl.h:101] Inlined method resolution crossed dex file boundary: from void com.xxx.l1ll1lIIl1I. Il1llll111.Il1IIl1lllII1.IIIIIlI1IIIl1$lIIIl11ll11.() in /data/app/~~dZ1qJ5hUv_FgD08Tr9d4pg==/com.xxx.android-x7T-p_8Jw69_1QmnZXhP6g==/base.apk/0x77fd7cf1d0 to void android.net.wifi.IWifiScanner$Stub.() in /apex/com.android.wifi/javalib/framework-wifi.jar/0xb4000077ed7ca590. This must be due to duplicate classes or playing wrongly with class loaders. The  runtime is in an unsafe state.

搜索系统art源码中抛出异常的地方:

 50 inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
 51                                     const CodeInfo& code_info,
 52                                     const BitTableRange& inline_infos)
 53     REQUIRES_SHARED(Locks::mutator_lock_) {
           ...
 94     if (UNLIKELY(inlined_method->GetDexFile() != method->GetDexFile())) {
 95       // TODO: We could permit inlining within a multi-dex oat file and the boot image,
 96       // even going back from boot image methods to the same oat file. However, this is
 97       // not currently implemented in the compiler. Therefore crossing dex file boundary
 98       // indicates that the inlined definition is not the same as the one used at runtime.
 99       bool target_sdk_at_least_p =
100           IsSdkVersionSetAndAtLeast(Runtime::Current()->GetTargetSdkVersion(), SdkVersion::kP);
101       LOG(target_sdk_at_least_p ? FATAL : WARNING)
102           << "Inlined method resolution crossed dex file boundary: from "
103           << method->PrettyMethod()
104           << " in " << method->GetDexFile()->GetLocation() << "/"
105           << static_cast(method->GetDexFile())
106           << " to " << inlined_method->PrettyMethod()
107           << " in " << inlined_method->GetDexFile()->GetLocation() << "/"
108           << static_cast(inlined_method->GetDexFile()) << ". "
109           << "This must be due to duplicate classes or playing wrongly with class loaders. "
110           << "The runtime is in an unsafe state.";
111     }
112     method = inlined_method;
113   }
115   return method;
116 }

发现大致意思就是,我们App中集成了framwwork的android.net.wifi.IWifiScanner这个类,在系统dexopt之后发生了内联优化,导致这个系统的类被内敛到odex中了,然后被系统检测到caller与callee处于不同的dex file,也就是在App的odex中有一份,在系统framework-wifi.jar中也有一份,所以主动发起abort(inline不允许跨dex文件),导致应用出现闪退等异常问题,但是sdk小于P的话,只会报WARNING而不是FATAL。
于是在安装了App之后手动对apk进行dexopt

adb shell cmd package compile -m speed -f  packageName

这个崩溃就必现了,这样就比较好分析了,只需要防止这个类被dex优化内敛就行了,加个try-catch就可以。
参考:
Android疑难杂症——因内联优化导致9.0机型Native Crash
Android P新增检测项 应用热修复受重大影响

你可能感兴趣的:(android内联优化导致Inlined method resolution crossed dex file boundary)