Writable dex file '/data/data/xxx.xxx.xxx/code_cache/.overlay/base.apk/classes3.dex' is not allowed
调试的时候遇到了下面的错误:
java.lang.SecurityException: Writable dex file '/data/data/com.deepcode.trace/code_cache/.overlay/base.apk/classes3.dex' is not allowed.
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:406)
at dalvik.system.DexFile.<init>(DexFile.java:128)
at dalvik.system.DexFile.<init>(DexFile.java:101)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:438)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:387)
at dalvik.system.DexPathList.<init>(DexPathList.java:166)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:160)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:130)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:146)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:93)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:134)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:126)
at android.app.ApplicationLoaders.getClassLoaderWithSharedLibraries(ApplicationLoaders.java:61)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:1048)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:1140)
at android.app.LoadedApk.getResources(LoadedApk.java:1392)
at android.app.ContextImpl.createAppContext(ContextImpl.java:3423)
at android.app.ContextImpl.createAppContext(ContextImpl.java:3415)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7047)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2340)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loopOnce(Looper.java:242)
at android.os.Looper.loop(Looper.java:362)
at android.app.ActivityThread.main(ActivityThread.java:8393)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:992)
这是老版本Android Studio(我用的是Android Studio2022.1.1)在Android14上的一个bug,在打开DEX、JAR、APK等文件时必须将DEX文件设置为只读,而Android Sutdio默认将代码的改动打包成DEX动态加载,从而引起错误.
解决方法是禁用Android Studio动态加载的打包优化,在Run->Edit Configurations中选中Always install with package manager (disables deploy optimizations on Android11 and later),或者换新版本的Android Studio.