[Android Studio] androidmaniac2014:
Pls Suggest method to load libraries (.aar) dynamically in android java application.

I have Tried below:

File file = Environment.getExternalStorageDirectory();

File jarFile = new File(file.getAbsolutePath()+File.separator + "myjarlib.jar");

dalvik.system.PathClassLoader myClassLoader = new dalvik.system.PathClassLoader("/storage/emulated/0/myjarlib.jar", ModuleLoader.class.getClassLoader());

myClassLoader.findLibrary("myjarliib.jar");

Enumeration<URL> en = myClassLoader.getResources("META-INF");

//Enumeration<URL> en = getClass().getClassLoader().getResources("META-INF");

List<String> profiles = new ArrayList<>();

while (en.hasMoreElements()) {

URL url = en.nextElement();

JarURLConnection urlcon = (JarURLConnection) (url.openConnection());

try (JarFile jar = urlcon.getJarFile();) {

Enumeration<JarEntry> entries = jar.entries();

while (entries.hasMoreElements()) {

String entry = entries.nextElement().getName();

System.out.println(entry);

Log.d("tag" ,entry );

}

}

}

Class classToLoad = Class.forName("MyClass.class", true, myClassLoader); -----> I am getting ClassNotFound exception here. Pls suggest .

ModuleLoader.java

***************************************************************************************************************************************

package com.tel.myapplication;

import java.io.File;

import java.io.FileFilter;

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.net.URL;

import java.net.URLClassLoader;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.jar.JarFile;

import java.util.jar.Manifest;

import android.annotation.SuppressLint;

import android.content.Context;

import com.tel.myapplication.IPeripheral;

public class ModuleLoader {

private static List<URL> urls = new ArrayList<URL>();

// to retrieve the unknown list of jar files contained in the directory folder

// in my case it was in the SDCard folder

// link to create a SDCard directory on the Eclipse emulator

// http://blog.lecacheur.com/2010/01/14/android-avoir-acces-a-une-carte-memoire-dans-lemulateur/

// retrieve the classes of all this jar files and their URL (location)

private static List<String> getModuleClasses(String folder)

{

List<String> classes = new ArrayList<String>();

//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 name of our peripherals thanks to ours manifest

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() && file.getName().toLowerCase().endsWith(".jar");

}

}

//This function loads the jar file into the dalvik system

// retrieves the associated classes using its name

// and try to know if the loaded classes are implementing our interface

public static List<IPeripheral> loadModules(String folder, Context CurrentContext) {

List<IPeripheral> modules = new ArrayList<IPeripheral>();

List<String> classes = getModuleClasses(folder);

int index = 0;

for(String c : classes)

{

try

{

dalvik.system.PathClassLoader myClassLoader = new dalvik.system.PathClassLoader(urls.get(index).toString(), ModuleLoader.class.getClassLoader());

Class<?> moduleClass = Class.forName(c, true, myClassLoader);

//check and cast to an interface, then use it

if(IPeripheral.class.isAssignableFrom(moduleClass))

{

@SuppressWarnings("unused")

Class<IPeripheral> castedClass = (Class<IPeripheral>)moduleClass;

IPeripheral module = (IPeripheral)moduleClass.newInstance();

modules.add(module);

}

index++;

}

catch (ClassNotFoundException e1)

{

e1.printStackTrace();

}

catch (InstantiationException e)

{

e.printStackTrace();

}

catch (IllegalAccessException e)

{

e.printStackTrace();

}

}

return modules;

}

}

IPeripheral

package com.tel.myapplication;

public interface IPeripheral {

public boolean Initialize();

public boolean configure();

public boolean execute();

public String GetName();

}