How to load a Java class dynamically on android/dalvik? How to load a Java class dynamically on android/dalvik? android android

How to load a Java class dynamically on android/dalvik?


There's an example of DexClassLoader in the Dalvik test suite. It accesses the classloader reflectively, but if you're building against the Android SDK you can just do this:

String jarFile = "path/to/jarfile.jar";DexClassLoader classLoader = new DexClassLoader(    jarFile, "/tmp", null, getClass().getClassLoader());Class<?> myClass = classLoader.loadClass("MyClass");

For this to work, the jar file should contain an entry named classes.dex. You can create such a jar with the dx tool that ships with your SDK.