Java(Eclipse) - 条件编译 [英] Java (Eclipse) - Conditional compilation
问题描述
在这个项目中,我想使用条件编译。
像...一样...
//#if android
...
//#endif
//如果j2me
...
//#endif
我一直在阅读这篇文章,但我没有找到任何有用的东西。
您可以使用 Antenna (有一个Eclipse的插件,您可以使用它与Ant构建系统)。
我正在使用它在我的项目中,以您描述的方式,它的工作原理:)
编辑:这里是与@ WhiteFang34相关的示例解决方案是一种方式:
在您的核心项目中:
// base class Base.java
public abstract class Base {
public static Base getInstance()
{
//#ifdef ANDROID
返回新的AndroidBaseImpl();
//#elif J2ME
返回新的J2MEBaseImpl();
//#endif
}
public abstract void doSomething();
}
// Android特定实现AndroidBaseImpl.java
//#ifdef ANDROID
public class AndroidBaseImpl extends Base {
public void doSomething(){
// Android代码
}
}
//#endif
// J2ME具体实现J2MEBaseImpl.java
//#ifdef J2ME
public class J2MEBaseImpl extends Base {
public void doSomething(){
// J2Me code
}
}
//#endif
在使用核心项目的项目中:
public class App {
public void something {
//取决于您用于构建项目的预处理器符号
Base.getInstance()。doSomething();
}
}
比如果你想为Android构建,你只要定义 ANDROID 预处理器符号或 J2ME ,如果你想为J2ME平台做一个构建...
无论如何,我希望它有助于:)
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.
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 :)
这篇关于Java(Eclipse) - 条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!