我想在我的 jar 文件中实例化 ScreenX 类并尝试此站点上的所有 solition,但它无法运行。
我有两个文件;
File folder = getDir("JarFile", MODE_PRIVATE);
//and
File file = new File(folder, "plugin.jar");
我尝试了闲置代码。
//this is one
PathClassLoader pathClassLoader = new PathClassLoader(file.getAbsolutePath(),
ClassLoader.getSystemClassLoader());
try {
Class<?> handler = Class.forName("com.example.plugins.ScreenX", true, pathClassLoader);
} catch (ClassNotFoundException e) {
// i catched class not found exception
}
// this is two
DexFile df = DexFile.loadDex(file.getAbsolutePath(), getFilesDir().getAbsolutePath() + "/outputdexcontainer.dex", 0);
ClassLoader cl = getClassLoader();
Class clazz = df.loadClass("com/example/plugins/ScreenX", cl);
// this code changed like com.example.plugins.ScreenX but it doesn't work again
// i catch java.io.IOException: unable to open DEX file
// 3
URL[] urls = { file.toURL() };
URLClassLoader loader = new URLClassLoader(urls, getClassLoader());
loader.loadClass("com.example.plugins.ScreenX");
//java.lang.ClassNotFoundException: com.example.plugins.ScreenX
//4
ClassLoader cl = new DexClassLoader(file.getAbsolutePath(), new File(getDir("testDex", 0),"ff.dex").getAbsolutePath(), file.getAbsolutePath()/*or null*/, getClassLoader());
cl.loadClass("com.example.plugins.ScreenX");
//java.lang.ClassNotFoundException: Didn't find class "com.example.plugins.ScreenX" on path: /data/data/com.example.anaekran/app_JarFile/plugin.jar
这是我的 plugin.jar 的 ScreenX 类的代码。
package com.example.plugins;
// import some package
public class ScreenX implements IEkran {
@Override
public Button butonVer(final Activity ac) {
Button b = new Button(ac);
b.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1));
b.setBackgroundResource(R.drawable.ic_launcher);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ac, ScreenXActivity.class);
ac.startActivity(intent);
}
});
return b;
}
}
有工作样本吗?请帮忙...