javaandroidpluginsurlclassloaderdexclassloader

Android Class Loader Error


I want to instance the ScreenX class in my jar file and try all of the solition on this site but it can not run.

I have two file that;

File folder = getDir("JarFile", MODE_PRIVATE);
//and
File file = new File(folder, "plugin.jar");

I tried fallowing code.

//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

This is code of my plugin.jar's ScreenX class.

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

Is there any working sample? Please help...


Solution

  • This problem was solved like that. .Jar file copied android-sdk\build-tools\android-x.x and press shift + right click than run command prompt. After that write that code on command prompt dx --dex --keep-classes --output=plugin.jar plugin.jar.