如何在我的 Android 项目中使用 Eclipse 的新 Xtend 语言?

发布于 2024-12-13 19:46:48 字数 275 浏览 4 评论 0原文

我想用 Xtend 语言编写 Java 类(只是因为它更简洁),并将其编译回可以在我的 Java 项目中使用的 Java 类。就像咖啡脚本一样。我该怎么做?

我尝试像创建新类一样创建 Xtend 文件,但是出现此错误:

在类路径中找不到强制库包'org.eclipse.xtext.xbase.lib'

这会禁用智能感知(自动完成)。另外,即使我确实可以正常工作,我怎样才能将其编译为 Java 类呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

○闲身 2024-12-20 19:46:48

在 Xtend 中,内部类使用美元符号('$')取消引用,静态成员使用双冒号('::')访问。

HelloAndroid 活动代码如下所示:

class XtendActivity extends Activity {

    override void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R$layout::main);
    }
}

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);
    }
}
没有你我更好 2024-12-20 19:46:48

尝试过同样的事情后,我可以确认启用 Xtend Nature 并添加三个 Xtend 库(前面提到的“org.eclipse.xtext.xtend2.lib”、“org.eclipse.xtext.xbase.lib”和“com”) .google.inject') 到项目的库中至少可以编译 Xtend 代码。不过,我在R级也遇到了麻烦。

仔细检查后,看起来 R 类的问题不在于它位于不同的目录中。使用不同的名称将文件复制到主源目录不会改变任何内容。相反,问题似乎出在 R 类是一个静态最终类,包含多个静态最终子类。如果我创建一个简单的纯 Java 包装类,它将对 R.layout.main 的引用(例如)包装在普通方法内,并从我的 Xtend 代码中调用它,那么它会接受它并顺利编译。

之后,我遇到的下一个问题是 Android 编译器抱怨“org.eclipse.xtext.xtend2.lib”、“org.eclipse.xtext.xbase.lib”和“中存在重复的 about.html 和 plugin.properties 文件” com.google.inject'。通过从三个 .jar 文件中的两个文件中删除这些文件,解决这个问题相对容易。我不确定它以后是否会破坏任何东西,但现在至少我有一个在 Android 模拟器上运行的 Xtend 代码的工作片段。

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.

捶死心动 2024-12-20 19:46:48

我刚刚编译它,但目前无法导入“R”类。
执行以下步骤:

首先,在 Eclipse 更新管理器中安装 Xtend SDK,输入“http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/”,展开节点“TMF Xtext-2.1” .0”,然后选择“Xtend2 SDK”。等待,然后重新启动 Eclipse。

在您的 Eclipse Android 项目文件夹中,编辑文件“.project”(例如,通过在 OSX 上的终端中键入“nano .project”),然后更改它,使其与此类似(名称除外;您实际上可以将整个内容复制到并将名称更改回您的项目名称):

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TestAndroidXtend</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
    <buildCommand>
        <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>com.android.ide.eclipse.adt.ApkBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>
<natures>
    <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
    <nature>org.eclipse.jdt.core.javanature</nature>
    <nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
</natures>

然后将以下内容插入到文件“.classpath”的 classpath 元素中:

<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>

我将其放在 com.android.ide.eclipse.adt.ANDROID_FRAMEWORK 条目之前,也许您也应该这样做。

回到 Eclipse,使用 F5 刷新项目,然后在项目顶层创建一个文件夹“META-INF”,并创建一个空文件“MANIFEST.MF”。将以下内容粘贴到其中:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Xtend Tutorial
Bundle-SymbolicName: xtend.tutorial
Bundle-Version: 2.1.0.qualifier
Bundle-Vendor: Eclipse Modeling
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Require-Bundle: org.eclipse.xtext.xtend2.lib;bundle-version="2.0.0",
 org.junit4,
 org.aopalliance;bundle-version="1.0.0"

执行“项目”-> “清理”您的项目,您现在可以创建和使用 Xtend 类。

如前所述,我无法导入 R 类,也许 Xtend 只是在普通的“src”文件夹中查找类文件。

I just got it to compile, though I cannot import the "R" class, for now.
Execute these steps:

First, install the Xtend SDK in the Eclipse update manager, by entering "http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/", expanding node "TMF Xtext-2.1.0", and selecting "Xtend2 SDK". Wait, then restart Eclipse.

In your Eclipse Android project folder, edit file ".project" (e.g. by typing "nano .project" in Terminal on OSX), and change it so that it resembles this (except for the name; you can actually copy the whole content over and change the name back to your project name):

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TestAndroidXtend</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
    <buildCommand>
        <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>com.android.ide.eclipse.adt.ApkBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>
<natures>
    <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
    <nature>org.eclipse.jdt.core.javanature</nature>
    <nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
</natures>

Then insert the following into file ".classpath", within the classpath element:

<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>

I put it before the entry with com.android.ide.eclipse.adt.ANDROID_FRAMEWORK, perhaps you should do that too.

Back in Eclipse, refresh the project with F5, then create a folder "META-INF" at the toplevel of the project, and create an empty file "MANIFEST.MF". Paste into it these contents:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Xtend Tutorial
Bundle-SymbolicName: xtend.tutorial
Bundle-Version: 2.1.0.qualifier
Bundle-Vendor: Eclipse Modeling
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Require-Bundle: org.eclipse.xtext.xtend2.lib;bundle-version="2.0.0",
 org.junit4,
 org.aopalliance;bundle-version="1.0.0"

Perform a "Project" -> "Clean" on your project, and you can now create and use Xtend classes.

As mentioned before, I cannot import the R class, perhaps Xtend just looks in the normal "src" folder for class files.

挖鼻大婶 2024-12-20 19:46:48

到目前为止我还没有做过任何Android开发。但 Xtend 代码是针对精简运行时库(用 Java 编写)进行编译的。它主要只是三个罐子:
- org.eclipse.xtext.xtend2.lib
- org.eclipse.xtext.xbase.lib
- com.google.collect

它们都安装在 eclipse 中,因此您应该在安装的插件文件夹中找到它们。我们通常使用 OSGI 和 PDE 将它们放在类路径中,但这实际上只是类路径依赖项。

Xtend 可以像 Java 一样导入类路径上的所有内容。它重用了 Java 项目配置以及 Java 开发工具。因此,只要类路径上有“R”类,它就应该可以工作。如果您无法引用它,请在 bugs.eclipse.org(位于 Modeling/TMF/Xtext 下)提交 bugzilla,尽管您可以从位于 Xtend 文件旁边的 Java 引用它。并提供一个小例子和一些额外的解释。

I haven't done any Android development so far. But Xtend code is compiled against a thin runtime library (written in Java). It's mainly just three jars:
- org.eclipse.xtext.xtend2.lib
- org.eclipse.xtext.xbase.lib
- com.google.collect

They are all installed in eclipse, so you should find them in the plugins folder of your installation. We usually use OSGI and PDE to have them on the classpath, but it's really just a classpath dependency.

Xtend can import everything which is on the classpath just like Java. It reuses the Java project configuration and also the Java Development Tools. So as long as you have the "R" class on the class path it should work. Please file a bugzilla at bugs.eclipse.org (under Modeling/TMF/Xtext) If you can't reference it although you can from a Java which sits next to the Xtend file. And provide a small example and some additional explanation.

沩ん囻菔务 2024-12-20 19:46:48

这是另一个问题:确保您的参数名称不与 R.java 中的任何内容冲突。

我使用向导创建了一个(Java)活动,复制了代码,删除了该活动,并在其位置创建了一个 MainActivity.xtend,如下所示:


public class MainActivity extends Activity {

    override void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R$layout::activity_main);
    }

    override boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R$menu::activity_main, menu);
        return true;
    }

它在 .xtend 文件中看起来很好,但它不会构建,因为生成的Java 代码

  public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater _menuInflater = this.getMenuInflater();
    _menuInflater.inflate(menu.activity_main, menu);
    return true;
  }

目前不喜欢 Activity_main。

我花了几分钟才发现“menu”参数覆盖了 R.menu.activity_main。
一旦我将 .xtend 的“menu”参数更改为“optionsMenu”,它就可以正常工作。

Here's another gotcha: Make sure your parameter names don't conflict with anything in R.java.

I created a (Java) Activity using the wizard, copied the code, deleted the Activity, and created a MainActivity.xtend in its place, like so:


public class MainActivity extends Activity {

    override void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R$layout::activity_main);
    }

    override boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R$menu::activity_main, menu);
        return true;
    }

It looks fine in the .xtend file, but it doesn't build because the generated Java code

  public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater _menuInflater = this.getMenuInflater();
    _menuInflater.inflate(menu.activity_main, menu);
    return true;
  }

doesn't like activity_main at this point.

It took me a few minutes to figure out that the "menu" parameter was overriding R.menu.activity_main.
Once I changed the .xtend's "menu" parameter to "optionsMenu", it worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文