Try PandaPow for Android

So you have the names of a bunch of classes, how can you load them? Here's the code (needs to be in an activity to make use of getPackageManager()):

    String packageName = "com.example.mypackage";
    String className = "com.example.mypackage.MyClass";

    String apkName = getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
    PathClassLoader myClassLoader =
        new dalvik.system.PathClassLoader(
                    apkName,
                ClassLoader.getSystemClassLoader());
    Class<?> handler = Class.forName(className, true, myClassLoader);

You can combine several apk-files in the string apkName by separating them with a ":". Pretty simple, huh :)

Thanks to Josh for his blog post on Dynamic Loading. His example has a problem though, as he assumes the apk-file is located in /data/app/PACKAGENAME.apk. That may or may not be true (it wasn't for me), so better to use the PackageManager (as above).

Comments:


Page: 1



Please login to post a reply.