人生难得几回搏,此时不搏待何时。这篇文章主要讲述Android打包相关的知识,希望能为你提供帮助。
- 01.android打包流程图
-(1)必须使用AS的SDK(纯净版),避免出问题不能Eclipse-(2)建议使用Eclipse学习打包,Eclipse目录结构简单
文章图片
1.生成R文件,调用命令aapt.exe(sdkuild-tools23.0.3)
2.编译aidl文件,调用命令aidl(sdkuild-tools23.0.3)
3.编译所有的java文件,调用命令jdk的javac.exe
4.解压第三方jar包,调用命令jdk的jar
5.生成dex文件,调用命令dx.bat(sdkuild-tools23.0.3)
6.生成资源包文件resources.ap_,调用命令aapt.exe
7.生成未签名apk,调用命令apkbuilder.bat(sdk ools)
8.生成签名apk,调用命令jdk的jarsigner.exe
- 02.命令行打包生成R文件
为了在任何目录都能够调用aapt.exe命令,需要配置到path环境变量
命令
E:akclassCmdDemo> aapt p -f -m -J buildgen -S res -I D:ProgramFilesandroidsdkplatformsandroid-18android.jar -M AndroidManifest.xml
- 03.命令行打包编译java文件
注意:使用jdk不能使用1.8,建议使用1.7
命令
E:akclassCmdDemo> D:ProgramFilesJava1.7jdk1.7.0_80injavac.exe -encoding utf-8 -bootclasspath D:ProgramFilesAndroidsdkplatformsandroid-18android.jar -cp libsandroid-support-v4.jar -d buildclasses srccom gybcmddemoMainActivity.java buildgencom gybcmddemoR.java
- 04.命令行打包解压第三方jar包
命令
jar xvf ....libsandroid-support-v4.jar
- 05.命令行打包生成dex文件
命令所在目录sdkuild-tools23.0.3dx.bat
使用jdk1.8,生成dex文件报错:建议使用jdk1.7
文章图片
解决上述问题:重新编译java文件,使用jdk1.7
dx --dex --output=E:akclassCmdDemouildclasses.dex E:akclassCmdDemouildclasses
- 06.命令行打包编译资源文件
E:akclassCmdDemo> aapt p -f -M AndroidManifest.xml -S res -A assets -I D:ProgramFilesAndroidsdkplatformsandroid-18android.jar -F build esoures.ap_
- 07.命令行打包生成未签名apk文件
命令
E:akclassCmdDemo> apkbuilder E:akclassCmdDemouildDemo_unsigned.apk -u -z E:akclassCmdDemouild esoures.ap_ -f E:akclassCmdDemouildclasses.dex -rf E:akclassCmdDemosrc -rj E:akclassCmdDemolibs
- 08.生成keystore
debug.keystore目录:C:Users用户名.androiddebug.keystore
debug.keystore运行测试项目,给apk签名,上线项目必须使用公司自己的keystore
文章图片
生成keystore图形化界面
文章图片
生成keystore命令(秘钥对称:公钥和使用)
E:akclassCmdDemo> keytool -genkeypair -alias ngyb -keyalg "RSA" -validity 3650 -keystore ngyb.jks
- 09.命令行打包生成签名apk文件
通过命令行获取keystore别名
keytool -list -keystore ngyb.jks -v
命令
E:akclassCmdDemo> jarsigner -keystore ngyb.jks -storepass 123456 -keypass 123456 -signedjar buildDemo_signed.apk buildDemo_unsigned.apk ngyb
- 10.手机卫士打包生成R文件
aapt p -f -m -J buildgen -S res -I D:ProgramFilesAndroidsdkplatformsandroid-18android.jar -M AndroidManifest.xml
- 11.手机卫士打包编译aidl文件
【Android打包】编译2个aidl文件
E:南宫燚滨day1MobileSafe> aidl -Isrc -pD:ProgramFilesAndroidsdkplatformsandroid-18framework.aidl -obuildgen srcandroid elephonyNeighboringCellInfo.aidl E:南宫燚滨day1MobileSafe> aidl -Isrc -pD:ProgramFilesAndroidsdkplatformsandroid-18framework.aidl -obuildgen srccomandroidinternal elephonyITelephony.aidl
- 12.手机卫士打包编译java文件
D:ProgramFilesJava1.7jdk1.7.0_80injavac.exe -encoding utf-8 -bootclasspath D:ProgramFilesAndroidsdkplatformsandroid-18android.jar -cp libsandroid-support-v4.jar; libs ineoldandroids-2.4.0.jar; -d buildclasses buildgencomandroidinternal elephonyITelephony.java buildgencom gybsafeR.java srccom gybsafeactivityAppManagerActivity.java srccom gybsafeactivityBlackEditActivity.java srccom gybsafeactivityCallSmsSafeActivity.java srccom gybsafeactivityCommonNumberActivity.java srccom gybsafeactivityCommonNumberActivity.java srccom gybsafeactivityCommonToolActivity.java srccom gybsafeactivityHomeActivity.java srccom gybsafeactivityNumberAdressActivity.java srccom gybsafeactivitySelectContactActivity.java srccom gybsafeactivitySelectContactActivity2.java srccom gybsafeactivitySettingActivity.java srccom gybsafeactivitySjfdActivity.java srccom gybsafeactivitySjfdBaseSetupActivity.java srccom gybsafeactivitySjfdSetupActivity1.java srccom gybsafeactivitySjfdSetupActivity2.java srccom gybsafeactivitySjfdSetupActivity3.java srccom gybsafeactivitySjfdSetupActivity4.java srccom gybsafeactivitySjfdSetupActivity5.java srccom gybsafeactivitySplashActivity.java srccom gybsafeeanAddressBean.java srccom gybsafeeanAppBean.java srccom gybsafeeanBlackBean.java srccom gybsafeeanContactBean.java srccom gybsafeeanHomeBean.java srccom gybsafeusinessAppProvider.java srccom gybsafedbBlackDao.java srccom gybsafedbBlackDB.java srccom gybsafedbBlackDBOpenHelper.java srccom gybsafedbCommonNumberDao.java srccom gybsafedbNumberAddressDao.java srccom gybsafe eceiverBootcompletedReceiver.java srccom gybsafe eceiverSafeAdminReceiver.java srccom gybsafe eceiverSmsReceiver.java srccom gybsafeserviceCallSmsSafeService.java srccom gybsafeserviceGPSService.java srccom gybsafeserviceNumberAddressService.java srccom gybsafe estTestBlackDao.java srccom gybsafeutilsConstants.java srccom gybsafeutilsContactUtils.java srccom gybsafeutilsGZipUtils.java srccom gybsafeutilsPackageUtils.java srccom gybsafeutilsPreferenceUtils.java srccom gybsafeutilsServiceStateUtils.java srccom gybsafeviewAddressDialog.java srccom gybsafeviewAddressToast.java srccom gybsafeviewFocusedTextView.java srccom gybsafeviewProgressStateView.java srccom gybsafeviewSettingItemView.java
- 13.手机卫士打包解压第三方jar包
jar xvf ....libsandroid-support-v4.jarjar xvf ....libs ineoldandroids-2.4.0.jar
- 14.手机卫士打包生成dex文件
E:南宫燚滨day1MobileSafe> dx --dex --output=E:南宫燚滨day1MobileSafeuildclasses.dex E:南宫燚滨day1MobileSafeuildclasses
- 15.手机卫士打包生成资源包文件
E:南宫燚滨day1MobileSafe> aapt p -f -M AndroidManifest.xml -S res -A assets -I D:ProgramFilesAndroidsdkplatformsandroid-18android.jar -F build esources.ap_
- 16.手机卫士打包生成未签名apk
E:南宫燚滨day1MobileSafe> apkbuilder E:南宫燚滨day1MobileSafeuildMobileSafe_unsigned.apk -u -z E:南宫燚滨day1MobileSafeuild esources.ap_ -f E:南宫燚滨day1MobileSafeuildclasses.dex -rf E:南宫燚滨day1MobileSafesrc -rj E:南宫燚滨day1MobileSafelibs
- 17.手机卫士打包生成签名apk
E:南宫燚滨day1MobileSafe> jarsigner -keystore ngyb.jks -storepass 123456 -keypass 123456 -signedjar buildMobileSafe_signed.apk buildMobileSafe_unsigned.apk ngyb
签名apk多出目录:META-INF
- 18.回顾打包流程图
2.编译aidl文件 -> 生成java文件,调用sdk的aidl.exe
3.编译所有的java文件,生成class文件,调用1.7jdk的javac.exe
4.解压第三方jar包,生成class文件,调用jdk的jar.exe
5.生成classes.dex文件,调用命令dx.bat
6.编译资源文件,生成resources.ap_,调用命令aapt
7.生成未签名apk,调用apkbuilder.bat,不能安装
8.生成签名apk,调用jdk的jarsigner.exe
- 19.简单了解非对称加密RSA
秘钥对:公钥和私钥
公钥加密私钥解密;私钥加密公钥解密
- 20.ant打包集成ant
项目根目录创建build.xml
- 21.ant打包简单语法
执行ant命令,需要安装环境apache-ant-1.9.7,需要调用ant.bat命令编译ant脚本文件
调用语法:ant target任务名称
< project name="MobileSafe"> < target name="hello"> < echo message="你好,我是ant"> < /echo> < /target> < /project>
22.ant打包常用语法
< ?xml version="1.0" encoding="UTF-8"?>
< !-- < !DOCTYPE project SYSTEM "ant.dtd" > -->
< !-- 为了快速入门:代码提示 -->
< !-- project:项目跟标签 ,name属性项目名称,default:默认执行的任务,basedir,-->
< !-- ant:一切皆任务,任务为驱动 -->
< !-- target:任务标签 -->
< !-- echo:打印输出标签,输出内容通过message属性指定 -->
< !-- 创建目录标签mkdir,通过dir属性指定目录位置 -->
< !-- 删除目录标签delete,通过dir属性指定删除目录 -->
< !-- depends:属性,指定任务直接的依赖关系,多个任务逗号隔开 -->
< project name="MobileSafe" default="hello">
< !-- 相当于抽象成员变量 -->
< property name="buildDir" location="E: gybMobileSafeuild_new"/>
< target name="hello">
< echo message="你好,我是ant"> < /echo>
< /target>
< target name="hello2">
< echo message="${buildDir}"> < /echo>
< /target>
< !-- 先删除,在创建目录 -->
< target name="deleteDir">
< delete dir="E: gybMobileSafeuild_new"/>
< /target>
< !-- 创建目录 -->
< target name="createDir" depends="deleteDir">
< mkdir dir="${buildDir}"/>
< /target>
< !-- 删除目录 -->
< !-- < target name="deleteDir">
< delete dir="E: gybMobileSafeuild_new"/>
< /target> -->
< /project>
- 23.ant打包手机卫士初始化
< !--项目初始化:删除build目录,创建build目录 --> < target name="initProject"> < echo message="初始化任务"/> < delete dir="E: gybMobileSafeuild"/> < mkdir dir="E: gybMobileSafeuildgen"/> < /target>
- 24.ant打包手机卫士-生成R文件
< target name="generateR" depends="initProject"> < exec executable="${aapt.exe}"> < arg value="https://www.songbingjia.com/android/p"/> < arg value="https://www.songbingjia.com/android/-f"/> < arg value="https://www.songbingjia.com/android/-m"/> < arg value="https://www.songbingjia.com/android/-J"/> < arg value="https://www.songbingjia.com/android/${genDir}"/> < arg value="https://www.songbingjia.com/android/-S"/> < arg value="https://www.songbingjia.com/android/${resDir}"/> < arg value="https://www.songbingjia.com/android/-I"/> < arg value="https://www.songbingjia.com/android/${android.jar}"/> < arg value="https://www.songbingjia.com/android/-M"/> < arg value="https://www.songbingjia.com/android/${manifest.xml}"/> < /exec>
- 25.ant打包手机卫士-编译aidl文件
< target name="compileAidl" depends="generateR"> < echo message="任务2:编译aidl文件"/> < apply executable="${aidl.exe}"> < arg value="https://www.songbingjia.com/android/-I${srcDir}"/> < arg value="https://www.songbingjia.com/android/-p${framework.aidl}"/> < arg value="https://www.songbingjia.com/android/-o${genDir}"/> < fileset includes="**/*.aidl" dir="${srcDir}"/> < /apply>
- 26.ant打包手机卫士-编译java文件
< target name="compileJava" depends="compileAidl,initJavac"> < echo message="任务3:编译java文件"/> < javac encoding="utf-8"bootclasspath="${android.jar}"destdir="${classesDir}"target="1.7"source="1.7"executable="${javac.exe}"includeantruntime="true"> < classpath> < fileset includes="**/*.jar" dir="${libsDir}"/> < /classpath> < src path="${srcDir}"/> < src path="${genDir}"/> < /javac>
- 27.ant打包手机卫士-解压第三方jar包
< target name="unZipLibs" depends="compileJava"> < echo message="任务4:解压第三方jar包"/> < unjar dest="${classesDir}"> < !-- fileset:指定解压jar包路径、类型 --> < fileset dir="${libsDir}" includes="**/*.jar"/> < /unjar>
- 28.ant打包手机卫士-生成dex文件
文章图片
< target name="compileDex" depends="unZipLibs"> < echo message="任务5:生成dex文件"/> < exec executable="${dx.bat}"> < arg value="https://www.songbingjia.com/android/--dex"/> < arg value="https://www.songbingjia.com/android/--output=${classes.dex}"/> < arg value="https://www.songbingjia.com/android/${classesDir}"/> < /exec>
- 28.ant打包手机卫士-生成资源包文件
< target name="compileResources" depends="compileDex"> < echo message="任务6:编译资源文件"/> < exec executable="${aapt.exe}"> < arg value="https://www.songbingjia.com/android/p"/> < arg value="https://www.songbingjia.com/android/-f"/> < arg value="https://www.songbingjia.com/android/-M"/> < arg value="https://www.songbingjia.com/android/${manifest.xml}"/> < arg value="https://www.songbingjia.com/android/-S"/> < arg value="https://www.songbingjia.com/android/${resDir}"/> < arg value="https://www.songbingjia.com/android/-A"/> < arg value="https://www.songbingjia.com/android/${basedir}assets"/> < arg value="https://www.songbingjia.com/android/-I"/> < arg value="https://www.songbingjia.com/android/${android.jar}"/> < arg value="https://www.songbingjia.com/android/-F"/> < arg value="https://www.songbingjia.com/android/${resourcesAp}"/> < /exec>
- 29.ant打包手机卫士-生成未签名apk
< target name="generateUnsignedApk" depends="compileResources"> < echo message="7.生成未签名apk"/> < exec executable="${apkbuilder.bat}"> < arg value="https://www.songbingjia.com/android/${unsignedApk}"/> < arg value="https://www.songbingjia.com/android/-u"/> < arg value="https://www.songbingjia.com/android/-z"/> < arg value="https://www.songbingjia.com/android/${resourcesAp}"/> < arg value="https://www.songbingjia.com/android/-f"/> < arg value="https://www.songbingjia.com/android/${classes.dex}"/> < arg value="https://www.songbingjia.com/android/-rf"/> < arg value="https://www.songbingjia.com/android/${srcDir}"/> < arg value="https://www.songbingjia.com/android/-rj"/> < arg value="https://www.songbingjia.com/android/${libsDir}"/> < /exec>
- 31.ant打包手机卫士-生成签名apk
< target name="generateSignedApk" depends="generateUnsignedApk"> < echo message="任务8:生成签名apk"/> < exec executable="${jarsigner.exe}"> < arg value="https://www.songbingjia.com/android/-keystore"/> < arg value="https://www.songbingjia.com/android/${ngyb.jks}"/> < arg value="https://www.songbingjia.com/android/-storepass"/> < arg value="https://www.songbingjia.com/android/123456"/> < arg value="https://www.songbingjia.com/android/-keypass"/> < arg value="https://www.songbingjia.com/android/123456"/> < arg value="https://www.songbingjia.com/android/-signedjar"/> < arg value="https://www.songbingjia.com/android/${signedApk}"/> < arg value="https://www.songbingjia.com/android/${unsignedApk}"/> < arg value="https://www.songbingjia.com/android/ngyb"/> < /exec>
- 32.ant打包手机卫士-收尾
1.所有property统一管理
2.环境变量
< property environment="env"> < /property> < target name="printEnv"> < echo message="android的SDK环境变量:${env.JAVA_HOME}"/> < /target>
ant脚本使用环境变量
< property environment="env"> < /property> < property name="android_sdk" location="${env.ANDROID_SDK}"/> < property name="aapt.exe" location="${android_sdk}uild-tools23.0.3aapt.exe"/>
工具软件
http://www.androiddevtools.cn/
快速搜索软件:http://www.listary.com/
推荐阅读
- Android的Fragment中的互相通信-桥梁activity
- Vant Weapp小程序蹲坑之navigateTo:fail page not found
- http://dl-ssl.google.com/android上不去解决方案
- Spring集成MyBatis的使用-使用Mapper映射器
- android 6.0.1 compiling
- MyBatis数据库连接的基本使用-补充Mapper映射器
- 安卓手机微信不能打开App下载地址的问题 微信跳转打开外部浏览器下载
- 如何运用u盘给神州装win8系统,本文教您如何运用
- 台式机用u盘装系统,本文教您用u盘装系统给台式