onsdag 12 augusti 2009

How to perform dynamic compilation on Android with Groovy.

First of all, a small disclaimer.
Discobot is quite early in its development so expect things to break. And when they do please send me an email or post a comment on this blog so that the bugs can be fixed. Also windows users have to work a bit harder as discobot is untested there. It should however be trivial to convert the bashscripts into bat files, other than that everything is pretty cross platform.
And now on to the howto...

Start with getting your hands on the latest and greatest version of discobot. At the time of writing it's discobot-0.3 You also need to have ant, groovy and android 1.5 installed. Untar the tar.gz file and navigate to the discobot root. In there you will find a file called project.conf set the android-folder property to the absolute path to the android sdk folder, for example:

Now enter the discobot folder with your favorite console. And run the command /discobot$ ./merge.sh clean This command copies the groovy-sourcetree into the mixer folder then copies the discobot patches to the same folder. When the trees are merged groovy is compiled and a discobot.jar file is put into the dist directory.

To create a discobot project, ie the actual android application, you need to run the mkProject script. This script takes 2 parameters one is -path which is used to determine where the project files should be put. Note that that the project files is put directly into this folder.
The other parameter is -name which specifies the application name and classpath. This parameter uses the format classpath.ApplicationName, make sure that the application name starts with a uppercase letter as it will be used as the name for a source file.
/discobot$ ./mkProject.sh -path ../classloadExample -name org.discobot.classloadExample.ClassloadExample

Put the following code into the file located at

package org.discobot.classloadExample

import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import groovy.lang.GroovyShell
import org.discobot.util.DiscobotClassLoader

public class ClassloadExample extends Activity{

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)

//setup the shell and binding
def binding = new Binding()
binding.setVariable("number", 123)
def shell = new GroovyShell(new DiscobotClassLoader(this), binding)

//Ask the shell to compile and run a small script for us
def newValue = shell.evaluate("return number * 10 + 4")

//Finally, display our results
def str = "The number went from ${binding.getVariable("number")} to ${newValue}"
def tv = new TextView(this)

Save the file and run ant from the project root (remember to tell ant to include discobot.jar).

/classloadExample$ ant -lib libs/discobot.jar

Now install the generated package on your emulator as usual. Make sure the emulator is started before installing your apk.

/classloadExample$ adb install bin/ClassloadExample-debug.apk

Your program should now slowly be installed (discobot is huge!) on your device and a icon for it should be pop up among the other applications eventually. When you run the program there will be a period of about 15 seconds when the screen only shows the application name, this is due to the rather long boot time of groovy. But once everything is loaded the application should display the text "The number went from 123 to 1234".

torsdag 23 juli 2009

The second release of discobot is out!

I have put together the second release of discobot and there are quite a some news:
  1. It now can use the groovy 1.7 code base so there is no need for the old discobot project based on groovy 1.6.3.
  2. The project now comes with a tool to generate android project directories complete with buildscripts et al.
  3. The generated jarfile is slightly smaller thanks some additional removed parts of groovy.
Now things are still under development so its not 100% certain that things will work as they should. And I have only tested things under linux so getting things to work under windows might require some hackery.
Also its not possible to run the unit tests after building groovy since a lot of files are currently missing from the source tree. This will be fixed in the next release.

Now to the usage guide:
To install you simply need to untar this file into a folder and make sure you have groovy, android sdk 1.1 and ant installed (dont untar into the same folder as the old discobot project).
When the file is untared you should be able to find a file named project.conf in the root directory
edit that file so that the specified path points to your android sdk folder.
now run ./merge.sh clean from the root folder. This will produce the discobot.jar file under the dist folder.
To generate a discobot project run:
./mkProject.sh -name org.testorg.test.Test -path ../testapp
The above command will create a project folder called testapp in the parent directory (the relevant files will be put into the root of this directory) and put a file called Test.groovy in the name space org.testorg.test. This is your programs entry point and contains a rather weak attempt to show off groovys features :)
It will also put a copy of the discobot jarfile under ../testapp/libs. To build the project run: ant -lib libs/discobot.jar and install the apk as usual.

fredag 10 juli 2009

Howto run groovy on android.

Its not super simple to run groovy on android yet but its certainly getting there. What you need is
this package and some patience. First of all edit the default.properties file so that sdk-folder points to your install if the android 1.1 sdk for example "sdk-folder=/home/hjalle/arbeten/gsoc/android-sdk-linux_x86-1.1_r1".
Now run "ant -lib ./libs/discobot.jar" from the command line to build the application this will result in a apk file in your projects bin directory.
Simply install it with the command "adb install bin/Hello-debug.apk" and run it from your emulator!
Theres a few caveats tho, this is a debuggy/hackish build of groovy so it spews out quite a bit of debug messages and theres no dynamic compilation yet.

If your interested in fiddling around a bit more you can download this tarfile which conatains a slightly more ambitious project. It is based on groovy 1.7 beta and contains a copy of that source tree inside the groovy-core folder. Inside the discobot-patches folder resides all the files that need to be modified for groovy to work on android.
And inside the libfolder there is a script called main.groovy that verfies all the patches (makes sure the original files have the correct CRC) and merges the two sourcetrees into the mixerfolder.
This is still a work in progress. Theres still no way of keeping track of which files belong to what fix but that is on the todo list along with moving as many of the patches as possible to retrotranslator.
And it doesnt run on android yet since groovy has evolved quite a bit since 1.6.3 and theres quite a few new problems to solve.

Note: Theres now a google code project @ http://code.google.com/p/discobot/ the files are downloadable from there aswell