接着上文, 直接上 ant 脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| <?xml version="1.0" encoding="utf-8"?> <project default="package resources" basedir="."> <description>安卓构建脚本</description> <property name="project-dir" location="."/>
<property environment="env" /> <property name="java_home" value="${env.JAVA_HOME}"/> <property name="sdk-folder" value="${env.ANDROID_HOME}" /> <property name="sdk-platform-folder" value="${sdk-folder}/platforms/android-22"/> <property name="android-jar" value="${sdk-platform-folder}/android.jar" /> <property name="sdk-tools" value="${sdk-folder}/tools" /> <property name="build-tools" value="${sdk-folder}/build-tools/25.0.2" /> <property name="sdk-platform-tools" value="${sdk-platform-folder}/tools" />
<property name="aapt" value="${build-tools}/aapt" /> <property name="aidl" value="${build-tools}/aidl" /> <property name="dx" value="${build-tools}/dx.bat" /> <property name="zipalign" value="${build-tools}/zipalign" /> <property name="apkbuilder" value="${sdk-tools}/apkbuilder" /> <property name="jarsigner" value="${java_home}/bin/jarsigner" />
<path id="compile.classpath"> <fileset dir="libs"> <include name="*.jar" /> </fileset> </path>
<target name="init"> <mkdir dir="gen" /> <mkdir dir="bin/classes" /> </target>
<target name="generate R" depends="init" description="Package the android resources得到R.java文件"> <exec executable="${aapt}"> <arg value="package" /> <arg value="-f" /> <arg value="-m" /> <arg value="-M" /> <arg value="AndroidManifest.xml" /> <arg value="-I" /> <arg value="${android-jar}" /> <arg value="-S" /> <arg value="res" /> <arg value="-J" /> <arg value="gen" /> <arg value="--auto-add-overlay" /> </exec> </target>
<target name="compile javac" depends="generate R" description="Javac编译(包含src和gen目录)java文件"> <javac encoding="utf-8" source="1.6" target="1.6" bootclasspath="${android-jar}" destdir="bin/classes" includeantruntime="false"> <classpath refid="compile.classpath" /> <src path="src" /> <src path="gen" /> </javac> </target>
<target name="dx" depends="compile javac" description="打包class文件和jar包dx为classes.dex"> <exec executable="${dx}" failonerror="true"> <arg value="--dex" /> <arg value="--output=bin/classes.dex" /> <arg path="bin/classes" /> <arg path="libs/*.jar" /> </exec> </target>
<target name="package resources" depends="dx" description="打包assets和res资源为资源压缩包(例如res.zip"> <exec executable="${aapt}"> <arg value="package" /> <arg value="-f" /> <arg value="-M" /> <arg value="AndroidManifest.xml" /> <arg value="-I" /> <arg value="${android-jar}" /> <arg value="-A" /> <arg value="assets" /> <arg value="-S" /> <arg value="res" /> <arg value="-F" /> <arg value="bin/resources.ap_" /> <arg value="--auto-add-overlay" /> </exec> </target>
<target name="apkbuilder" depends="package resources"> <java classname="com.android.sdklib.build.ApkBuilderMain"> <classpath> <pathelement path="D:\Android\android-sdk\tools\lib\sdklib.jar " /> </classpath>
<arg value="bin/unsigned.apk" /> <arg value="-u" /> <arg value="-z" /> <arg value="bin/resources.ap_" /> <arg value="-f" /> <arg value="bin/classes.dex" /> <arg value="-rf" /> <arg value="src" /> <arg value="-rj" /> <arg value="libs" /> <arg value="-nf" /> <arg value="libs" /> </java> </target>
<target name="jarsigner" depends="apkbuilder"> <exec executable="${jarsigner}"> <arg value="-keystore" /> <arg value="C:\Users\hp\Desktop\保全_其他文档\密钥\android.keystore" /> <arg value="-storepass" /> <arg value="android" /> <arg value="-keypass" /> <arg value="android" /> <arg value="-signedjar" /> <arg value="bin/signed.apk" /> <arg value="bin/unsigned.apk" /> <arg value="androiddebugkey" /> </exec> </target>
<target name="zipalign" depends="jarsigner"> <exec executable="${zipalign}"> <arg value="-v" /> <arg value="4" /> <arg value="bin/signed.apk" /> <arg value="bin/signed_aligned.apk" /> </exec> </target>
<target name="clean" description="clean up"> <delete dir="gen" /> <delete dir="bin/classes" /> </target> </project>
|
由于我的项目中用到了 BuildConfig.java 文件, 在第一个生成 R 文件后我手动拷贝到了R.java的同级目录, 否则接下来的操作会报错
1 2 3 4 5 6
| package com.nci.insprotection;
public final class BuildConfig { public final static boolean DEBUG = true; }
|
注意
先手动编译通过在调整 ant 脚本, 否则很容易出错
参考
- https://blog.csdn.net/kevin_nazgul/article/details/48767101
- https://blog.csdn.net/javazejian/article/details/50579416