爪哇(Eclipse中) - 条件编译 [英] Java (Eclipse) - Conditional compilation

查看:457
本文介绍了爪哇(Eclipse中) - 条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在J2ME项目和Android项目引用的Java项目。 在这个项目中,我想使用条件编译。

I have a java project that is referenced in j2me project and in android project. In this project i would like to use conditional compilation.

喜欢的东西...

//#if android
...
//#endif

//if j2me
...
//#endif

我一直在阅读关于这一点,但我没有找到什么有用的东西呢。

I have been reading about this but i did not find anything useful yet.

推荐答案

您可以使用天线(有一的Eclipse插件,你可以将它用于Ant构建系统)。 我在你所描述的方式使用它在我的项目,它完美的作品:)

You could use Antenna (there is a plugin for Eclipse, and you can use it with the Ant build system). I'm using it in my projects in a way you've described and it works perfectly :)

编辑:这是与@ WhiteFang34解决方案,这是一个路要走例如:

here is the example related to @WhiteFang34 solution that is a way to go:

在你的核心项目:

//base class Base.java
public abstract class Base {
    public static Base getInstance() 
    {
        //#ifdef ANDROID
        return new AndroidBaseImpl();
        //#elif J2ME
        return new J2MEBaseImpl();
        //#endif
    }

    public abstract void doSomething();
}

//Android specific implementation AndroidBaseImpl.java
//#ifdef ANDROID
public class AndroidBaseImpl extends Base {
    public void doSomething() {
     //Android code
    }
}
//#endif

//J2ME specific implementation J2MEBaseImpl.java
//#ifdef J2ME
public class J2MEBaseImpl extends Base {
    public void doSomething() {
        // J2Me code
    }
}
//#endif

在项目中使用的核心项目:

public class App {

    public void something {
        // Depends on the preprocessor symbol you used to build a project
        Base.getInstance().doSomething();
    }
}

比,如果你想建立为Android,你刚才定义的机器人的preprocessor符号或 J2ME 的,如果你想要做一个版本的J2ME平台。 ..

Than if you want to build for the Android, you just define ANDROID preprocessor symbol or J2ME if you want to do a build for a J2ME platform...

不管怎样,我希望它能帮助:)

Anyway, I hope it helps :)

这篇关于爪哇(Eclipse中) - 条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆