用build-tools的dex打包失败
改用d8即可具体使用方式可参照
https://developer.android.google.cn/studio/command-line/d8
加载时 No such file or directory
private static dalvik.system.DexPathList$Element[] dalvik.system.DexPathList.makeDexElements(java.util.List,java.io.File,java.util.List,java.lang.ClassLoader)
2020-07-20 18:54:58.633 30177-30177/com.dexloadtest E/System: Unable to load dex file: /storage/emulated/0/Android/data/com.dexloadtest/cache/classes3.dex
2020-07-20 18:54:58.641 30177-30177/com.dexloadtest E/System: java.io.IOException: Failed to open dex files from /storage/emulated/0/Android/data/com.dexloadtest/cache/classes3.dex because: Unable to open '/storage/emulated/0/Android/data/com.dexloadtest/cache/classes3.dex' : No such file or directory
每次加载都会删除掉dex文件,导致no such file
情况分析,Dexclassloader在加载file时,不能直接使用.dex文件,需要是d8或者dex生成的jar包或者apk包,如果直接是dex文件,会失败,然后删除文件,以下是DexClassLoader的解释
/**
* Creates a {@code DexClassLoader} that finds interpreted and native
* code. Interpreted classes are found in a set of DEX files contained
* in Jar or APK files.
*
* <p>The path lists are separated using the character specified by the
* {@code path.separator} system property, which defaults to {@code :}.
*
* @param dexPath the list of jar/apk files containing classes and
* resources, delimited by {@code File.pathSeparator}, which
* defaults to {@code ":"} on Android
* @param optimizedDirectory this parameter is deprecated and has no effect since API level 26.
* @param librarySearchPath the list of directories containing native
* libraries, delimited by {@code File.pathSeparator}; may be
* {@code null}
* @param parent the parent class loader
*/