是否可以在运行时从 Android 应用程序动态加载库?
有没有办法让Android应用程序在运行时下载并使用Java库?
下面是一个示例:
假设应用程序需要根据输入值进行一些计算。应用程序请求这些输入值,然后检查所需的Classe
或Method
是否可用。
如果没有,它会连接到服务器,下载所需的库,并在运行时加载它以使用反射技术调用所需的方法。实现可能会根据各种标准(例如下载库的用户)而改变。
Is there any way to make an Android application to download and use a Java library at runtime?
Here is an example:
Imagine that the application needs to make some calculations depending on the input values. The application asks for these input values and then checks if the required Classe
s or Method
s are available.
If not, it connects to a server, downloads the needed library, and loads it at runtime to calls the required methods using reflection techniques. The implementation could change depending on various criteria such as the user who is downloading the library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
抱歉,我迟到了,问题已经得到了接受的答案,但是是的,您可以下载并执行外部库。我是这样做的:
我想知道这是否可行,所以我编写了以下类:
并将其打包到 DEX 文件中,并将其保存在设备的 SD 卡上,名称为
/sdcard/shlublu.jar
。然后,在从 Eclipse 项目中删除 MyClass 并清理它之后,我编写了下面的“愚蠢的程序”:
它基本上以这种方式加载类
MyClass
:创建一个
DexClassLoader
用它来提取类
"/sdcard/shlublu.jar"
中的
MyClass
并将此类存储到应用程序的
“dex”
私有目录(手机的内部存储)。然后,它创建一个
MyClass
实例并在创建的实例上调用doSomething()
。它有效...我在 LogCat 中看到
MyClass
中定义的跟踪:我已经在模拟器 2.1 和我的实体 HTC 手机(运行 Android 2.2 并且未 root)上进行了尝试。
这意味着您可以创建外部 DEX 文件供应用程序下载和执行它们。这里它是用困难的方式制作的(丑陋的
Object
转换,Method.invoke()
丑陋的调用......),但它必须可以使用Interface
是为了让东西更干净。哇。我是第一个惊讶的。我期待着一个
SecurityException
。一些有助于调查更多信息的事实:
Sorry, I'm late and the question has already an accepted answer, but yes, you can download and execute external libraries. Here is the way I did:
I was wondering whether this was feasible so I wrote the following class:
And I packaged it in a DEX file that I saved on my device's SD card as
/sdcard/shlublu.jar
.Then I wrote the "stupid program" below, after having removed
MyClass
from my Eclipse project and cleaned it:It basically loads the class
MyClass
that way:create a
DexClassLoader
use it to extract the class
MyClass
from"/sdcard/shlublu.jar"
and store this class to the application's
"dex"
private directory (internal storage of the phone).Then, it creates an instance of
MyClass
and invokesdoSomething()
on the created instance.And it works... I see the traces defined in
MyClass
in my LogCat:I've tried on both an emulator 2.1 and on my physical HTC cellphone (which is running Android 2.2 and which is NOT rooted).
This means you can create external DEX files for the application to download and execute them. Here it was made the hard way (ugly
Object
casts,Method.invoke()
ugly calls...), but it must be possible to play withInterface
s to make something cleaner.Wow. I'm the first surprised. I was expecting a
SecurityException
.Some facts to help investigating more:
Shlublu 的 anwser 真的很好。不过,一些小事情会对初学者有所帮助:
“.NotExecutable”不是保留字。只是我必须在这里放一些东西)
其他步骤与 Shlublu 中描述的相同。
Shlublu's anwser is really nice. Some small things though that would help a beginner:
(".NotExecutable" is not a reserved word. It is just that I had to put something here)
Other steps are the same as described by Shlublu.
从技术上讲应该可行,但是谷歌的规则呢?
来自:play.google.com/intl/en-GB/about/developer-content-policy-pr int
Technically should work but what about Google rules?
From: play.google.com/intl/en-GB/about/developer-content-policy-print
我不确定是否可以通过动态加载java代码来实现这一点。也许你可以尝试在你的代码中嵌入一个脚本引擎,比如 rhino,它可以执行可以动态下载和更新的 java 脚本。
I am not sure if you can achieve this by dynamically loading java code. May be you can try embedding a script engine your code like rhino which can execute java scripts which can be dynamically downloaded and updated.
当然,这是可能的。未安装的apk可以被宿主android应用程序调用。一般来说,解析资源和activity的生命周期,然后,可以动态加载jar或apk。
详细请参考我在github上的开源研究: https ://github.com/singwhatiwanna/dynamic-load-apk/blob/master/README-en.md
另外,还需要DexClassLoader和反射,现在看一些关键代码:
你的需求只是其中的一部分开头提到的开源项目中的函数。
sure, it is possible. apk which is not installed can be invoked by host android application.generally,resolve resource and activity's lifecircle,then,can load jar or apk dynamically.
detail,please refer to my open source research on github: https://github.com/singwhatiwanna/dynamic-load-apk/blob/master/README-en.md
also,DexClassLoader and reflection is needed, now look at some key code:
your demands is only partly of function in the open source project mentioned at the begining.
如果您将 .DEX 文件保存在手机的外部存储器中,例如 SD 卡(不推荐!任何具有相同权限的应用程序都可以轻松覆盖您的类并执行代码注入攻击),请确保您已给出应用程序读取外部存储器的权限。如果是这种情况,抛出的异常是“ClassNotFound”,这是相当具有误导性的,请在清单中添加类似以下内容(请咨询 Google 以获取最新版本)。
If you're keeping your .DEX files in external memory on the phone, such as the SD card (not recommended! Any app with the same permissions can easily overwrite your class and perform a code injection attack) make sure you've given the app permission to read external memory. The exception that gets thrown if this is the case is 'ClassNotFound' which is quite misleading, put something like the following in your manifest (consult Google for most up to date version).
我认为@Shlublu 的答案是正确的,但我只是想强调一些关键点。
要从外部 jar 加载 UI,我们可以使用fragment。创建片段的实例并将其嵌入到活动中。但请确保片段动态创建 UI
如下所示。
I think @Shlublu answer is correct but i just want to highlight some key points.
To load the UI from external jar we can use fragment. Create the instance of the fragment and embedded it in the Activity. But make sure fragment creates the UI dynamically
as given below.