android手动编译打包apk

首先,还是先来看看android整体的编译打包流程:
在这里插入图片描述

1、资源打包

1)将AndroidManifest.xml与res目录除了assets目录以外的文件进行编译成R.java:

aapt package -m -J build -S res -I D:/Android/Sdk/platforms/android-27/android.jar -M AndroidManifest.xml

-f 如果编译出来的文件已经存在,强制覆盖。

-m 使生成的包的目录放在-J参数指定的目录。

-J 指定生成的R.java的输出目录

-S res文件夹路径

-A assets文件夹的路径

-M AndroidManifest.xml的路径

-I 某个版本平台的android.jar的路径

-F 具体指定apk文件的输出目录,也就是资源压缩包的目录

2)将res跟asset打包进资源压缩包

aapt package -f -M AndroidManifest.xml -I D:/Android/Sdk/platforms/android-27/android.jar -S res -A assets  -F ./build/res.zip

3)上面两步合并执行亦可

aapt package -f -m -J build -S res -A assets -M AndroidManifest.xml -I D:/Android/Sdk/platforms/android-27/android.jar  -F ./build/res.zip

2、java编译:

1)aidl生成java

aidl -Iaidl -pD:/Android/Sdk/platforms/android-27/framework.aidl -obuild aidl/com/android/vending/billing/IInAppBillingService.aidl 

-I 指定import语句的搜索路径,注意-I与目录之间一定不要有空格
-p 指定系统类的import语句路径,如果是要用到android.os.Bundle系统的类,一定要设置sdk的framework.aidl 路径
-o 生成java文件的目录,注意-o与目录之间一定不要有空格,而且这设置项一定要在aidl文件路径之前设置

2)java编译为.class

dir /b/s *.java > all.txt(先将当前目录下的所有java文件的全路径保存到all.txt文件)

javac -target 1.8 -bootclasspath D:/Android/Sdk/platforms/android-27/android.jar -d ./build @all.txt(通过@all.txt将其内容读取出来)

3、dex生成:

dx --dex --output=./build/classes.dex ./build

4、生成apk

java -classpath D:/Android/Sdk/tools/lib/sdklib-26.0.0-dev.jar com.android.sdklib.build.ApkBuilderMain source.apk -v -u -z build/res.zip -f build/classes.dex

【注意】
1.如果需要将so文件打包进apk,一定要加上-nf参数
2.如果第三方jar包里含有图片资源,一定要加上-rj参数,不然jar包里资源文件解不出来,程序会因为无法引用资源而报错

-v Verbose.
-d Debug Mode: Includes debug files in the APK file.
-u Creates an unsigned package.
-storetype Forces the KeyStore type. If omitted the default is used.
-z Followed by the path to a zip archive.Adds the content of the application package.
-f Followed by the path to a file.Adds the file to the application package.
-rf Followed by the path to a source folder. Adds the java resources found in that folder to the application package, while keeping their path relative to the source folder.
-rj Followed by the path to a jar file or a folder containing jar files. Adds the java resources found in the jar file(s) to the application package.
-nf Followed by the root folder containing native libraries to include in the application package.

5、apk签名

apksigner sign --ks demo.jks --in source.apk --out source-signed.apk

或者

  apksigner sign -v --ks <keystore路径> --ks-key-alias <keystore别名>  --ks-pass pass:<keystore密码> --key-pass pass:<key保护密码> --in <未签名的apk路径>  --out <已签名的apk路径>

6、优化apk

zipalign -v 4 source-signed.apk source-signed-align.apk

7、apk签名验证

apksigner verify -v --print-certs <apk路径>
发布了36 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43278826/article/details/86508751