动态加载使用资源的apk [英] Dynamically load apk that uses resources

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

问题描述

我有 service.apk 正在使用资源在状态栏中显示通知.

I have service.apk that is using resources to show a notification in the status bar.

我正在动态加载使用这些资源的类,但随后通知的资源不知何故消失了,我收到此错误:

I am dynamically loading the class that is using these resources but then the resources for the notifications disappear somehow, and I'm getting this error:

W/ResourceType(1142): 获取资源编号 0x7f020001 的值时没有已知包W/StatusBarIconView(1142):在 2130837505 中找不到图标:7f020001W/StatusBarIconView(1142): 插槽 android/0x20 没有图标W/ActivityManager(941):crashApplication:试图让自己崩溃!D/NotificationService(941):onNotification 错误 pkg=android tag=null id=32;将崩溃应用程序(uid=1000,pid=941)W/StatusBar(1142): removeNotification for unknown key: android.os.BinderProxy@41b166b8

动态加载的代码:

PathClassLoader classLoader =
                new PathClassLoader("system/app/ServiceImpl.apk",
                               ClassLoader.getSystemClassLoader());
              Class someClass =
            classLoader.loadClass("com.bla.ServiceImpl");
              Constructor<Class> ctor = someClass.getConstructor(Context.class);
              Object someObject = ctor.newInstance(context);

我在想也许我加载 apk 的方式是错误的?也许我应该为资源添加路径?

I'm thinking maybe the way I'm loading the apk is wrong? Maybe I should add a path for the resources?

非常感谢任何帮助!

推荐答案

可以参考以下开源项目:https://github.com/singwhatiwanna/dynamic-load-apk/blob/master/README-en.md

you can refer to the following open source project: https://github.com/singwhatiwanna/dynamic-load-apk/blob/master/README-en.md

以下是关键思想:首先,apk 可以由主机 android 应用程序加载,忽略它是在 sdcard 或系统目录上.但是我们怎么做呢?需要解决两个问题:资源和活动的生命周期.上述项目解决了这两个问题,现在,动态加载使用资源的apk成为可能,使用DexClassLoader并提供新的AssetManager可以满足您的需求.

following is the key thinking: first, apk can be loaded by host android application, ignored that it is on sdcard or on system directory. but how can we do this? two problems need to be resolved: resource and activity's lifecircle. the above project resolves this two problems, now, Dynamically load apk that uses resources is possible, use DexClassLoader and provide new AssetManager can satisfy your demand.

protected void loadResources() {
    try {
        AssetManager assetManager = AssetManager.class.newInstance();
        Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
        addAssetPath.invoke(assetManager, mDexPath);
        mAssetManager = assetManager;
    } catch (Exception e) {
        e.printStackTrace();
    }
    Resources superRes = super.getResources();
    mResources = new Resources(mAssetManager, superRes.getDisplayMetrics(),
            superRes.getConfiguration());
    mTheme = mResources.newTheme();
    mTheme.setTo(super.getTheme());
}

那么,资源就可以通过R标识符访问了,使用getResource.

then, resources can be visited by R identifer, use getResource.

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

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