2

My URLClassLoader implementation seems to only work on pc, but not on Android.

try {

        Class<?> loadedClass = new URLClassLoader(
                new URL[] { new File("http://10.0.2.2/myphpfile.php").toURI().toURL() })
                .loadClass("MyClass");

        IInterface inter = (IInterface) loadedClass.getConstructor(
                String.class).newInstance("Hello");
        if (inter != null)
            System.out.println(inter.doSomething(2));
    } catch (Exception e) {
        e.printStackTrace();
    }

It just works fine, but on android I´m getting

java.lang.ClassNotFoundException: MyClass

Internet Permission is set.

Does anyone have an idea how I can get it working?

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
  • checkout sample code in this project: https://github.com/gelldur/msc-thesis/tree/master/project/SecureMe/app/src/main/java/drozd/dawid/dexode/com/secureme – Gelldur Oct 28 '17 at 19:33

1 Answers1

4

Use DexClassLoader instead of URLClassLoader.

First you need your jar file to include a classes.dex, there's this answer:

dx --dex --output="JarProjectWithDex.jar" "JarProject.jar"

And then you simply replace your URLClassLoader with a DexClassLoader and continue the same way:

File jarFile = new File("/data/JarProjectWithDex.jar");
DexClassLoader loader = new DexClassLoader(jarFile.getPath(), getFilesDir().getPath(), null, this.getClassLoader());
Class<?> classToLoad = Class.forName("zordon.MegaZords", true, loader);
Community
  • 1
  • 1
Morad
  • 734
  • 5
  • 14