2 bingducaijun BINGDUCAIJUN 于 2016.05.09 17:43 提问

Android ant 脚本打包 关联library project的项目

以下是build.xml
Android ant 脚本打包 关联library project的项目,java源文件编译成class文件时 找不到R 打包cardview的library时 总是不能成功,其他library生成的都是包。但是cardview就是不行·················································

<?xml version="1.0" encoding="UTF-8"?>

<!-- 指定配置文件 -->



<!-- 定义工具目录 -->



<property name="android.tools.absolute.dir" location="${android.tools.dir}" />
<property name="android.platformtools.absolute.dir" location="${android.platformtools.dir}" />
<property name="android.platforms.absolute.dir" location="${android.platforms.dir}" />

<!--        定义工具        
<property name="verbose" value="false" />-->
<condition property="exe" value=".exe" else="">
    <os family="windows" />
</condition>
<property name="jar.proguard" value="${proguard}" />
<property name="android-jar" value="${android.platforms.absolute.dir}/android.jar" />
<property name="android-dx" value="${android.platformtools.absolute.dir}/dx.bat" />
<property name="android-apkbuilder" value="${android.tools.absolute.dir}/apkbuilder.bat" />
<property name="android-adb" value="${android.platformtools.absolute.dir}/adb${exe}" />
<property name="android.zipalign" value="${android.tools.absolute.dir}/zipalign${exe}" />
<property name="android-aapt" value="${android.platformtools.absolute.dir}/aapt${exe}" />
<property name="android-jarsigner" value="${JAVA_HOME}/bin/jarsigner${exe}" />


<!-- ANT环境变量 -->
<property environment="env" />

<!-- 生成R文件的相对目录 -->
<property name="outdir-gen" value="${project.home}/gen" />
<!-- 编译后的文件放置目录 -->
<property name="outdir-bin" value="${project.home}/bin" />
<!-- 输出目录 -->
<property name="outdir" value="${project.outdir}" />
<!-- 清单文件 -->
<property name="manifest-xml" value="${project.home}/AndroidManifest.xml" />
<!-- 源文件目录 -->
<property name="resource-dir" value="res" />
<property name="asset-dir" value="assets" />
<!-- 资源文件目录 -->
<property name="drawable-dir" value="${resource-dir}/drawable" />
<property name="drawable-hdpi-dir" value="${resource-dir}/drawable-hdpi"/>
<property name="drawable-ldpi-dir" value="${resource-dir}/drawable-ldpi"/>
<property name="drawable-mdpi-dir" value="${resource-dir}/drawable-mdpi"/>
<property name="drawable-zh-rCN-dir" value="${resource-dir}/drawable-zh-rCN"/>
<!-- java源文件目录 -->
<property name="srcdir" value="src" />
<property name="srcdir-ospath" value="${project.home}/${srcdir}" />
<!-- 动态库so文件目录 -->
<property name="library" value="${project.home}/libs" />
<!-- 第三方library -->  
<property name="library-dir1" value="${library1}" />  
<property name="library-dir3" value="${library3}" />  
<!-- 外部类库所在目录 -->
<property name="external-lib" value="lib" />
<property name="external-lib-ospath" value="${project.home}/${external-lib}" />
<!-- 生成class目录 -->
<property name="outdir-classes" value="${outdir-bin}/classes" />
<property name="outdir-classes-ospath" value="${outdir-classes}" />
<!-- classes.dex相关变量 -->
<property name="dex-file" value="classes.dex" />
<property name="dex-path" value="${outdir-bin}/${dex-file}" />
<property name="dex-ospath" value="${dex-path}" />
<!-- 经过aapt生成的资源包文件 -->
<property name="resources-package" value="${outdir-bin}/resources.ap_" />
<property name="resources-package-ospath" value="${resources-package}" />
<!-- 未认证apk包 -->
<property name="out-unsigned-package" value="${outdir-bin}/${appName}-unsigned.apk" />
<property name="out-unsigned-package-ospath" value="${out-unsigned-package}" />
<!-- 证书文件 -->
<property name="keystore-file" value="${keystore}" />
<!-- 已认证apk包 -->
<property name="out-signed-package" value="${outdir-bin}/${appName}.apk" />
<property name="out-signed-package-ospath" value="${out-signed-package}" />
<!-- 已优化apk包 -->
<property name="zipalign-signed-package" value="${outdir-bin}/${appName}${version}_release.apk" />
<!-- 输出路径的apk包 -->
<property name="outdir-package" value="${project.outdir}/${appName}.apk" />
<!-- 初始化工作 -->
<target name="init">
    <echo>Initializing all output directories...</echo>
    <delete dir="${outdir-bin}" />
    <delete dir="${outdir-gen}" />
     <delete dir="${library-dir1}/bin" />  
    <delete dir="${library-dir3}/bin" />  
    <delete dir="${library-dir1}/gen" />  
    <delete dir="${library-dir3}/gen" />  
    <mkdir dir="${outdir-gen}" />
    <mkdir dir="${outdir-bin}" />
     <delete dir="${library-dir1}/bin" />  
    <delete dir="${library-dir3}/bin" />  
    <delete dir="${library-dir1}/gen" />  
    <delete dir="${library-dir3}/gen" />  
    <mkdir dir="${outdir-classes}" />
<!-- 把缺少的BuildConfig复制进去,暂时不知道如何生成 -->
   </target>
<!-- 根据工程中的资源文件生成R.java文件 ,多个合并为一个-->
<target name="GenRJAVA" depends="init">
    <echo>Generating R.java / Manifest.java from the resources...</echo>
    <exec executable="${android-aapt}" failonerror="true">
        <arg value="package" /><!-- package表示打包-->
        <arg value="-m" /><!--m,J,gen表示创建包名的目录和R.java到gen目录下 -->
        <arg value="-J" />
        <arg value="${outdir-gen}" />
        <arg value="-M" /><!-- M指定AndroidManifest.xml文件-->
        <arg value="${manifest-xml}" />
        <arg value="-S" /><!-- S指定res目录,生成对应的ID,可多个-->
        <arg value="${resource-dir}" />
        <arg value="-S" />
         <arg value="${library-dir1}/res" /> <!--  注意点:同时需要调用Library的res-->  
        <arg value="-S" />  
        <arg value="${library-dir3}/res" />  
         <arg value="--extra-packages" />  
        <arg value="slidingmenu_library:SMSSDK" />                   
        <arg value="-I" /><!-- I指定android包的位置-->
        <arg value="${android-jar}" />
        <arg value="--auto-add-overlay" /> <!-- 这个重要,覆盖资源,不然报错-->
    </exec>       
</target>


<!-- 将工程及库应用中的java源文件编译成class文件 -->
<target name="compile" depends="GenRJAVA">
    <echo>Compiling java source code...</echo>
    <javac encoding="UTF-8"target="1.6"  destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="on">
        <src path="${library-dir1}/src" /><!-- 库应用源码 -->
        <src path="${outdir-gen}" /><!-- 生成的class文件全部保存到bin/classes目录下 -->
        <classpath>
            <fileset dir="${library-dir1}/libs" includes="*.jar" /><!-- 第三方jar包需要引用,用于辅助编译 -->
        </classpath>
    </javac>

    <javac encoding="UTF-8" destdir="${outdir-classes}" bootclasspath="${android-jar41}" includeantruntime="on">  
        <src path="${library-dir3}/src" />  
        <src path="${outdir-gen}" />  
        <classpath>  
            <fileset dir="${library-dir3}/libs" includes="*.jar" />   
        </classpath>  
    </javac>  
    <javac encoding="UTF-8" destdir="${outdir-classes}" bootclasspath="${android-jar}">
        <src path="${project.home}/src" /><!-- 工程源码-->
        <src path="${outdir-gen}" /><!-- 生成的class文件全部保存到bin/classes目录下 -->
        <classpath>
            <fileset dir="${library}" includes="*.jar" /><!-- 第三方jar包需要引用,用于辅助编译 -->
        </classpath>
    </javac>
</target>

<!--执行代码混淆和优化 必须在java文件编译以后-->

<target name="optimize" depends="compile">
    <echo>optimize classes are put to "${out.absolute.dir}" .</echo>
    <jar basedir="${outdir-classes}" destfile="${outdir-bin}/temp.jar"/>
    <taskdef resource="proguard/ant/task.properties"  classpath="${jar.proguard}" />
    <proguard>  
        -injars                     ${outdir-bin}/temp.jar   
        -outjars                    ${outdir-bin}/optimized.jar
        -libraryjars                ${android-jar}   
        -optimizationpasses 5
        -dontskipnonpubliclibraryclasses
        -dontpreverify
        -verbose
        -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
        -include proguard-project.txt
      </proguard>
    <delete file="${outdir-bin}/temp.jar"/>
    <delete dir="${outdir-classes}" failonerror="false" />
    <mkdir dir="${outdir-classes}"/>
    <unzip src="${outdir-bin}/optimized.jar" dest="${outdir-classes}"/>
    <delete file="${outdir-bin}/optimized.jar"/>
</target>


<!-- 将.class文件转化成.dex文件 -->
<target name="dex" depends="compile">
    <echo>Converting compiled files and external libraries into ${outdir-dx}test.dex...</echo>
    <exec executable="${android-dx}" failonerror="true">
        <arg value="--dex" />
        <arg value="--output=${dex-ospath}" /><!-- 输出 -->
        <arg path="${outdir-classes}" /><!-- classes文件位置 -->
        <arg value="${library}" /> <!-- 把libs下所有jar打包 -->
    </exec>
</target>
<!-- 将资源文件放进输出目录 -->
<target name="package-res-and-assets">
    <echo>Packaging resources and assets...</echo>
    <exec executable="${android-aapt}" failonerror="true">
        <arg value="package" />
        <arg value="-f" /><!-- 资源覆盖重写 -->
        <arg value="-M" />
        <arg value="${manifest-xml}" />
        <arg value="-S" />
        <arg value="${resource-dir}" />
        <arg value="-S"/>
        <arg value="${library-dir1}/res"/>  
        <arg value="-S"/>  
        <arg value="${library-dir3}/res"/>  
        <arg value="-A" /><!-- asset目录也打包 -->
        <arg value="${asset-dir}" />
        <arg value="-I" />
        <arg value="${android-jar}" />
        <arg value="-F" /><!-- 输出资源压缩包 -->
        <arg value="${resources-package}" />
        <arg value="--auto-add-overlay" />
    </exec>
</target>
<!-- 打包成未签证的apk -->
<target name="package" depends="dex,package-res-and-assets">
    <echo>Packaging unsigned apk for release...</echo>     
    <exec executable="${android-apkbuilder}" >
        <arg value="${out-unsigned-package-ospath}" /><!-- 输出 -->
        <arg value="-u" /><!-- u指创建未签名的包-->
        <arg value="-z" /><!-- 资源压缩包 -->
        <arg value="${resources-package-ospath}" />
        <arg value="-f" /><!-- dex文件 -->
        <arg value="${dex-ospath}" />
        <arg value="-nf"/><!-- 把libs下的so库打包 -->
        <arg value="${library}" />
    </exec>
    <echo>It will need to be signed with jarsigner before being published.</echo>
</target>
<!-- 对apk进行签证 -->
<target name="jarsigner" depends="package">
    <echo>Packaging signed apk for release...</echo>
    <exec executable="${android-jarsigner}" failonerror="true">
        <arg value="-keystore" />
        <arg value="${keystore-file}" />
        <arg value="-storepass" />
        <arg value="111111" /><!-- 验证密钥完整性的口令,创建时建立的 -->
        <arg value="-keypass" />
        <arg value="111111" /> <!-- 专用密钥的口令,就是key密码 -->
        <arg value="-signedjar" />
        <arg value="${out-signed-package-ospath}" /><!-- 输出 -->
        <arg value="${out-unsigned-package-ospath}" /><!-- 未签名的apk -->
        <!-- 不要忘了证书的别名 -->
        <arg value="recyclersign" />
    </exec>
     <echo message="签名成功" />  
</target>
<!-- 优化 -->
<target name="optimize-app" depends="jarsigner">
  <echo> optimizing the application...</echo>
  <exec executable="${android-zipalign}">
    <arg value="-v" />
    <arg value="-f" />
    <arg value="4" />
    <arg value="${out-signed-package-ospath}" />
    <arg value="${zipalign-signed-package}" />
 </exec>
</target>
<!-- 发布 -->
<target name="release" depends="optimize-app">
    <!-- 删除未签证apk -->
    <delete file="${out-unsigned-package-ospath}" />
    <echo>APK is released. path:${out-signed-package-ospath}</echo>
    <copy file="${out-signed-package-ospath}" tofile="${outdir-package}"></copy>
    <echo>APK is copy to path:${outdir-package}</echo>
</target>


2个回答

CSDNXIAON
CSDNXIAON   2016.05.09 17:52

Android通过ant脚本打包带Library project的方法
Android通过ant脚本打包带Library project的方法
Android ant 脚本打包(一)
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

BINGDUCAIJUN
BINGDUCAIJUN 我就参考的这些文章,然后出的问题··························
一年多之前 回复
BINGDUCAIJUN
BINGDUCAIJUN   2016.05.10 09:02

我就参考的这些文章,然后出的问题··························

Csdn user default icon
上传中...
上传图片
插入图片