Java (Eclipse) - Conditional Compilation

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

Sort of...

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

I read about it, but have not yet found anything useful.

+4
source share
3 answers

You can use Antenna (there is a plugin for Eclipse, and you can use it with the Ant build system), I use it in my projects as you described, and it works fine :)

EDIT: here is an example related to @ WhiteFang34 solution, which is the way:

In your main 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 main project:

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

If you want to create for Android, you simply define the symbol of the ANDROID or J2ME preprocessor if you want to build for the J2ME platform ...

Anyway, I hope this helps :)

+8
source

Perhaps you should consider creating interfaces around the profile-specific logic (J2ME, Android, or others in the future). Then create specific implementations of your interface for each profile. Any common parts that you could split into an abstract base class for both versions. Thus, your logic for each profile is perfectly separated for different problems. For each profile, just create the appropriate set of classes (you can separate them by package, for example). Ultimately, it will be easier to maintain, debug, test, and understand.

+5
source

Eclipse The MTJ project provides document preprocessing support. This support was mainly aimed at solving fragmentation problems in JavaME. I have not tested preprocessing support with Android tools, but it may just work.

0
source

Source: https://habr.com/ru/post/1344899/


All Articles