What is the best way to compile J2ME and J2SE applications from the same code base?

I am trying to create an application for J2ME and J2SE. The presentation code will obviously be different, but I hope to keep as much logic as possible.

My plan is to use an Ant or Antenna preprocessor to select a J2ME or J2SE Graphics object, this class being the only intersection between my logic and the displayed code. All I need to do is swap two or two import lines in multiple files during my Ant / Antenna build task.

I would like some advice on tuning.

I currently have two Eclipse projects, one J2ME and one J2SE. I have a couple of ideas on how I can configure the preprocessor:

  • Use the default J2SE code and only pre-process the J2SE code for exchange in special import versions of J2SE.
  • Use the Antenna preprocessor for J2ME and J2SE projects.
  • Use Ant text replacement to make necessary changes to the original state

    I. It looks hard to configure correctly
    II. feels a bit kludgy
    III. seems the least bad because I don’t see that I ever needed to use much more than a few conditional imports.

Does anyone have experience with this kind of thing? Some tips would be greatly appreciated.

+3
source share
3 answers

, ? MIDlet, - Java . . J2ME, J2SE "" , , . , , .

. , 3 Eclipse, J2ME, J2SE ( J2ME). J2ME-/J2SE.

+3

. J2SE, J2ME , , . , , .

:

<project name="SomeProject" default="buildJ2ME" basedir="..">
...
...
...
    <taskdef name="jadUpdater" classname="net.jxta.j2me.tools.Jad" classpath="${lib.dir}/jxta-tools.jar"/>
    <taskdef resource="proguard/ant/task.properties" classpath="${lib.dir}/proguard.jar" />
    <taskdef resource="antenna.properties" classpath="${lib.dir}/antenna-bin-1.0.2.jar" />

    <macrodef name="preprocess">
        <attribute name="source"/>
        <attribute name="dest"/>
        <attribute name="symbols"/>
        <sequential>
            <wtkpreprocess 
                version="2"
                srcdir="@{source}" 
                destdir="@{dest}" 
                symbols="@{symbols}"
                printsymbols="true">

            </wtkpreprocess>
        </sequential>
    </macrodef>


    <target name="compile" depends="init">
        <copy todir="${temp.dir}/src" >
            <fileset dir="${src.dir}"/>
        </copy>

        <preprocess source="${temp.dir}/src" dest="${temp.dir}/preprocessed" symbols="${symbols}"/>

        <javac srcdir="${temp.dir}/preprocessed"
               destdir="${temp.dir}/classes"
               bootclasspath="${bootclasspath}"
               classpath="${lib.dir}"
               debug="off"
               source="1.3"
               target="1.1" />

       <antcall target="jarForObfuscate"/>
    </target>

    <target name="buildJ2ME" depends="clean">
        <property name="symbols" value="J2ME1,J2ME2"/>
        <antcall target="compile"/>
    </target>

    <target name="buildJ2SE">
        <property name="symbols" value="J2SE1,J2SE2"/>
        <antcall target="compile"/>
    </target>
...
...
...

</project>

, !

+1

, j2me . j2se lib.

, , , , , j2me

Endresult: , #ifdef

0

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


All Articles