Android使用Ant自動編譯簽名打包詳解
1.生成R.java類檔案:
- <!-- Generate the R.java file for this project's resources. -->
- <target name="resource-src" depends="copy">
- <echo>Generating R.java / Manifest.java from the resources...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-m" />
- <arg value="-J" />
- <arg value="${outdir-gen}" />
- <arg value="-M" />
- <arg value="AndroidManifest.xml" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- </exec>
- </target>
2.將.aidl檔案生成.java類檔案:
- <!-- Generate java classes from .aidl files. -->
- <target name="aidl" depends="copy">
- <echo>Compiling aidl files into Java classes...</echo>
- <apply executable="${aidl}" failonerror="true">
- <arg value="-p${android-framework}" />
- <arg value="-I${srcdir}" />
- <fileset dir="${srcdir}">
- <include name="**/*.aidl" />
- </fileset>
- </apply>
- </target>
3.編譯.java類檔案生成class檔案:
- <!-- Compile this project's .java files into .class files. -->
- <target name="compile" depends="copy, resource-src, aidl">
- <javac encoding="GB18030" target="1.5" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}">
- <classpath>
- <fileset dir="${external-libs}" includes="*.jar" />
- </classpath>
- </javac>
- </target>
- <!-- Convert this project's .class files into .dex files. -->
- <target name="dex" depends="compile">
- <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
- <apply executable="${dx}" failonerror="true" parallel="true">
- <arg value="--dex" />
- <arg value="--output=${intermediate-dex-ospath}" />
- <arg path="${outdir-obfuscate-classes-ospath}" />
- <fileset dir="${external-libs}" includes="*.jar" />
- </apply>
- </target>
5.打包資原始檔(包括res、assets、androidmanifest.xml等):
- <!-- Put the project's resources into the output package file. -->
- <target name="package-res-and-assets">
- <echo>Packaging resources and assets...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-f" />
- <arg value="-M" />
- <arg value="AndroidManifest.xml" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-A" />
- <arg value="${asset-dir}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- <arg value="-F" />
- <arg value="${resources-package}" />
- </exec>
- </target>
6.生成未簽名的apk安裝檔案:
- <target name="package" depends="dex, package-res">
- <echo>Packaging ${out-unsigned-package} for release...</echo>
- <exec executable="${apk-builder}" failonerror="true">
- <arg value="${out-unsigned-package-ospath}" />
- <arg value="-u" />
- <arg value="-z" />
- <arg value="${resources-package-ospath}" />
- <arg value="-f" />
- <arg value="${intermediate-dex-ospath}" />
- <arg value="-rf" />
- <arg value="${srcdir-ospath}" />
- <arg value="-rj" />
- <arg value="${external-libs-ospath}" />
- </exec>
- <echo>It will need to be signed with jarsigner before being published.</echo>
- </target>
- <target name="jarsigner" depends="package">
- <echo>Packaging ${out-unsigned-package} for release...</echo>
- <exec executable="${jarsigner}" failonerror="true">
- <arg value="-verbose" />
- <arg value="-storepass" />
- <arg value="byread002" />
- <arg value="-keypass" />
- <arg value="byread002" />
- <arg value="-keystore" />
- <arg value="bbyread.keystore" />
- <arg value="-signedjar" />
- <arg value="${out-signed-package-ospath}" />
- <arg value="${out-unsigned-package-ospath}" />
- <arg value="byread" />
- </exec>
- </target>
*完整Ant配置檔案build.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="Byread" default="debug">
- <property environment="env" />
- <property name="sdk-folder" value="${env.ANDROID_SDK_HOME}" />
- <!-- SDK Locations -->
- <property name="sdk2.2-folder" value="${env.ANDROID_SDK_HOME}" />
- <property name="sdk-folder" value="${sdk2.2-folder}/platforms/android-3" />
- <property name="sdk-tools" value="${sdk-folder}/tools" />
- <property name="android-tools" value="${sdk2.2-folder}/tools" />
- <property name="proguardpath" location="${wtkhome}/lib/proguard" />
- <!-- step 1.generate R.java by aapt 2.compile java to class by javac。exe 3.generate classes.dex by dx.bat -->
- <!-- 4.package resources by aapt 5. package resource and classes.dex by apkbuilder.bat 6.sign apk by jarsinger -->
- <!-- Tools -->
- <property name="aapt" value="${sdk-tools}/aapt.exe" />
- <property name="dx" value="${sdk-tools}/dx.bat" />
- <property name="apk-builder" value="${android-tools}/apkbuilder.bat" />
- <property name="aidl" value="${android-tools}/aidl.exe" />
- <property name="adb" value="${android-tools}/adb.exe" />
- <property name="android-jar" value="${sdk-folder}/android.jar" />
- <property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner.exe" />
- <!-- Application Package Name -->
- <property name="application-package" value="com.byread.reader" />
- <property name="useragent" value="byAndroidWeb" />
- <property name="version" value="1.02" />
- <!-- The intermediates directory -->
- <!-- Eclipse uses "bin" for its own output, so we do the same. -->
- <property name="outdir-bin" value="bin" />
- <property name="outdir-gen" value="gen" />
- <!-- source directories -->
- <property name="resource-dir" value="res" />
- <property name="asset-dir" value="assets" />
- <property name="srcdir" value="src" />
- <property name="srcdir-ospath" value="${basedir}/${srcdir}" />
- <property name="external-libs" value="libs" />
- <property name="external-libs-ospath" value="${basedir}/${external-libs}" />
- <!-- dest directories -->
- <property name="des-resource-dir" value="${outdir-bin}/res" />
- <property name="des-asset-dir" value="${outdir-bin}/assets" />
- <property name="des-srcdir" value="${outdir-bin}/src" />
- <property name="des-srcdir-ospath" value="${basedir}/${outdir-bin}/${srcdir}" />
- <property name="des-external-libs" value="${outdir-bin}/libs" />
- <property name="des-external-libs-ospath" value="${basedir}/${outdir-bin}/${external-libs}" />
- <!-- Output directories -->
- <property name="outdir-classes" value="${outdir-bin}/src" />
- <property name="outdir-obfuscate-classes" value="${outdir-bin}/classes" />
- <property name="outdir-obfuscate-classes-ospath" value="${basedir}/${outdir-obfuscate-classes}" />
- <!-- Intermediate files -->
- <property name="dex-file" value="classes.dex" />
- <property name="intermediate-dex" value="${outdir-bin}/${dex-file}" />
- <property name="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" />
- <!-- The final package file to generate -->
- <property name="resources-package" value="${outdir-bin}/${ant.project.name}" />
- <property name="resources-package-ospath" value="${basedir}/${resources-package}" />
- <property name="out-debug-package" value="${outdir-bin}/${ant.project.name}-debug.apk" />
- <property name="out-debug-package-ospath" value="${basedir}/${out-debug-package}" />
- <property name="out-unsigned-package" value="${outdir-bin}/${ant.project.name}-unsigned.apk" />
- <property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />
- <property name="out-signed-package" value="${useragent}/${ant.project.name}.apk" />
- <property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />
- <!-- init -->
- <target name="init">
- <echo>Creating all output directories </echo>
- <delete dir="${outdir-bin}" />
- <delete dir="${useragent}" />
- <mkdir dir="${outdir-bin}" />
- <mkdir dir="${outdir-classes}" />
- <mkdir dir="${useragent}" />
- </target>
- <!-- copy original strings and modify useragent -->
- <target name="copy" depends="init">
- <echo>copy files to output folder</echo>
- <delete file="${resource-dir}/values/strings.xml" />
- <copy file="strings.xml" todir="${resource-dir}/values" />
- <replace file="${resource-dir}/values/strings.xml" token="@USERAGENT@" value="${useragent}" encoding="utf-8" />
- </target>
- <!-- Generate the R.java file for this project's resources. -->
- <target name="resource-src" depends="copy">
- <echo>Generating R.java / Manifest.java from the resources...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-m" />
- <arg value="-J" />
- <arg value="${outdir-gen}" />
- <arg value="-M" />
- <arg value="AndroidManifest.xml" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- </exec>
- </target>
- <!-- Generate java classes from .aidl files. -->
- <target name="aidl" depends="copy">
- <echo>Compiling aidl files into Java classes...</echo>
- <apply executable="${aidl}" failonerror="true">
- <arg value="-p${android-framework}" />
- <arg value="-I${srcdir}" />
- <fileset dir="${srcdir}">
- <include name="**/*.aidl" />
- </fileset>
- </apply>
- </target>
- <!-- Compile this project's .java files into .class files. -->
- <target name="compile" depends="copy, resource-src, aidl">
- <javac encoding="GB18030" target="1.5" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}">
- <classpath>
- <fileset dir="${external-libs}" includes="*.jar" />
- </classpath>
- </javac>
- </target>
- <!-- Convert this project's .class files into .dex files. -->
- <target name="dex" depends="compile">
- <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
- <apply executable="${dx}" failonerror="true" parallel="true">
- <arg value="--dex" />
- <arg value="--output=${intermediate-dex-ospath}" />
- <arg path="${outdir-obfuscate-classes-ospath}" />
- <fileset dir="${external-libs}" includes="*.jar" />
- </apply>
- </target>
- <!-- Put the project's resources into the output package file. -->
- <target name="package-res-and-assets">
- <echo>Packaging resources and assets...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-f" />
- <arg value="-M" />
- <arg value="AndroidManifest.xml" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-A" />
- <arg value="${asset-dir}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- <arg value="-F" />
- <arg value="${resources-package}" />
- </exec>
- </target>
- <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->
- <target name="package-res-no-assets">
- <echo>Packaging resources...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-f" />
- <arg value="-M" />
- <arg value="AndroidManifest.xml" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <!-- No assets directory -->
- <arg value="-I" />
- <arg value="${android-jar}" />
- <arg value="-F" />
- <arg value="${resources-package}" />
- </exec>
- </target>
- <!-- Invoke the proper target depending on whether or not
- an assets directory is present. -->
- <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument
- only when the assets dir exists. -->
- <target name="package-res">
- <available file="${asset-dir}" type="dir" property="res-target" value="and-assets" />
- <property name="res-target" value="no-assets" />
- <antcall target="package-res-${res-target}" />
- </target>
- <!-- Package the application and sign it with a debug key.
- This is the default target when building. It is used for debug. -->
- <target name="debug" depends="dex, package-res">
- <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
- <exec executable="${apk-builder}" failonerror="true">
- <arg value="${out-debug-package-ospath}" />
- <arg value="-z" />
- <arg value="${resources-package-ospath}" />
- <arg value="-f" />
- <arg value="${intermediate-dex-ospath}" />
- <arg value="-rf" />
- <arg value="${srcdir-ospath}" />
- <arg value="-rj" />
- <arg value="${external-libs-ospath}" />
- </exec>
- </target>
- <!-- Package the application without signing it.
- This allows for the application to be signed later with an official publishing key. -->
- <target name="package" depends="dex, package-res">
- <echo>Packaging ${out-unsigned-package} for release...</echo>
- <exec executable="${apk-builder}" failonerror="true">
- <arg value="${out-unsigned-package-ospath}" />
- <arg value="-u" />
- <arg value="-z" />
- <arg value="${resources-package-ospath}" />
- <arg value="-f" />
- <arg value="${intermediate-dex-ospath}" />
- <arg value="-rf" />
- <arg value="${srcdir-ospath}" />
- <arg value="-rj" />
- <arg value="${external-libs-ospath}" />
- </exec>
- <echo>It will need to be signed with jarsigner before being published.</echo>
- </target>
- <target name="jarsigner" depends="package">
- <echo>Packaging ${out-unsigned-package} for release...</echo>
- <exec executable="${jarsigner}" failonerror="true">
- <arg value="-verbose" />
- <arg value="-storepass" />
- <arg value="byread002" />
- <arg value="-keypass" />
- <arg value="byread002" />
- <arg value="-keystore" />
- <arg value="bbyread.keystore" />
- <arg value="-signedjar" />
- <arg value="${out-signed-package-ospath}" />
- <arg value="${out-unsigned-package-ospath}" />
- <arg value="byread" />
- </exec>
- </target>
- <target name="release" depends="jarsigner">
- <echo>release for release...</echo>
- </target>
- <!-- Install the package on the default emulator -->
- <target name="install" depends="debug">
- <echo>Installing ${out-debug-package} onto default emulator...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg value="install" />
- <arg value="${out-debug-package}" />
- </exec>
- </target>
- <target name="reinstall" depends="debug">
- <echo>Installing ${out-debug-package} onto default emulator...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg value="install" />
- <arg value="-r" />
- <arg value="${out-debug-package}" />
- </exec>
- </target>
- <!-- Uinstall the package from the default emulator -->
- <target name="uninstall">
- <echo>Uninstalling ${application-package} from the default emulator...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg value="uninstall" />
- <arg value="${application-package}" />
- </exec>
- </target>
- </project>
*在工程目錄下建立一個build.bat批處理檔案,內容為:
ant -buildfile build.xml
執行這個批處理檔案即可。
注意:SDK的安裝目錄最後不要有中文和空格,否則會出現不必要的錯誤。如果你的安裝路徑包含Program Files(有空格),Ant方式打包會報錯,沒關係,不用重灌,解決方法:配置SDk環境變數的時候把Program Files改為Progra~1即可。
轉自:http://jojol-zhou.iteye.com/blog/729271
相關文章
- Android自動打包、簽名、優化、上傳ANT指令碼Android優化指令碼
- Android筆記之:App自動化之使用Ant編譯專案多渠道打包的使用詳解Android筆記APP編譯
- Android 自動編譯、打包生成apk檔案 3 - 使用SDK Ant方式Android編譯APK
- 分享一個自動編譯,打包,簽名 android apk 的小指令碼編譯AndroidAPK指令碼
- 教你在Android自動打包解包簽名方法Android
- android使用ant編譯(rem)Android編譯REM
- Ant自動打包(可動態修改包名、資源等)生成不同簽字不同包名的APPAPP
- 通過ant指令碼編譯打包android工程指令碼編譯Android
- ant指令碼實現的Android自動編譯指令碼Android編譯
- Android ant自動打包指令碼:自動替換友盟渠道、版本號、包名Android指令碼
- 【Android自動化打包】03. APK的數字簽名AndroidAPK
- Android ant修改包名打包 (轉)Android
- iOS自動化編譯打包iOS編譯
- 簽名打包Android版apkAndroidAPK
- Android App的簽名打包AndroidAPP
- Android Studio 簽名打包過程圖解Android圖解
- Window下采用ant 指令碼構建Android自動化編譯指令碼Android編譯
- Android 打包簽名 從生成keystore到完成簽名Android
- Android打包簽名那點事Android
- 使用ANT打包Android應用Android
- Xamarin Android使用自簽名證書Android
- Ant 編譯、打包 build.xml 指令碼模板編譯UIXML指令碼
- android 利用shell指令碼重新打包簽名Android指令碼
- Android 生成簽名apk與多渠道打包AndroidAPK
- Android APK簽名原理和過程詳解AndroidAPK
- android 使用ANT批量打包apk步驟AndroidAPK
- OpenSSL 證書請求和自簽名命令 req 詳解
- Android使用Ant進行apk多渠道打包AndroidAPK
- 使用ant編譯Java檔案(一)編譯Java
- 使用ant編譯Java檔案(二)編譯Java
- Android Studio生成簽名檔案,自動簽名,以及獲取SHA1和Android
- 通過Ant將Android project編譯打包成APK檔案並安裝到手機AndroidProject編譯APK
- Gradle For Android(二) 多渠道打包與簽名配置GradleAndroid
- Gradle for Android(三)多渠道打包、配置簽名資訊GradleAndroid
- Android專案如何進行簽名打包釋出Android
- apktool反編譯和二次打包,jarsigner和apksigner進行簽名APK編譯JAR
- 利用ant編譯釋出打包jar檔案和打包api文件為rar檔案編譯JARAPI
- 蘋果軟體打包簽名原理蘋果