在Android运行时执行Groovy脚本嵌入Java

前端之家收集整理的这篇文章主要介绍了在Android运行时执行Groovy脚本嵌入Java前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从2.4版开始,Groovy支持 Android.我想在我的 Android应用程序中运行Java中的Groovy脚本嵌入.

这个网站解释说这是可能的:http://melix.github.io/blog/2014/06/grooid2.html

One of the difficulties of adapting the Groovy language is that,as we
said,Groovy is a highly dynamic language. One of its capabilities is
executing scripts at runtime. So what if we wanted to type a Groovy
script on the device,and execute it directly on it? Is it possible?
In fact,yes,it is possible,given that you follow this process:

You can have an application,written in Groovy or not,but in the end,
once the application is running,you have Groovy source code that you
want to execute. Then it needs to compile the classes,call dex
directly on the device,package the result into a jar file on the
local filesystem,then load it using a special classloader. So why
this is possible,the process is very complex,not straightforward,
but more importantly,it is dramatically slow.

我很高兴,这的确是可以的,不管速度.这里有一个示例项目:https://github.com/melix/grooidshell-example.

不幸的是我不太明白这个过程.我有这个项目,这是相当小的,但我不知道究竟在哪里看.

你可以指出我在正确的方向执行Groovy脚本嵌入Java在运行时为Android?

解决方法

看更密切的 https://github.com/melix/grooidshell-example/blob/master/GroovyDroid/src/main/java/me/champeau/groovydroid/GrooidShell.java

这是创建一个DexFile(第71行)实例,将一个字节码后处理器添加到编译(第73行),以获取类的生成字节,并将其转换为dex格式(第76行),最后将其存储在dexfile实例77).然后获取dalvik字节码(第91行),使用DexClasLoader(第136行)加载dalvik / dex类(第138行).

猜你在找的Android相关文章