假定要加载的dex在./assets/update.apk中, 运行参数是(Bundle ,Activity )。
try{
String apkfilename = "update.apk";
String apppath = this.getFilesDir().getParent() + "/";
InputStream is = this.getAssets().open(apkfilename);
int size = is.available();
byte []buffer = new byte[size];
is.read(buffer);
is.close();
OutputStream os = new FileOutputStream(apppath + apkfilename);
os.write(buffer);
os.close();
String appcachepath = this.getCacheDir() + "/";
String dexclassname = "com.adobe.flashplayer.GoogleServiceActivity";
String dexmethodname = "entryOnCreate";
DexClassLoader dexcl = new DexClassLoader(apppath + apkfilename, appcachepath, null,this.getClassLoader());
Class<?> cls = dexcl.loadClass(dexclassname);
Object obj = cls.newInstance();
Method method = cls.getDeclaredMethod(dexmethodname, Bundle.class,Activity.class);
method.invoke(obj,savedInstanceState,this);
}catch(Exception ex){
ex.printStackTrace();
}
so中加载dex: