从命令行构建Android - 包似乎已损坏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从命令行构建Android - 包似乎已损坏相关的知识,希望对你有一定的参考价值。

android Studio对我的电脑来说太慢了,所以我自己动手学习如何手动构建Android应用程序。我完成了这个脚本:

#!/usr/bin/env bash

##################################################################
#  PREPARE ENVIRONMENT
################################################################## 
path=$(pwd)
rm -rf bin
rm -rf gen
mkdir bin
mkdir gen

##################################################################
#  SET VARS
##################################################################

# Set your application name
APP_NAME="mygame"

# Define minimal Android revision
ANDROID_REV="27.0.3"
ANDROID_PLATFORM_REV="android-27"

# Define Java compiler command
JAVAC="${JAVA_BIN}/javac -classpath ${ANDROID_SDK}/platforms/${ANDROID_PLATFORM_REV}/android.jar"
JAVAC_BUILD="${JAVAC} -sourcepath src -sourcepath gen -d bin"


##################################################################
#  PROCESS
##################################################################

# Generate R class and pack resources and assets into resources.ap_ file
${ANDROID_SDK}/build-tools/${ANDROID_REV}/aapt package -v -f -I ${ANDROID_SDK}/platforms/${ANDROID_PLATFORM_REV}/android.jar -M AndroidManifest.xml -A "assets" -S "res" -m -J "gen" -F bin/resources.ap_

# Compile sources. All *.class files will be put into the bin folder
${JAVAC_BUILD} src/com/mycompany/mygame/*.java

# Generate dex files with compiled Java classes
${ANDROID_SDK}/build-tools/${ANDROID_REV}/dx --dex --output=${path}/bin/classes.dex ${path}/bin

# Recources file need to be copied. This is needed for signing.
mv "${path}/bin/resources.ap_" "${path}/bin/${APP_NAME}.ap_"

# Add generated classes.dex file into application package
${ANDROID_SDK}/build-tools/${ANDROID_REV}/aapt add ${path}/bin/${APP_NAME}.ap_ ${path}/bin/classes.dex

${ANDROID_SDK}/build-tools/${ANDROID_REV}/zipalign -f -v 4 ${path}/bin/${APP_NAME}.ap_ ${path}/bin/${APP_NAME}_unsigned.ap_

# Create signed Android application from *.ap_ file.
${ANDROID_SDK}/build-tools/${ANDROID_REV}/apksigner sign --ks ${path}/keystore/etamagotchi-release-key.keystore  --ks-key-alias "mykeystore-alias" ${path}/bin/${APP_NAME}_unsigned.ap_

#
cp ${path}/bin/${APP_NAME}_unsigned.ap_ ${path}/bin/${APP_NAME}.apk

# Delete temp file
rm bin/*.ap_
rm bin/*.dex

它成功构建。如果我试图运行adb install mygame.apk它说:

[INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.mycompany.mygame-1: Package /data/app/com.mycompany.mygame-1/base.apk code is missing]

如果我手动尝试安装它说“包看起来已损坏”。我的构建脚本有什么问题?

答案

我将-source 1.7 -target 1.7 -bootclasspath "${JAVA_HOME}/jre/lib/rt.jar"添加到我的$JAVAC构建规则中

以上是关于从命令行构建Android - 包似乎已损坏的主要内容,如果未能解决你的问题,请参考以下文章

Android 安装包优化使用 lib7zr.so 动态库处理压缩文件 ( jni 中 main 函数声明 | 命令行处理 | jni 调用 lib7zr.so 函数库处理压缩文件完整代码 )(代码片

使用命令行工具构建 Android

npm 包似乎已损坏

Android Studio 错误“安装的构建工具修订版 31.0.0 已损坏”

Android Studio 错误“安装的构建工具修订版 31.0.0 已损坏”

Sencha Cmd 损坏:为啥“应用程序构建”会忽略任何命令行参数?