问题描述:

I tried to run the following code in 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);

But unfortunately it throws an error:

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

I was trying to run Groovy in Android and Im almost there! By the way I followed this tutorial: Creating Android Apps with Groovy 2.4

相关阅读:
Top