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

build_windows.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>


原文地址:http://pjq.me/wiki/doku.php?id=android:ant-build-issues

### 回答1: ant.jar包是Apache Ant项目的核心组件之一,它的作用是提供了Ant构建工具的运行环境和功能支持。 Ant是一种基于Java的构建工具,它用于自动化软件构建过程。Ant使用XML文件作为构建脚本,通过定义一系列的任务和目标来描述软件的构建过程。而ant.jar包则是Ant运行所必需的,它含了Ant运行时所需的库和资源文件。 具体来说,ant.jar包的功能括: 首先,ant.jar包提供了Ant任务的执行擎。它可以解析构建脚本,执行任务并控制任务的执行顺序,从而实现整个构建过程的自动化。 其次,ant.jar包提供了一系列的核心任务和型。这些任务和括文件操作、编译、打、文档生成等等,可以满足不同项目的构建需求。开发人员可以根据具体的项目需求,选择适当的任务和型来完成构建过程的各种操作。 此外,ant.jar包还提供了丰富的插件支持。开发人员可以将自定义的任务和型打为插件,然后通过ant.jar包将这些插件集成到Ant的运行环境,从而扩展Ant的功能。这使得Ant可以灵活应对各种构建需求,满足不同项目的特殊要求。 总之,ant.jar包Ant构建工具的核心组件之一,它提供了Ant运行环境和功能支持,使得开发人员可以通过Ant实现软件构建的自动化,并根据项目需求灵活扩展功能。 ### 回答2: ant.jar包是Apache Ant工具的核心库文件,它是一种用于构建Java应用程序的自动化构建工具。Ant是基于Java的构建工具,用于编译、测试和打Java项目,以及执行各种构建任务。ant.jar包的作用是为Ant提供必要的和方法,以支持Ant自身的功能和扩展。 ant.jar包含了Ant的核心和任务,其括了任务执行流程的控制、构建目标的定义和执行、文件操作、运行系统命令、资源管理等功能。通过引入ant.jar包,可以在Java项目通过Ant的脚本来定义和执行构建任务,提高了项目构建的效率和可维护性。 ant.jar包具有以下功能和特点: 1. 定义构建目标和任务:通过ant.jar包,可以使用Ant的XML脚本语言来定义构建目标和任务,按照需要执行编译、测试、打等构建任务。 2. 跨平台支持:Ant是基于Java的,因此ant.jar包可以在不同的操作系统上运行,实现跨平台的构建工作。 3. 灵活性和可扩展性:ant.jar包Ant工具的核心库文件,提供了大量的任务和扩展点,用户可以根据自己的需求和项目要求来编写自定义的任务和扩展。 4. 集成第三方工具:ant.jar包可以与其他开发工具和构建工具(如JUnit、Ivy等)进行集成,方便进行项目管理和构建工作。 5. 构建任务的自动化:通过ant.jar包,可以自动执行一系列编译、打、部署等构建任务,减少了手动执行这些任务的工作量。 总而言之,ant.jar包是Apache Ant工具的核心库文件,为Ant提供必要的和方法,实现了自动化构建和项目管理的功能,提高了Java项目的开发效率和可维护性。 ### 回答3: ant.jar是Apache Ant工具的核心库文件,它主要用于构建和自动化构建过程使用的任务和操作。具体来说,ant.jar包的作用括以下几个方面: 1. 构建自动化:ant.jar提供了一组任务和操作,可以通过编写Ant构建文件来定义和执行各种构建过程。开发人员可以使用Ant的任务来编译源代码、打应用程序、运行单元测试、生成文档等,从而实现构建自动化。 2. 构建管理:ant.jar支持将构建过程分解为多个独立的目标和任务,以及定义它们之间的依赖关系。这使得构建过程更加模块化和可管理,开发人员可以根据需要选择性地执行某些目标,或者定义特定的构建流程。 3. 跨平台性:ant.jar是基于Java的,因此可以在多个操作系统平台上运行。开发人员可以使用相同的Ant构建文件在不同的平台上执行构建过程,无需担心平台差异和兼容性问题。 4. 扩展性:ant.jar提供了插件机制,开发人员可以编写自定义任务和操作,并将其添加到Ant构建文件。这使得Ant可以根据具体项目的需求进行扩展,并实现更多特定的构建功能。 总之,ant.jar是一个功能强大的构建工具库,它可以帮助开发人员实现构建自动化和管理,并且具有跨平台性和扩展性。通过使用ant.jar,开发人员可以更高效地进行软件开发和构建过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值