命令行构建Android应用
发布时间:2022-01-14 15:29:25.656 文章来源:AiSoftCloud 浏览次数:757 下载次数:1 

安装Android SDK tools

下载android-sdk:http://tools.android-studio.org/index.php/sdk/

linux:

  1. wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz

mac:

  1. wget http://dl.google.com/android/android-sdk_r24.4.1-macosx.zip

windows:

  1. wget http://dl.google.com/android/android-sdk_r24.4.1-windows.zip

解压:

  1. tar xzvf android-sdk_r24.4.1-linux.tgz

设置环境变量:

  1. export ANDROID_HOME=${PWD}/android-sdk-linux
  2. export PATH=${ANDROID_HOME}/tools:${PATH}
  3. export PATH=${ANDROID_HOME}/build-tools/30.0.0-preview:${PATH}

更新SDK:

  1. # 显示当前安装的sdk
  2. android list sdk
  3. # 更新sdk
  4. android update sdk --no-ui

创建一个android工程:

  1. android --verbose create project --name TestApp --activity MainActivity --package com.test --target 21 --path TestApp

生成R.java:

  1. cd TestApp
  2. mkdir gen
  3. aapt package -v -f -S res -J gen -M AndroidManifest.xml -I $ANDROID_HOME/platforms/android-21/android.jar
  4. cat gen/R.java

编译代码:

  1. mkdir obj
  2. javac -verbose -d obj -classpath $ANDROID_HOME/platforms/android-21/android.jar -sourcepath src src/com/test/MainActivity.java gen/R.java

创建 DEX 文件:

Android 系统使用 Dalvik Virtual Machine 来运行字节码。Dalvik 并不认识 .class 格式的字节码,因此,需要把 .class 文件翻译成 Dalvik 可以识别的字节码格式:

  1. dx --dex --verbose --output=bin/classes.dex obj libs

生成 APK 文件:

  1. aapt package -v -f -M AndroidManifest.xml -S res -I $ANDROID_HOME/platforms/android-21/android.jar -F bin/TestApp-unaligned.unsigned.apk bin

给 APK 签名:

  1. mkdir keystores
  2. keytool -v -genkeypair -validity 10000 -dname "CN=company_name,OU=organisational_unit,O=organisation,L=location,S=state,C=country_code" -keystore keystores/testapp.keystore -storepass store_password -keypass key_password -alias key_alias -keyalg RSA

给APK打签名:

  1. jarsigner -verbose -keystore keystores/testapp.keystore -keypass key_password -signedjar bin/TestApp-unaligned.signed.apk bin/TestApp-unaligned.unsigned.apk key_alias

ZIP 对齐:

  1. zipalign -v -f 4 bin/TestApp-unaligned.signed.apk bin/TestApp.apk

完整的脚本

  1. #!/bin/bash
  2. ANDROID_HOME=/Users/yangxiaokai/baidu/tmp/android-sdk-linux
  3. export PATH=${ANDROID_HOME}/tools:${PATH}
  4. export PATH=${ANDROID_HOME}/build-tools/30.0.0-preview:${PATH}
  5. PROJECT_NAME=TestApp
  6. # 显示当前安装的sdk
  7. android list sdk
  8. # 更新sdk
  9. android update sdk --no-ui
  10. android --verbose create project --name ${PROJECT_NAME} --activity MainActivity --package com.test --target 21 --path ${PROJECT_NAME}
  11. cd ${PROJECT_NAME}
  12. mkdir gen
  13. aapt package -v -f -S res -J gen -M AndroidManifest.xml -I $ANDROID_HOME/platforms/android-21/android.jar
  14. cat gen/R.java
  15. mkdir obj
  16. javac -verbose -d obj -classpath $ANDROID_HOME/platforms/android-21/android.jar -sourcepath src src/com/test/MainActivity.java gen/R.java
  17. dx --dex --verbose --output=bin/classes.dex obj libs
  18. aapt package -v -f -M AndroidManifest.xml -S res -I $ANDROID_HOME/platforms/android-21/android.jar -F bin/${PROJECT_NAME}-unaligned.unsigned.apk bin
  19. #mkdir keystores
  20. #keytool -v -genkeypair -validity 10000 -dname "CN=company_name,OU=organisational_unit,O=organisation,L=location,S=state,C=country_code" -keystore keystores/testapp.keystore -storepass store_password -keypass key_password -alias key_alias -keyalg RSA
  21. #jarsigner -verbose -keystore keystores/testapp.keystore -keypass key_password -signedjar bin/${PROJECT_NAME}-unaligned.signed.apk bin/${PROJECT_NAME}-unaligned.unsigned.apk key_alias

参考文章

从命令行构建 Android 应用

更多文章可关注公众号
aisoftcloud