Attempt to load writeable dex file 问题解决方案
做插件化的时候把apk文件从 assets 里面拷贝到存储空间里面开始用的是外部存储目录context.getExternalFilesDir(plugin),对应的存储目录是storage/emulated/0/Android/data/com.plugin.plugindemo/plugin.apk然后使用DexClassLoader加载插件包一直碰到这个报错Attempt to load writeable dex file设置了 file.setReadOnly()后显示修改成功了但还是报这个错。后来改成内部存储目录 context.getFilesDir()对应的存储目录是/data/user/0/com.plugin.plguindemo/files/plugin.apk再调用file.setReadOnly()后就不报错了。下面贴出完整代码PluginManager.javapackage com.plugin.plguindemo; import android.content.Context; import android.content.res.AssetManager; import android.content.res.Resources; import android.util.Log; import java.io.File; import java.lang.reflect.Method; import dalvik.system.DexClassLoader; public class PluginManager { private static PluginManager pluginManager; private Context context; private DexClassLoader classLoader; //存放插件包里的资源 private Resources resources; public static final String TAG PluginManager.class.getSimpleName(); public static PluginManager getInstance(Context context) { if (pluginManager null) { synchronized (PluginManager.class) { if (pluginManager null) { pluginManager new PluginManager(context); } } } return pluginManager; } private PluginManager(Context context) { this.context context; } /** * context.getFilesDir() * /data/user/0/com.plugin.plguindemo/files/plugin.apk * * context.getExternalFilesDir(); * storage/emulated/0/Android/data/com.plugin.plugindemo/plugin.apk */ public void loadPlugin() { String pluginName plugin.apk; //这一句也很重要放在私有目录下getFilesDir setReadOnly才有作用 //实测证明 context.getExternalFilesDir(); 这样获取到的是外部存储对外部存储的文件设置setReadOnly不起作用。 //Attempt to load writeable dex file: storage/emulated/0/Android/data/com.plugin.plugindemo/plugin.apk File pluginFile new File(context.getFilesDir(), pluginName); Log.e(xxx,pluginFile.getAbsolutePath()); if (!pluginFile.exists()) { // 调用之前学过的拷贝方法将assets中的APK复制到私有目录 if (!FileUtils.copyApkFromAssetsToPrivateDir(context, pluginName, pluginName)) { Log.e(TAG, 插件文件复制失败); }else{ load(pluginFile); } }else{ Log.d(TAG,pluginFile.getAbsolutePath()); load(pluginFile); } } private void load(File pluginFile){ try{ //这一句很重要。 pluginFile.setReadOnly(); // 2. 确定优化目录 (DexClassLoader 用于存放优化后的dex文件) File optimizedDir; if (android.os.Build.VERSION.SDK_INT android.os.Build.VERSION_CODES.LOLLIPOP) { // Android 5.0 推荐使用 code_cache 目录 [citation:3] optimizedDir context.getCodeCacheDir(); } else { // 低版本手动创建私有目录 [citation:1][citation:4][citation:5] optimizedDir context.getDir(dexopt, Context.MODE_PRIVATE); } // 3. 创建 DexClassLoader 实例来加载APK // 参数dexPath(apk路径), optimizedDirectory(优化目录), librarySearchPath(so库路径此处为null), parent(父加载器) [citation:1][citation:4] classLoader new DexClassLoader( pluginFile.getAbsolutePath(), optimizedDir.getAbsolutePath(), null, context.getClassLoader() ); //1.获取到操作插件里资源的Resources //资源管理器 可通过反射方式创建对象 AssetManager assetManager AssetManager.class.newInstance(); //context.getAssets(); //我们要调用添加资源路径的方法,String参数 Method addAssetPathMethod assetManager.getClass().getMethod(addAssetPath, String.class); addAssetPathMethod.invoke(assetManager, pluginFile.getAbsolutePath()); //执行了这一句后assetManager就能寻找插件包里的资源 //宿主里的资源配置信息如x-dpi等拿到这些信息给插件项目使用。 Resources hostResources context.getResources(); //加载插件里资源的Resources resources new Resources(assetManager, hostResources.getDisplayMetrics(), hostResources.getConfiguration()); Log.e(xxx,加载成功); }catch(Exception e){ e.fillInStackTrace(); } } public DexClassLoader getClassLoader() { return classLoader; } public Resources getResources() { return resources; } }补充说明 经过我亲测发现在targetSdkVersion30时无论是将需要加载的apk放在context.getExternalFilesDir()目录还是context.getFilesDir()设置为setReadOnly后都有效。不再报Attempt to load writeable dex file的错。在targetSdkVersion35时将需要加载的apk放在context.getExternalFilesDir() 即使设置了setReadOnly目录还是会报错但放在context.getFilesDir()目录下设置readOnly后就不会再报错了。

相关新闻

零基础搭建Kali Linux虚拟机环境:从下载到实战的完整指南

零基础搭建Kali Linux虚拟机环境:从下载到实战的完整指南

1. 项目概述与核心价值 如果你对网络安全、渗透测试或者Linux系统感兴趣,那么“Kali Linux”这个名字你一定不陌生。它不是一个普通的Linux发行版,而是一个由Offensive Security团队维护的、集成了数百种安全审计和渗透测试工具的“武器库”。对于安全从…

2026/10/31 22:21:01 阅读更多 →
Windows终极优化指南:用WinUtil一键解决90%系统问题

Windows终极优化指南:用WinUtil一键解决90%系统问题

Windows终极优化指南:用WinUtil一键解决90%系统问题 【免费下载链接】winutil Chris Titus Techs Windows Utility - Install Programs, Tweaks, Fixes, and Updates 项目地址: https://gitcode.com/GitHub_Trending/wi/winutil 还在为新电脑配置软件而烦恼&…

2026/10/25 20:51:42 阅读更多 →
如何快速上手开源服装制版软件:Seamly2D完整入门指南

如何快速上手开源服装制版软件:Seamly2D完整入门指南

如何快速上手开源服装制版软件:Seamly2D完整入门指南 【免费下载链接】Seamly2D Open source patternmaking software to democratize fashion. 项目地址: https://gitcode.com/gh_mirrors/se/Seamly2D 想要自己设计服装却担心专业软件太贵太难学&#xff1f…

2026/11/3 10:32:59 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/3 1:09:25 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/3 10:57:27 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/3 11:35:49 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/3 2:27:21 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/3 10:47:27 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/3 10:57:33 阅读更多 →

日新闻

周新闻

月新闻