Android类加载器错误

时间:2014-03-21 09:45:08

标签: java android plugins urlclassloader dexclassloader

我想在我的jar文件中实例化ScreenX类并尝试该网站上的所有搜索,但它无法运行。

我有两个文件;

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;
}
}

有没有工作样品? 请帮忙......

1 个答案:

答案 0 :(得分:-1)

这个问题就这样解决了。 .Jar文件复制android-sdk \ build-tools \ android-x.x并按shift +右键单击运行命令提示符。之后在命令提示符下写入该代码dx --dex --keep-classes --output = plugin.jar plugin.jar。