从其他APK动态加载资源
我正在考虑从其他APK加载资源的某种方式。例如,我在SDCARD上有一个APK(它只是APK,它不安装在手机存储器中),它包含我要在安装的应用程序中使用的资源。是否可以从存储在SDCARD上的APK到我的已安装应用程序(例如布局,图像,字符串等)加载资源。
谢谢
I am thinking about some way of loading resources from other APKs. For example, I have an APK (it is just APK, it is not installed in phone memory) on sdcard, which contains resources I want to use in my installed application. Is it possible to load resources from res/x from APK stored on the sdcard to my installed application (e.g. layout, images, strings, ...).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在阅读和调试一些 AOSP 代码后,我终于成功地动态加载资源。分步指南:
创建一个新的 Android 项目,我将其命名为“动态 APK 加载”,指定
whatever.dynamicapkloading
应用 ID,并留下“应用”模块名称。< /p>创建另一个没有任何 Activity 的 Android 应用程序模块。我已经在同一个项目中完成了此操作,但这取决于您。我将模块命名为“dynamic”,并将应用程序 ID 设置为
whatever.dynamic
。从“动态”项目中删除所有不必要的内容。我已经删除了启动器绘图以及任何其他资源,并且还从 build.gradle 中删除了 AppCompat 依赖项。我的清单内容看起来很简约,如下所示:
。向“动态”项目添加一些代码。例如:
向“动态”项目添加一些资源。我创建了
strings.xml
:将其添加到运行配置中。将启动选项/启动设置为无。构建->构建APK!在此步骤中,我获得了名为
dynamic-debug.apk
的 4.9 KB 文件。将此文件
dynamic/build/outputs/apk/dynamic-debug.apk
移动到我们主项目的资产中,即app/src/main/assets/
>.创建/打开一个将加载代码和代码的类资源。比方说,这将是 DynamicActivity。
编写代码,将 APK 从资产复制到私有应用程序目录。这很无聊&微不足道,你知道:
编写将加载必要的类并实例化它的代码。
编写将从指定 APK 加载资源的代码。这太难了!所有构造函数和方法都是公共的,但它们是隐藏的。我以反射的方式编写了它,但为了避免这种情况,您可以根据完整的框架 jar 编译代码或在 Smali 中编写部分代码。
使用这些资源!有两种获取资源 ID 的方法。
就是这样!这对我有用。 完整的 DynamicActivity.java 代码
在实际项目中,您必须在构建时签署 APK 并检查其加载时签名。当然,切勿将其存储在 SD 卡上,否则您的 APK 可能会被欺骗!
您还应该静态缓存资源 ID,但在配置更改时重新创建资源并重新查询资源值。
有用的链接:
After reading and debugging some AOSP code, I've finally managed to load resources dynamically. Step-by-step guide:
Create a new Android project, I've called it 'Dynamic APK Loading', specified
whatever.dynamicapkloading
app ID, and left 'app' module name.Create another Android application module without any Activities. I've done this in the same project, but this is up to you. I've called module 'dynamic', and set application ID to
whatever.dynamic
.Remove all unnecessary things from 'dynamic' project. I've removed launcher drawables along with any other resources, and also removed AppCompat depencency from build.gradle. My manifest content looked minimalistic, like this:
<application />
.Add some code to 'dynamic' project. For example:
Add some resources to 'dynamic' project. I've created
strings.xml
:Add it to Run configurations. Set Launch Options / Launch to Nothing. Build -> Build APK! On this step I've got 4.9 KB file called
dynamic-debug.apk
.Move this file,
dynamic/build/outputs/apk/dynamic-debug.apk
, into our main project's assets, i. e. toapp/src/main/assets/
.Create/open a class which will load code & resources. Say, this will be DynamicActivity.
Write code which will copy APK from assets to private app directory. It's boring & trivial, you know:
Write code which will load necessary class and instantiate it.
Write code which will load resources from the specified APK. This was a hard one! All constructors and methods are public, but they are hidden. I've written it in reflective way, but to avoid this you can either compile your code against full framework jar or write a part of code in Smali.
Use these resources! There are two ways of getting resource IDs.
That's it! This worked for me. Full DynamicActivity.java code
In real-world projects you must sign APK while building and check its signature while loading. And, of course, never store it on sdcard, otherwise your APK may be spoofed!
You should also cache resource IDs statically, but re-create
Resources
and re-query resource values when configuration gets changed.Useful links:
您可以使用 PackageManager.getResourcesForApplication< /a> 访问在不同 apk 中找到的资源。
You can use PackageManager.getResourcesForApplication to access resources found in a different apk.