How can i Load a jar file dynamically in an android application (4.0.3)
<p>I have an android application which has to load dynamically class ,an undefined number of a jar class which implemented an interface.</p>

<p>In fact, I look at a directory and list all the jar files which are in this directory
I open the manifest of the jar file and find the associated class and list them.
And after, i instancied a dexClassLoader to load all the jar files and to find if the classes i have found in the manisfest implement my interface.
Like this I can have all the class which implemented my interface without knowing them at the begginning</p>

<p>To resume, i have a list of class jar which implement my interface but the list is unknown by my android application and by me. The list of jar class can changed each time i launch my application.</p>

<p>But when i tried to create the DexClassLoader it is failed. I have always a null pointer</p>

<pre><code>DexClassLoader classLoader = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(),dexOutputDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
</code></pre>

<p>To make my test i used the emulator. I have copied with my DDMS the jar files into the directory
/data/data/com.example.Myappli/JarFilesDirectory/*.jar</p>

<p>Notice that my jar file contents the dex file</p>

<p>I read a lot of thing about this. Some permissions issues
I have tried every thing but not found the solution</p>

<p>Can someone help me please !!!</p>

<p>here the content of a manifest of a jar file</p>

<p>Manifest-Version: 1.0</p>

<p>Module-Class: com.example.asktester.AskPeripheral</p>

<p>Here my code :</p>

<p>public class ModuleLoader { </p>

<pre><code>private static List&lt;URL&gt; urls = new ArrayList&lt;URL&gt;(); 

private static List&lt;String&gt; getModuleClasses(String folder)
{ 
    List&lt;String&gt; classes = new ArrayList&lt;String&gt;(); 

    //we are listing the jar files
    File[] files = new File(folder).listFiles(new ModuleFilter()); 

    for(File f : files)
    { 
        JarFile jarFile = null; 

        try 
        { 
            //we open the jar file
            jarFile = new JarFile(f); 

            //we recover the manifest 
            Manifest manifest = jarFile.getManifest(); 

            //we recover the class
            String moduleClassName = manifest.getMainAttributes().getValue("Module-Class"); 

            classes.add(moduleClassName); 

            urls.add(f.toURI().toURL()); 
        }
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
        finally
        { 
            if(jarFile != null)
            { 
                try
                { 
                    jarFile.close(); 
                }
                catch (IOException e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
        } 
    } 

    return classes; 
} 

private static class ModuleFilter implements FileFilter { 
    @Override 
    public boolean accept(File file) { 
        return file.isFile() &amp;&amp; file.getName().toLowerCase().endsWith(".jar"); 
    } 
}

private static ClassLoader classLoader; 

public static List&lt;IPeripheral&gt; loadModules(String folder, Context CurrentContext) throws IOException, ClassNotFoundException
{ 
    List&lt;IPeripheral&gt; modules = new ArrayList&lt;IPeripheral&gt;(); 

    List&lt;String&gt; classes = getModuleClasses(folder);


    final File dexInternalStoragePath = new File(CurrentContext.getDir("dex", Context.MODE_PRIVATE),"ask.dex");

     File dexOutputDir = CurrentContext.getDir("dex", Context.MODE_PRIVATE);

     final File dexClasses = new File(CurrentContext.getDir("dex", Context.MODE_PRIVATE),"ASK.jar");
     DexFile dexFile = DexFile.loadDex(dexClasses.getAbsolutePath(), dexOutputDir.getAbsolutePath(), 0);




    DexClassLoader classLoader = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(),dexOutputDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
    //Class&lt;?&gt; myClass = classLoader.loadClass("com.example.asktester.AskPeripheral");



            if(IPeripheral.class.isAssignableFrom(myClass )){ 
                Class&lt;IPeripheral&gt; castedClass = (Class&lt;IPeripheral&gt;)myClass ; 

                IPeripheral module = castedClass.newInstance(); 

                modules.add(module); 
        }  
    }
        catch (ClassNotFoundException e1) 
        { 
            e1.printStackTrace(); 
        } 
        catch (InstantiationException e) 
        { 
            e.printStackTrace(); 
        }
        catch (IllegalAccessException e) 
        { 
            e.printStackTrace(); 
        } 
    } 

    return modules; 
}
</code></pre>
