Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Eclipse's new Xtend language in my Android project?

I'd like to write Java classes in the Xtend language (simply because its way more terse), and have it compile back down into Java classes of which I can use in my Java project. Just like coffeescript. How can I do this?

I tried creating an Xtend file just as I would do with a new class, however I get this error:

Mandatory library bundle 'org.eclipse.xtext.xbase.lib' not found on the classpath.

This disables intellisense (autocompletion). Also, even if I do get that working, how can I have it compile to a Java class?

like image 918
nubela Avatar asked Nov 06 '11 11:11

nubela


People also ask

What is Xtend language?

Xtend is a statically-typed programming language which translates to comprehensible Java source code. Syntactically and semantically Xtend has its roots in the Java programming language but improves on many aspects: Extension methods - enhance closed types with new functionality.

What is Xtend in Java?

Xtend is a flexible and expressive dialect of Java, which compiles into readable Java 8 compatible source code. You can use any existing Java library seamlessly.

Why use Xtend?

Benefits of Xtend Xtend is one of the leading intra workout supplements on the market. It is packed with ingredients that will help you get the most out of your training, boosting your performance, energy, maximisuing protein synthesis and enhancing your recovery so you can get back in to the gym faster.


2 Answers

In Xtend inner classes are dereferenced using a dollar sign ('$') and static members are accessed using a double colon ('::').

The HelloAndroid activity code would look like this:

class XtendActivity extends Activity {

    override void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R$layout::main);
    }
}
like image 77
Sven Efftinge Avatar answered Sep 20 '22 04:09

Sven Efftinge


Having tried the same thing, I can confirm that enabling the Xtend Nature and adding the three Xtend libraries (mentioned earlier, 'org.eclipse.xtext.xtend2.lib', 'org.eclipse.xtext.xbase.lib' and 'com.google.inject') to the project's libraries makes the Xtend code compile, at least. However, I am also having trouble with the R class.

On closer inspection, it looks like the problem with the R class is not with it being located in a different directory. Copying the file to the main source dir with a different name doesn't change anything. Rather, it looks like the problem is with the R class being a static final class, containing several static final subclasses. If I create a simple plain-Java wrapper class that wraps a reference to R.layout.main (for example) inside a normal method, and call that from my Xtend code, then it does accept it and happily compiles.

After that, the next issue I came across was the Android compiler complaining about duplicate about.html and plugin.properties files in 'org.eclipse.xtext.xtend2.lib', 'org.eclipse.xtext.xbase.lib' and 'com.google.inject'. That is relatively easy to fix, by removing those files from two of the three .jar files. I'm not sure if it breaks anything later on, but now at least I have a working snippet of Xtend code running on the Android emulator.

like image 28
Nico de Poel Avatar answered Sep 20 '22 04:09

Nico de Poel