我尝试在Android中运行以下代码:
final GroovyClassLoader classLoader = new GroovyClassLoader();
final StringBuilder groovyScript = new StringBuilder();
groovyScript.append("class Sample {");
groovyScript.append("String sayIt(){\"Groovy says: Cool!\"}");
groovyScript.append("}");
Class groovy = classLoader.parseClass(groovyScript.toString());
GroovyObject groovyObj = null;
try {
groovyObj = (GroovyObject) groovy.newInstance();
} catch (InstantiationException ex) {
Logger.getLogger(MainApplication.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MainApplication.class.getName()).log(Level.SEVERE, null, ex);
}
String output = (String) groovyObj.invokeMethod("sayIt", null);
但不幸的是它引发了一个错误:
01-06 11:28:01.109 19597-19597/com.mscharhag.groovytodo E/AndroidRuntime: FATAL EXCEPTION: main
01-06 11:28:01.109 19597-19597/com.mscharhag.groovytodo E/AndroidRuntime: Process: com.mscharhag.groovytodo, PID: 19597
01-06 11:28:01.109 19597-19597/com.mscharhag.groovytodo E/AndroidRuntime: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
01-06 11:28:01.109 19597-19597/com.mscharhag.groovytodo E/AndroidRuntime: General error during class generation: can't load this type of class file
我试图在Android中运行 Groovy 而我几乎就在那里!顺便说一句,我遵循了本教程:Creating Android Apps with Groovy 2.4