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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章