Java (Eclipse) - 条件编译
我有一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Antenna (有一个 Eclipse 插件,您可以将它与 Ant 构建系统一起使用) 。
我在我的项目中以您所描述的方式使用它,并且效果完美:)
编辑:这是与 @WhiteFang34 解决方案相关的示例,这是一种可行的方法:
在您的核心项目中:
在使用核心项目的项目中:
如果您想针对 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:
In your project that uses the core project:
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 :)
也许您应该考虑围绕特定于配置文件(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.
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.