Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a directory file and the ClassLoader for a libGDX Android game

I have a libGDX game project for Android, and I want to execute a Groovy script in it.

To do so, I am examining this example code: https://github.com/melix/grooidshell-example

They managed to execute Groovy embed in Java on Android. Particularly GrooidShell.java (https://github.com/melix/grooidshell-example/blob/master/GroovyDroid/src/main/java/me/champeau/groovydroid/GrooidShell.java)

I have managed to implement most of the code in the Android launcher of the libGDX project. However, I cannot run it because I am missing two arguments:

public GrooidShell(File tmpDir, ClassLoader parent) {

The first one can be any directory. And the second one, I don't even know what is it for.

My question is, what is the ClassLoader and File arguments supposed to be? I need to get them and use them in the AndroidLauncher class of libGDX, which is like this:

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(), config);
    }
}
like image 246
Voldemort Avatar asked Dec 10 '14 00:12

Voldemort


1 Answers

At first pay attention to GroovyActivity.groovy:

GrooidShell shell = new GrooidShell(applicationContext.getDir("dynclasses", 0), this.classLoader)

first argument of GrooidShell wants to create to a directory named "dynclasses" with default premission:

public abstract File getDir (String name, int mode)

Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.

Parameters

name Name of the directory to retrieve. This is a directory that is created as part of your application data. mode Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.

Returns

A File object for the requested directory. The directory will have been created if it does not already exist.

second argument this.classLoader refer to current running ClassLoader and you can use it as is or this.class.classLoader in groovy script. you also can use getApplicationContext().getClassLoader() in your activity java code.

getClassLoader()
Embedding Groovy

like image 193
01e Avatar answered Nov 14 '22 13:11

01e