Android-使用DexClassLoader加载apk文件 [英] Android- Using DexClassLoader to load apk file

查看:496
本文介绍了Android-使用DexClassLoader加载apk文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经打了一下墙壁。任何帮助将是AP preciated。我有,我想用DexClassLoader加载另一个apk文件的应用程序。

I've hit a bit of a wall. Any help would be appreciated. I have an app that I want to use DexClassLoader to load another apk file.

下面是我的code:

DexClassLoader dLoader = new DexClassLoader("/sdcard/download/test.apk","/sdcard/download",null,ClassLoader.getSystemClassLoader().getParent());
Class calledClass = dLoader.loadClass("com.test.classname");
Intent it=new Intent(this, calledClass);
it.setClassName("com.test", "com.test.classname");
startActivity(it);

现在我已经安装了test.apk所以,当我跑上面code这 正常工作和启动的应用程序。不过,我希望能够到 没有test.apk正在安装已经运行这个(因为这将 打败应用程序的整个点)。所以我卸载它, 当我再次跑到了我的应用我得到这个错误:

Now I had already installed test.apk so when I ran the above code it worked fine and launched the application. However I want to be able to run this without test.apk being installed already (as that would defeat the entire point of the application) . So I uninstalled it and when I ran the my app again I get this error:

android.content.ActivityNotFoundException: Unable to find explicit
activity class {com.test/com.test.classname}; have you declared this
activity in your AndroidManifest.xml.

所以我有点难倒在这里。这项活动是在清单中声明 在APK中我试图运行。我不能在我的应用程序中声明它 表现。任何想法?

So I'm a bit stumped here. This activity is declared in the Manifest of the apk I am trying to run. I can't declare it in my applications Manifest. Any ideas?

谢谢, 克雷格

推荐答案

尝试使用Android的PathClassLoader:

Try using Android's PathClassLoader:

    String packagePath = "com.mypackage";
    String classPath = "com.mypackage.ExternalClass";

    String apkName = null;
    try {
        apkName = getPackageManager().getApplicationInfo(packagePath,0).sourceDir;
    } catch (PackageManager.NameNotFoundException e) {
        // catch this
    }

    // add path to apk that contains classes you wish to load
    String extraApkPath = apkName + ":/path/to/extraLib.apk" 

    PathClassLoader pathClassLoader = new dalvik.system.PathClassLoader(
            apkName,
            ClassLoader.getSystemClassLoader());

    try {
        Class<?> handler = Class.forName(classPath, true, pathClassLoader);
    } catch (ClassNotFoundException e) {
        // catch this
    }

这篇关于Android-使用DexClassLoader加载apk文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆