Programming for Android with Yeti

Posted on July 31, 2011

2


The last tow years I was on search for a new programming-language for the JVM. First I started with Groovy, did a bit Clojure finally used scala a lot. This way I learned to appreciate modern statically typing with type-inference and the productivity of functional abstractions. The only thing which I did not like with scala was its complexity. Therefore I was again searching another functional statically typed language and finally found yeti.

Yeti is a functional language for the JVM, which is statically typed with full type-inference, powerful functional abstractions and structural-typing therefore allows much shorter code than Java. In this respect it is similar to scala but much simpler both in the type-system and  in the language features. (The language-tutorial is quite short and can be found here: http://linux.ee/~mzz/yeti/intro.html)

By now I have done quite a bit of classical JVM programming with yeti and just wanted to test whether it also works for Android, because I thought it would be perfect for Android as well. Not just because it has the mentioned advantages over Java, but also because it has a smale language-api (about 177 KB) and because it is compiled and so quite fast.

In this post I want to show how to setup an Android project using ant to compile with yeti and in the end how to do the same thing with maven.

Prerequisites

1. If you don’t have it already, install the android-sdk and eclipse with the android plugin (http://developer.android.com/sdk/installing.html)

2. Download the yeti.jar to its own directory this includes the compiler http://linux.ee/~mzz/yeti/yeti.jar

3. Download the yeti-lib.jar this are only the runtime-classes without the compiler http://linux.ee/~mzz/yeti/yeti-lib.jar

(Alternatively you can also get the sources from github (git clone git://github.com/mth/yeti.git) and run ant for the yeti.jar and ant yeti-lib for the lib jar)

Create a new Eclipse project with ant support

1. In eclipse create a new Android Activity project. Exactly as described in the Android  “Hello World Tutorial: Create a New Android Project “: http://developer.android.com/resources/tutorials/hello-world.html#create

3. Run the newly created project from Eclipse (press the green run button and run as Android Activity)

2. Create an ant build.xml for your project: Open a command prompt and navigate to the base directory of you project and type android update project – path .

>android update project --path .
Updated local.properties
Added file c:\workspace\helloandroid\build.xml

If the android command is not found on the path then you need to update your path to include the Android tools. On windows at to the Path %ANDROID_HOME%/tools, where %ANDROID_HOME% points to the installation-directory of the android sdk.

Add yeti-lib.jar Update build.xml and add –post-compile target with the yeti compiler

1. Create in the base directory of your project a directory libs and copy the yeti-lib.jar there.

2. In eclipse right-click on the yeti-lib.jar and add it to the projects build-path.

3. modify the ant build.xml file to add the –post-compile target with the yeti compile task:


   <!-- insert following in build.xml before the already present <setup/> tag -->

    <!-- define the yeti compiler task -->
    <target name="yetic-def">
        <taskdef name="yetic" classname="yeti.lang.compiler.YetiTask"
                 classpath="C:/yeti/yeti.jar"/> <!-- path to where you downlaoded the yeti.jar -->
    </target>

    <!-- compile yeti sources after java sources have been compiled -->
    <target name="-post-compile" depends="yetic-def">
		<yetic srcdir="${source.absolute.dir}" destdir="${out.absolute.dir}"
			   includes="**/*.yeti"
			   preload="yeti/lang/std">
			<classpath>
				<path location="${out.absolute.dir}"/>
				<pathelement location="${android.jar}"/>
				<fileset dir="${native.libs.absolute.dir}" includes="*.jar"/>
			</classpath>
		</yetic>

    </target>

	<!-- continues original build.xml -->
	<setup />

</project>

Update the build configuration of the eclipse project to include the yeti compiler ant-task

In eclipse you can update the builders property to run the yeti compiler each time files are saved and modified. Because the yeti compiler is very fast this does not hinder your workflow.

1.) right-click on your project –> select properties –> choose Builders

2.) Create a new Ant Builder name it yetic

3.) in the first tab “Main” choose for “Buildfile:” the ant build.xml file we just have modified before, make sure the “Base Directory:” is ${workspace}/yourprojectname

4.) on the “Targets” tab choose in both “After a Clean” and “Auto Build” the –post-compile target from the build.xml

5.) press ok

Finally rewrite your  HelloAndroid.java file in yeti and run it

1.) delete the HelloAndroid.java file

2.) and rewrite the content of the HelloAndroid.java file in yeti: Create a file named “yetihello.yeti” in the same package as the HelloAndroid.java activity was and add the following content

module com.example.helloandroid.yetihello;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

class HelloAndroid extends Activity
	void onCreate(Bundle savedInstanceState)
		super#onCreate(savedInstanceState);
		tv = new TextView(this);
		tv#setText("Hello, Yeti on Android");
		this#setContentView(tv),

end;

()

3.) Press Run again for Hello Android

Using Maven to build Android Application

Finally I want to mention that you can also use maven-android-plugin together with the yeti-maven-plugin to build android applications using maven. All you have to do is setup a maven android project as described here  http://code.google.com/p/maven-android-plugin/ and add the yeti plugin as described here  https://github.com/chrisichris/yeti-maven-plugin. You should replace the dependency on yeti.jar with yeti-lib.jar and add the property yeti-full-jar=false in the yeti-plugin’s configuration section

<!-- yeti-lib dependency -->
<dependency>
	<groupId>org.yeti</groupId>
	<artifactId>yeti-lib</artifactId>
	<version>0.1-SNAPSHOT</version>
</dependency>

<!-- yeti plugin -->
<plugin>
	<groupId>org.yeti</groupId>
	<artifactId>yeti-maven-plugin</artifactId>
	<version>0.1-SNAPSHOT</version>
	<executions>
		<execution>
			<goals>
				<goal>compile</goal>
				<goal>testCompile</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<yeti-full-jar>false</yeti-full-jar>
	</configuration>
</plugin>
Posted in: Yeti