简体   繁体   English

Java(Eclipse) - 条件编译

[英]Java (Eclipse) - Conditional compilation

I have a java project that is referenced in j2me project and in android project. 我有一个java项目,在j2me项目和android项目中引用。 In this project i would like to use conditional compilation. 在这个项目中,我想使用条件编译。

Something like... 就像是...

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

//if j2me
...
//#endif

I have been reading about this but i did not find anything useful yet. 我一直在读这个,但我还没有找到任何有用的东西。

You could use Antenna (there is a plugin for Eclipse, and you can use it with the Ant build system). 您可以使用Antenna (Eclipse有一个插件,您可以将它与Ant构建系统一起使用)。 I'm using it in my projects in a way you've described and it works perfectly :) 我正在以你描述的方式在我的项目中使用它,它完美地工作:)

EDIT: here is the example related to @WhiteFang34 solution that is a way to go: 编辑:这是与@ WhiteFang34解决方案相关的示例,这是一种方法:

In your core project: 在您的核心项目中:

//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

In your project that uses the core project: 在使用核心项目的项目中:

public class App {

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

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... 如果你想为Android构建,你只需定义ANDROID预处理器符号或J2ME,如果你想为J2ME平台构建一个...

Anyway, I hope it helps :) 无论如何,我希望它有帮助:)

Perhaps you should consider creating interfaces around the logic that's specific to a profile (J2ME, Android or other in the future). 也许您应该考虑围绕特定于配置文件的逻辑创建接口(J2ME,Android或其他未来)。 Then create concrete implementations of your interface for each profile. 然后为每个配置文件创建接口的具体实现。 Any common parts you could split out into an abstract base class for both implementations to extend. 您可以将任何公共部分拆分为抽象基类,以便扩展两个实现。 This way your logic for each profile is nicely separated for different concerns. 这样,每个配置文件的逻辑可以很好地分离,以适应不同的问题。 For each profile just build the appropriate set of classes (you could separate them by package for example). 对于每个配置文件,只需构建适当的类集(例如,您可以通过包将它们分开)。 It'll be easier to maintain, debug, test and understand in the long run. 从长远来看,维护,调试,测试和理解会更容易。

Eclipse MTJ project provides preprocessing support as documented . Eclipse的MTJ项目提供预处理支持, 记录 This support was mainly targeted for tackling fragmentation problems on JavaME. 这种支持主要针对解决JavaME上的碎片问题。 I have not tested the preprocessing support together with the Android tooling but it may just work. 我没有使用Android工具测试预处理支持,但它可能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM