在 Dalvik VM(Android 的 VM)上,您不能在 Sun VM 中做什么?
我知道你可以在 Dalvik 的 VM 中运行几乎所有的 Java,而你可以在 Java 的 VM 中运行,但是限制不是很清楚.有没有人遇到任何主要的绊脚石?有什么大图书馆有问题吗?任何编译成 Java 字节码的语言(Scala、Jython 等)都不能按预期工作?
I know that you can run almost all Java in Dalvik's VM that you can in Java's VM but the limitations are not very clear. Has anyone run into any major stumbling blocks? Any major libraries having trouble? Any languages that compile to Java byte code (Scala, Jython etc...) not work as expected?
推荐答案
Dalvik 不会处理或不会以与标准 Java 字节码完全相同的方式处理许多事情,尽管其中大多数都非常先进.
There is a number of things that Dalvik will not handle or will not handle quite the same way as standard Java bytecode, though most of them are quite advanced.
最严重的例子是运行时字节码生成和自定义类加载.假设您想创建一些字节码,然后使用类加载器为您加载它,如果该技巧在您的普通机器上有效,则保证在 Dalvik 上无效,除非您更改字节码生成.
The most severe example is runtime bytecode generation and custom class loading. Let's say you would like to create some bytecode and then use classloader to load it for you, if that trick works on your normal machine, it is guaranteed to not work on Dalvik, unless you change your bytecode generation.
这会阻止您使用某些依赖注入框架,最著名的例子是 Google Guice(尽管我相信有些人在做这方面的工作).另一方面,AspectJ 应该可以工作,因为它使用字节码检测作为编译步骤(尽管我不知道是否有人尝试过).
That prevents you from using certain dependency injection frameworks, most known example being Google Guice (though I am sure some people work on that). On the other hand AspectJ should work as it uses bytecode instrumentation as a compilation step (though I don't know if anyone tried).
至于其他 jvm 语言——最终编译为标准字节码并且在运行时不使用字节码检测的任何东西都可以转换为 Dalvik 并且应该可以工作.我知道人们确实在 Android 上运行过 Jython,而且运行良好.
As to other jvm languages -- anything that in the end compiles to standard bytecode and does not use bytecode instrumentation at runtime can be converted to Dalvik and should work. I know people did run Jython on Android and it worked ok.
需要注意的另一件事是没有及时编译.这不是严格意义上的 Dalvik 问题(如果你愿意,你可以随时编译任何字节码)但是 Android 不支持并且不太可能这样做.实际上,虽然标准 Java 的微基准测试毫无用处——组件在测试中的运行时特性与作为大型系统的一部分不同——但 Android 手机的微基准测试完全有意义.
Other thing to be aware of is that there is no just in time compilation. This is not strictly Dalviks problem (you can always compile any bytecode on the fly if you wish) but that Android does not support that and is unlikely to do so. In the effect while microbenchmarking for standard Java was useless -- components had different runtime characterstics in tests than as parts of larger systems -- microbenchmarks for Android phones totally make sense.
相关文章