加载apk,并启动apk中activity
private void loadPlugin() { String pluginFolder = Environment.getExternalStorageDirectory() + "/download/plugin.apk"; File file = new File(pluginFolder); if (file.exists()) { PackageInfo packageInfo = getPackageManager().getPackageArchiveInfo(file.getPath(), PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES); if (packageInfo == null) { return; } String launcherActivityName = null; if (packageInfo.activities != null && packageInfo.activities.length > 0) { launcherActivityName = packageInfo.activities[0].name; } Intent intent = new Intent(); intent.setPackage(packageInfo.packageName); intent.setClassName(packageInfo.packageName, launcherActivityName); startActivity(intent); // DexClassLoader dexClassLoader = createDexClassLoader(file.getPath()); // AssetManager assetManager = createAssetManager(file.getPath()); // Resources resources = createResources(assetManager); } } private DexClassLoader createDexClassLoader(String dexPath) { File dexOutputDir = getDir("dex", Context.MODE_PRIVATE); String dexOutputPath = dexOutputDir.getAbsolutePath(); String libPath = getDir("lib", Context.MODE_PRIVATE).getAbsolutePath(); DexClassLoader loader = new DexClassLoader(dexPath, dexOutputPath, libPath, getClassLoader()); return loader; } private AssetManager createAssetManager(String dexPath) { try { AssetManager assetManager = AssetManager.class.newInstance(); Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class); addAssetPath.invoke(assetManager, dexPath); return assetManager; } catch (Exception e) { e.printStackTrace(); return null; } } private Resources createResources(AssetManager assetManager) { Resources superRes = getResources(); Resources resources = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration()); return resources; }