Java (Eclipse) - 条件编译

发布于 2024-10-24 21:17:03 字数 194 浏览 1 评论 0原文

我有一个在 j2me 项目和 android 项目中引用的 java 项目。 在这个项目中我想使用条件编译。

就像......

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

//if j2me
...
//#endif

我一直在阅读有关此内容的内容,但我还没有发现任何有用的东西。

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.

Something like...

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

//if j2me
...
//#endif

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

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

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

发布评论

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

评论(3

时常饿 2024-10-31 21:17:04

您可以使用 Antenna (有一个 Eclipse 插件,您可以将它与 Ant 构建系统一起使用) 。
我在我的项目中以您所描述的方式使用它,并且效果完美:)

编辑:这是与 @WhiteFang34 解决方案相关的示例,这是一种可行的方法:

在您的核心项目中:

//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 进行构建,则只需定义 ANDROID 预处理器符号或 J2ME(如果)您想为 J2ME 平台进行构建...

无论如何,我希望它有所帮助:)

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 :)

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

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

Anyway, I hope it helps :)

耳根太软 2024-10-31 21:17:04

也许您应该考虑围绕特定于配置文件(J2ME、Android 或将来的其他)的逻辑创建接口。然后为每个配置文件创建界面的具体实现。您可以将任何公共部分拆分为一个抽象基类,以便两个实现都可以扩展。这样,每个配置文件的逻辑就可以针对不同的问题很好地分开。对于每个配置文件,只需构建适当的类集(例如,您可以通过包将它们分开)。从长远来看,它将更容易维护、调试、测试和理解。

Perhaps you should consider creating interfaces around the logic that's specific to a profile (J2ME, Android or other in the future). 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.

屋顶上的小猫咪 2024-10-31 21:17:04

Eclipse MTJ 项目 提供预处理支持,如 记录 。此支持主要针对解决 JavaME 上的碎片问题。我还没有测试预处理支持和 Android 工具,但它可能可以工作。

Eclipse MTJ project provides preprocessing support as documented . This support was mainly targeted for tackling fragmentation problems on JavaME. I have not tested the preprocessing support together with the Android tooling but it may just work.

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