Running LibGdx (as a Clojure game) in Android

This was a fun experience, after what seemed like too many hours wasted I have my clojure libGdx example running in Android built from Eclipse.

Stay tuned for a more indepth description and steps, but for now here are the quick details to get you going:

Make sure your project.clj reflects:
:dependencies [[org.clojure/clojure "1.5.1"]
[com.badlogicgames.gdx/gdx "0.9.9-SNAPSHOT"]]

Notice it does not have any backend Gdx libraries specified!

(see this post about the new repo location)

In your core.clj make sure these changes are made:

(ns libgdxexample.core
  (:import (com.badlogic.gdx ApplicationListener Gdx Graphics)
           (com.badlogic.gdx.graphics GL10 Color Camera PerspectiveCamera)
           ;(com.badlogic.gdx.backends.lwjgl LwjglApplication) ;we took this out! this is handled in Android as you'll soon see
           (com.badlogic.gdx.assets AssetManager)
           (com.badlogic.gdx.graphics.g3d Model ModelBatch ModelInstance)
           (com.badlogic.gdx.graphics.g3d.lights Lights DirectionalLight)
           (com.badlogic.gdx.graphics.g3d.utils CameraInputController))
 
(:gen-class
    :implements [com.badlogic.gdx.ApplicationListener]
    :methods [#^{:static true} [applistener [] com.badlogic.gdx.ApplicationListener]]))

Notice that I changed the app-listener defn to applistener, this is important. We also took out any backend libraries, just include the Gdx core itself in clojure.

Change your app-listener function to have no hyphen and include an external calling flag for java-interop:

(defn -applistener []

Final piece to this, change your main function to not call out the ‘app’ function we have originally defined in the last example post. In addition to this, comment out your app function!

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  ;; work around dangerous default behaviour in Clojure
  (alter-var-root #'*read-eval* (constantly false)))

Run lein uberjar and copy the standalone to your android project’s lib folder.
Download the libGdx nightly zip to get the android backend and Arm libraries: http://libgdx.badlogicgames.com/nightlies/
Copy both arm folders and the gdx-android-backend.jar into the lib folder for your android app.
Click properties and click Java Build Path, then Libraries tab: click Add Jars and select the 2 jars (your standalone build and the gdx backend). Click on Order tab and make sure they are checked off, I put them in order as well with standalone and then classes of your android app at the end of the list, just under gdx backend.

In your main activity import the backend library and refactor the mainactivity class as follows:

import com.badlogic.gdx.backends.android.AndroidApplication;
 
public class MainActivity extends AndroidApplication {
 
	@Override
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initialize(core.applistener(), false);
		//setContentView(R.layout.activity_main);
	}
}

Last step: Copy your resources folder which contains the model into the assets folder (keep the resource folder as a subfolder)

Good luck! I’ll be back with more indepth information and maybe screens! I’ll also soon have a Github clone link for you to just download and run which will save you time I’m sure.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current month ye@r day *