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后就不会再报错了。