Android远程代码加载
我正在开发一个 Android 库,需要中央服务器频繁更新。 我在想如果我的库可以自我更新,或者如果我可以发布一个引导库,在安装应用程序时下载目标库,那该多好。
我在 1.5 中看到这个类称为“DexClassLoader”,但除了 API 文档之外,网络上似乎没有什么宝贵的信息。 有人在我描述的场景中成功使用过这个吗?
另外,Android Market 的条款允许这样的事情吗?
I am developing a library for Android that requires frequent updates from a central server. I was thinking how nice it would be if my library could update itself -- or if I could just release a bootstrap library that downloads the target library when the app is installed.
I see this class in 1.5 called "DexClassLoader" but there seems to be precious little on the web besides the API docs. Has anyone used this successfully for the scenario which I described?
Also, do the terms of the Android Market permit such a thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经成功使用了 DexClassLoader。 提供一个可由您的应用实际写入的
dexOutputDir
非常重要,因此不能/data/dalvik-cache
。 否则,日志将显示一两行有关写入失败的信息,后跟ClassNotFoundException
。要使 Class.forName() 工作,您可以尝试 Thread.setContextClassLoader() (我没有)。
I've successfully used DexClassLoader. It's important to provide a
dexOutputDir
that is actually writeable by your app, so not/data/dalvik-cache
. Otherwise the log will show one or two lines about failing to write there, followed byClassNotFoundException
.To make
Class.forName()
work, you could try Thread.setContextClassLoader() (I haven't).事实上,您想要的东西得到支持并且有效。 DexClassLoader 没有按我的预期工作,但以下代码工作正常。
关于市场问题,我认为这没有任何问题,但您必须阅读 EULA 才能确定。
Indeed what you want is supported and works. DexClassLoader is not working as expected for me, but the following code works fine.
About the market question, i don't see any issue with this, but you have to read the EULA to be sure.
DexClassLoader
是正确的答案。 应用程序永远不应该直接使用DexFile
(它应该由类加载器使用)。您可以将外部存储 (
/sdcard
) 或应用的私有数据区域用于dexOutputDir
参数。 外部存储通常较大,但如果卡被弹出,您的应用程序将被终止,并且由于缺乏文件权限强制执行,第三方很容易替换您的代码。 这可能允许恶意应用程序导致您的应用程序执行任意操作。 (如果您无论如何都想这样做,请通过Environment.getExternalStorageDirectory()
获取路径;需要WRITE_EXTERNAL_STORAGE
权限。)应用程序私有数据区域(从
Context.getFilesDir()
) 更安全,并且还具有在卸载应用程序时自动清理的优点。 这是推荐的方法。DexClassLoader
is the right answer. Applications should never useDexFile
directly (it's meant to be used by class loaders).You could use external storage (
/sdcard
), or the app's private data area, for thedexOutputDir
parameter. External storage is usually larger, but if the card is ejected your app will be killed, and due to the lack of file permission enforcement it's easy for a third party to replace your code. This can allow malicious apps to cause your app to perform arbitrary actions. (If you want to do it anyway, get the path viaEnvironment.getExternalStorageDirectory()
; requires theWRITE_EXTERNAL_STORAGE
permission.)The app-private data area (get the path from
Context.getFilesDir()
) is more secure, and also has the advantage of being cleaned up automatically if the app is uninstalled. This is the recommended approach.