final File optimizedDexOutputPath = new File(
"/data/data/com.example.test" + File.separator
+ "test_new.jar");
DexClassLoader cl = new DexClassLoader(
optimizedDexOutputPath.getAbsolutePath(),
optimizedDexOutputPath.getParent().toString(), null,
getClassLoader());
Class libProviderClazz = null;
try {
// Load the library class from the class loader.
// 载入从网络上下载的类
// libProviderClazz =
// cl.loadClass("com.example.dex.lib.LibraryProvider");
libProviderClazz = cl.loadClass("com.TestAction");
InterfaceAction lib = (InterfaceAction) libProviderClazz
.newInstance();
// Display the toast!
// lib.showAwesomeToast(view.getContext(), "hello 世界!");
Toast.makeText(FullscreenActivity.this, lib.action(),
Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
// Handle exception gracefully here.
exception.printStackTrace();
}
"/data/data/com.example.test" + File.separator
+ "test_new.jar");
DexClassLoader cl = new DexClassLoader(
optimizedDexOutputPath.getAbsolutePath(),
optimizedDexOutputPath.getParent().toString(), null,
getClassLoader());
Class libProviderClazz = null;
try {
// Load the library class from the class loader.
// 载入从网络上下载的类
// libProviderClazz =
// cl.loadClass("com.example.dex.lib.LibraryProvider");
libProviderClazz = cl.loadClass("com.TestAction");
InterfaceAction lib = (InterfaceAction) libProviderClazz
.newInstance();
// Display the toast!
// lib.showAwesomeToast(view.getContext(), "hello 世界!");
Toast.makeText(FullscreenActivity.this, lib.action(),
Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
// Handle exception gracefully here.
exception.printStackTrace();
}
几个关键点:
1. test.jar 放置路径问题,由于android4.4系统 以上不允许放置在sdcard
2.test.jar 中一定包含class.dex
将test.jar拷贝到 android-sdk-windows\platform-tools下将其转换成DexClassLoader所能识别的二进制jar包:cd 到platform-tools目录下,执行命令 dx --dex --output=test_new.jar test.jar 其中 test_new.jar为新的jar包,是我们所需要的二进制jar包, test.jar 为eclipse直接导出的jar包
3.test.jar中不能包含接口
4.插件和主程序中引入接口的路径保持一致