Ant引入第三方jar遇到的问题

最近在Android工程中引入了umeng统计,在Eclipse中一切正常,但是用ant编译时遇到了一些问题:

可以正常编译过,但运行时会遇到“ClassNotFound Exception”,如果把它去掉,工程就根本编译不过,很奇怪的问题。

由于ant build.xml是根据apk的生成过程来写的,所以我想问题应该是出在生成APK的某个过程之中,于是就查了一些APK的生成步骤,见这里: http://blog.csdn.net/itachi85/article/details/6460158 核对了一下那张图,发现在生成dex的过程中也需要Libraries,所以我想问题不会出在这个上面吧,因为在build.xml中,生成dex这个target没有依赖第三方lib, 于是添加第三方jar路径:

  <arg path="${basedir}/jar/" />

再重新编译

ant -f build_windows.xml debug

重新安装,运行正常。

附build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
<project basedir="." default="debug" name="IShare">


	<!-- Manual: 
	this file(build.xml) can be used to either Windows or Linux, as you wish.
	Generation:
	the defualt target is "debug", so if you type the "ant" in commandline , it will result the same
	as "ant debug", and the steps will be :clean -> dirs -> resource-src -> compile -> dex ->package-res-and-assets -> debug
	for details, see the dependencies among the target!

	Now it's scripted in the linux template, actually ,it's mainly the same with the situation in windows,
	the only defference is ,the "TOOLS_DX", "TOOLS_AAPT","TOOLS_APKBUILDER",
	in Android android-sdk-linux, the corresponding tools are  "dx" "aapt" "apkbuilder"
	while in android-sdk-linux, the corresponding tools are "dx.bat" "aapt.exe" "apkbuilder.bat".

	if you wanna adapt the file to your java project ,the only thing you need to do is to change 
	the following 6 properties,	

	<property name="Adbase" ........................./>      the Android SDK path
	<property name="Androidjar" ...................../>      the path of android.jar
	<property name="Javacpath" ...................../>	 the path of javac compiler	
	<property name="TOOLS_DX" ....................../>       the path of dx tools
	<property name="TOOLS_AAPT ....................../>	 the path of aapt tools
	<property name="TOOLS_APKBUILDER ................./>	 the path of apkbuilder tools                 -->


	<property name="Adbase" value="D:\android\sdk\android-sdk-all"/>
	<property name="Androidjar" value="${Adbase}\platforms\android-8\android.jar" />
	<property name="Javacpath"  value="C:\Program Files\Java\jdk1.6.0_20\bin\javac.exe" />
	<property name="TOOLS_DX"  value="${Adbase}\platforms\android-8\tools\dx.bat"/>
	<property name="TOOLS_AIDL" value="${Adbase}\platforms\android-8\tools\aidl.exe"/>
	<property name="TOOLS_AAPT" value="${Adbase}\platforms\android-8\tools\aapt.exe"/>
	<property name="TOOLS_APKBUILDER" value="${Adbase}\tools\apkbuilder.bat"/>
	<property name="TOOLS_ZIPALIGN" value="${Adbase}\tools\zipalign.exe"/>
	<property name="NDK" value="c:\mytools\ndk_bat.bat"/>



	<property name="external-libs" value="libs" />
	<condition property="external-libs-ospath"
		value="${basedir}\${external-libs}"
		else="${basedir}/${external-libs}" >
		<os family="windows"/>
	</condition>

	<path id="lib_classpath">
		<pathelement location="bin"/>
		<pathelement location="${Androidjar}"/>
		<!--pathelement location="${basedir}\jar\httpmime-4.1.1.jar"/>
		<pathelement location="${basedir}\jar\signpost-commonshttp4-1.2.1.1.jar"/>
		<pathelement location="${basedir}\jar\signpost-core-1.2.1.1.jar"/>
		<pathelement location="${basedir}\jar\Analytics-Android-SDK-2.1.jar2"/-->
        <fileset dir="${basedir}\jar">
            <include name="*.jar"/>
        </fileset>  
        
	</path>

	<target name="clean">
		<delete dir="bin"/>
	</target>

	<target name="dirs">  
		<echo>===By East===Creating output directories if needed...</echo>
		<mkdir dir="bin" />  
		<mkdir dir="bin/classes" />  
	</target> 

	<target name="ndk">  
		<echo>===By East===ndk-build...</echo>
		<exec executable="${NDK}" failonerror="true">  
		</exec>  
	</target> 

	<target name="aidl" >  
		<echo>===By East===Compiling the aidl to java classes</echo>
		<echo> ${android-framework} </echo>
		<apply executable="${TOOLS_AIDL}" failonerror="true">
			<arg value="-I${basedir}/src" />
			<fileset dir="src">
				<include name="**/*.aidl"/>
			</fileset>
		</apply>
	</target> 

	<target name="compile" depends="clean,dirs,resource-src,aidl">  
		<echo>===By East===Compiling the java code via ${Javacpath}...</echo>
		<echo> *********** ${srcdir} </echo>
		<javac  executable="${Javacpath}" fork="true"
			debug="false" extdirs="" srcdir="${basedir}/src" destdir="${basedir}/bin/classes">
			<classpath refid="lib_classpath"/>    
		</javac>  
	</target> 




	<target name="dex" depends="compile">  
		<echo>===By East===Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>  
		<exec executable="${TOOLS_DX}" failonerror="true">  
			<arg value="--dex" />  
			<arg value="--output=${basedir}/bin/classes.dex" />  
            <arg path="${basedir}/bin/classes" />
            <!--MUST ADD THIS,OTHERWISE IT WILL OCCUR class not found error, add by pjq -->
            <arg path="${basedir}/jar/" />
		</exec>  
	</target>  


	<target name="resource-src" depends="dirs">  
		<echo>===By East===Generating R.java / Manifest.java from the resources...</echo>  
		<exec executable="${TOOLS_AAPT}" failonerror="true">  
			<arg value="package" />  
			<arg value="-m" />  
			<arg value="-J" />  
			<arg value="src" />  
			<arg value="-M" />  
			<arg value="AndroidManifest.xml" />  
			<arg value="-S" />  
			<arg value="res" />  
			<arg value="-I" />  
			<arg value="${Androidjar}" />  
		</exec>  
	</target>  


	<target name="package-res-and-assets">  
		<echo>Packaging resources and assets...</echo>  
		<exec executable="${TOOLS_AAPT}" failonerror="true">  
			<arg value="package" />  
			<arg value="-f" />  
			<arg value="-M" />  
			<arg value="AndroidManifest.xml" />  
			<arg value="-S" />  
			<arg value="res" />

			<arg value="-A" />
			<arg value="assets" /> 

			<arg value="-I" />  
			<arg value="${Androidjar}" />  
			<arg value="-F" />  
			<arg value="bin/${ant.project.name}.ap_" />  
		</exec>  
	</target>  




	<target name="apk" depends="clean, dex,dirs,package-res-and-assets">  
		<echo>===By East===Packaging ${out-debug-package}, and signing it with a debug key...</echo>  
		<echo>"${external-libs-ospath}"</echo>  
		<exec executable="${TOOLS_APKBUILDER}" failonerror="true">  
			<arg value="${basedir}/bin/${ant.project.name}_temp.apk" />  
			<arg value="-z" />  
			<arg value="${basedir}/bin/${ant.project.name}.ap_" />  
			<arg value="-f" />  
			<arg value="${basedir}/bin/classes.dex" /> 
			<arg value="-rf" />  
			<arg value="${basedir}/src" />
			<arg value="-nf" />
			<arg value="${external-libs-ospath}" />
		</exec>  
	</target>  


	<target name="debug" depends="apk" >  
		<echo>===By East===begin to Check the 4 byte problem...</echo>  
		<exec executable="${TOOLS_ZIPALIGN}" failonerror="true">  
			<arg value="-v" />  
			<arg value="4" />  
			<arg value="bin/${ant.project.name}_temp.apk" />  
			<arg value="bin/${ant.project.name}.apk" />  
		</exec>  
	</target>  


</project>