antenna 是一款免费的,开源的j2me预编译,打包的工具。之前一直以为他很神秘。由于干项目,花了一个星期的学习了下,现在把一些心得写写。
antenna的英文文档主页http://antenna.sourceforge.net/index.php
要学习antenna首先还需要一些ant的基本知识.参见 “ant基础学习笔记”
假如之前用过j2me-polish,对学习antenna是有好处的。我个人认为polish是在ant 的基础上增加了自己的一些处理,使得比antenna还更方便,更加简单。 学习antenna要掌握他的一些基本流程
常用的一般有 预编译,编译,校验,打包(混淆或不混淆)
因为我也是从一个初学者开始的,我根据我在学习过程中所遇到的疑问说说。
1.定义一些全局变量。包括文件路径,包名,jad名,midlet 名,版本号,等等
2.预编译 。因为我们用antenna一般是解决 对于同一份代码 适应于不同的设备,而不去创建多分代码从而减少代码的维护,预编译我认为就是把原始代码截取出符合本设备的代码,然后把它放到某个文件下去。因为我们终究是要编译的,只是根据原始代码中的限制条件而选取的代码,所以预编译的代码应当是完整的。【一般只移动source源文件,资源文件不做处理】
3.编译 。编译就是对预编译过程中选出的适合此设备的那份代码 进行编译。此过程相当于javac的处理,注意期间需要把资源文件拷到该编译的文件下。
4.打包 。生成jad ,jar。在此过程中需要把资源文件同样打入包内。需要进行校验处理。假如需要混淆则进行设置混淆属性
下面我具体通过实例说说 如何进行编写 antenna的重要文件。(确保你已经下载了antenna jar包,以及ant,我用的是eclipse3.2 里面自带ant )
我现在的任务是在soyoh 项目下把原始代码编译到两个不同的手机 iphone ,genericPhone下面去。如图:
主要的思路是,通过一个主的 build.xml文件分别去编译两个子的build.xml文件。
主build.xml文件如下
<?xml version="1.0"?> <project name="moblieStock" default="all" basedir="."> <target name="iphone"> <ant dir="iphone" /> </target> <target name="genericPhone"> <ant dir="genericPhone"></ant> </target> <target name="all" depends="iphone,genericPhone" /> </project>
该文件是一个入口文件,主要是去调用 iphone ,genericPhone的子任务。
相应的 genericPhone 我这里只是象征性的写了个空任务:
<?xml version="1.0"?> <project name="genericPhone" default="build" basedir="."> <target name="build"> </target> </project>
主要把精力放在iphone 上
<?xml version="1.0" ?> <project name="iphone" default="build" basedir="."> <target name="init" description="变量定义"> <echo message="Define variable" /> <taskdef resource="antenna.properties" classpath="C:/Program Files/antenna/antenna-bin-1.2.0-beta.jar" /> <!-- 相关变量定义 --> <property name="basedir" value="." /> <property name="jad.filename" value="iphoneStock" /> <property name="jar.filename" value="${jad.filename}" /> <property name="wtk.proguard.home" value="D:/software/proguard4.2"/> <!-- 目录结构定义 --> <property name="dir.src" value="../src" /> <property name="dir.res" value="../iphoneRes" /> <property name="dir.output" value="${basedir}/build" /> <property name="dir.dist" value="${basedir}/dist" /> <property name="dir.classes" value="${dir.output}/classes" /> <property name="dir.deployed" value="${dir.output}/deployed" /> <property name="wtk.home" value="C:/WTK22" /> <property name="wtk.cldc.version" value="1.0" /> <property name="wtk.midp.version" value="2.0" /> <property name="wtk.debug" value="false" /> <property name="wtk.obfuscate" value="true" /> <!-- JAD相关属性定义--> <property name="jad.midlet-name" value="联通手机证券" /> <property name="jad.midlet-vender" value="中国联通" /> <property name="jad.midlet-version" value="3.0.7" /> <!--Midlet相关定义--> <property name="jad.midlet-class" value="SoyohMobile" /> <property name="jad.midlet-icon" value="logo.png" /> <!--发布--> <!-- <property name="deploy.target" value="http://localhost:8080/deployer" /> <property name="deploy.login" value="admin" /> <property name="deploy.password" value="admin" /> --> </target> <target name="clean" description="清除文件"> <echo message="Clean dir" /> <delete dir="${dir.output}" /> <delete failonerror="false"> <fileset dir="."> <exclude name="build.xml"/> </fileset> </delete> </target> <target name="preprocess" depends="init,clean" description="预处理"> <!--<echo message="preprocess" />--> <mkdir dir="${dir.dist}" /> <wtkpreprocess encoding="utf-8" srcdir="${dir.src}" destdir="${dir.dist}" device="Iphone" printsymbols="true" /> </target> <target name="compile" depends="preprocess" description="编译"> <echo>Starting compile</echo> <mkdir dir="${dir.classes}" /> <copy todir="${dir.dist}"> <fileset dir="${dir.res}" includes="*.png" /> <fileset dir="${dir.res}" includes="*.cfg" /> </copy> <echo>build from all the ${dir.src} to ${dir.classes}</echo> <wtkbuild encoding="utf-8" srcdir="${dir.dist}/" destdir="${dir.classes}" preverify="false" debug="${wtk.debug}" listfiles="no" > </wtkbuild> </target> <target name="build" depends="compile" description="生成"> <echo message="Packaging library"/> <copy todir="${dir.classes}"> <fileset dir="${dir.res}" includes="*.png" /> <fileset dir="${dir.res}" includes="*.cfg" /> </copy> <mkdir dir="${dir.deployed}" /> <wtkjad encoding="utf-8" jadfile="${dir.deployed}/${jad.filename}.jad" jarfile="${dir.deployed}/${jar.filename}.jar" name="${jad.midlet-name}" vendor="${jad.midlet-vender}" version="${jad.midlet-version}"> <midlet name="${jad.midlet-name}" class="soyoh.${jad.midlet-class}" icon="${jad.midlet-icon}"/> <attribute name="MIDlet-Icon" value="${jad.midlet-icon}" /> <attribute name="MIDlet-Permissions" value="${jad.midlet-permissions}"/> <attribute name="MicroEdition-Configuration" value="CLDC-1.0"/> <attribute name="MicroEdition-Profile" value="MIDP-2.0"/> </wtkjad> <wtkpackage encoding="utf-8" jarfile="${dir.deployed}/${jar.filename}.jar" jadfile="${dir.deployed}/${jad.filename}.jad" obfuscate="${wtk.obfuscate}" preverify="true"> <fileset dir="${dir.classes}"/> </wtkpackage> </target> </project>
这里我在 <wtkpreprocess/>,<wtkbuild />, <wtkpackage /> 都加了一个 encoding="utf-8" ,这是有原因的,在没加之前进行编译的文件时ANSI编码格式,而这样可以使得编码转换成utf-8格式。不会产生乱码。
编译时的输出内容如下:
Buildfile: D:/moblieStock/soyohmobile/build.xml
iphone:
init:
[echo] Define variable
clean:
[echo] Clean dir
[delete] Deleting directory D:/moblieStock/soyohmobile/iphone/build
[delete] Deleting 101 files from D:/moblieStock/soyohmobile/iphone
preprocess:
[wtkpreprocess] **************************************************************
[wtkpreprocess] * Antenna 1.2.0 initialized for project "iphone" *
[wtkpreprocess] * Using "Sun Wireless Toolkit 2.3" (CLDC-1.0; MIDP-2.0) *
[wtkpreprocess] **************************************************************
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/lib/capabilities.xml not found
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/./capabilities.xml not found
[wtkpreprocess] Devices: loading capabilities.xml from classpath
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/lib/groups.xml not found
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/./groups.xml not found
[wtkpreprocess] Devices: loading groups.xml from classpath
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/lib/devices.xml not found
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/./devices.xml not found
[wtkpreprocess] Devices: loading devices.xml from classpath
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/lib/custom-devices.xml not found
[wtkpreprocess] Devices: D:/moblieStock/soyohmobile/./custom-devices.xml not found
[wtkpreprocess] Devices: loading custom-devices.xml from classpath
Warning: unknown device "Iphone"
Symbols:
[wtkpreprocess] Preprocessing 90 file(s) at D:/moblieStock/soyohmobile/src
compile:
[echo] Starting compile
[mkdir] Created dir: D:/moblieStock/soyohmobile/iphone/build/classes
[copy] Copying 11 files to D:/moblieStock/soyohmobile/iphone/dist
[echo] build from all the ../src to D:/moblieStock/soyohmobile/iphone/build/classes
[wtkbuild] Compiling 90 source files to D:/moblieStock/soyohmobile/iphone/build/classes
build:
[echo] Packaging library
[copy] Copying 11 files to D:/moblieStock/soyohmobile/iphone/build/classes
[mkdir] Created dir: D:/moblieStock/soyohmobile/iphone/build/deployed
[wtkjad] Creating JAD file D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jad
[wtkpackage] Building jar: D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jar
[wtkpackage] Obfuscating D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jar with ProGuard
[wtkpackage] ProGuard, version 4.2
[wtkpackage] Reading program jar [D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jar]
[wtkpackage] Reading library jar [C:/WTK22/lib/midpapi20.jar]
[wtkpackage] Reading library jar [C:/WTK22/lib/cldcapi10.jar]
[wtkpackage] Note: duplicate definition of library class [com.sun.cldc.i18n.j2me.UTF_8_Reader]
[wtkpackage] Note: there were 1 duplicate class definitions.
[wtkpackage] Preparing output jar [D:/moblieStock/soyohmobile/iphone/99e690d3.tmp/obfuscated.jar]
[wtkpackage] Copying resources from program jar [D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jar]
[wtkpackage] Preverifying D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jar
[wtkpackage] Executable: C:/WTK22/bin/preverify
[wtkpackage] Arguments : -classpath C:/WTK22/lib/midpapi20.jar;C:/WTK22/lib/cldcapi10.jar -d D:/moblieStock/soyohmobile/iphone/99e690d3.tmp -target CLDC1.0 D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jar
[wtkpackage] Updating JAD file D:/moblieStock/soyohmobile/iphone/build/deployed/iphoneStock.jad
genericPhone:
build:
all:
BUILD SUCCESSFUL
Total time: 13 seconds
成功编译之后,文件结构如下
好了,成功了!!~~~