fun Context.copyFiles(fileName: String, desFile: File) { val input = assets.open(fileName) val output = FileOutputStream(desFile.absoluteFile) valdata: ByteArray = kotlin.ByteArray(1024) var i: Int while (true) { i = input.read(data) if (i == -1) break output.write(data, 0, i) }
funloadDexClass(){ var cacheFile = externalCacheDir var internalPath = cacheFile.absolutePath+ File.separator+"dynamic_dex.jar" var desFile = File(internalPath) if(!desFile.exists()){ desFile.createNewFile() copyFiles("dynamic_dex.jar",desFile) }
// 开始加载dex class val dexClassLoader = DexClassLoader(internalPath,cacheFile.absolutePath,null,classLoader) val libClazz = dexClassLoader.loadClass("com.xiasuhuei321.firstpro.ShowToastImpl") val impl = libClazz.newInstance() as IShowToast impl.showToast(this) } }