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

时间:2018-03-01 19:20:43

标签: android bash build apk adb

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]

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

1 个答案:

答案 0 :(得分:0)

我在-source 1.7 -target 1.7 -bootclasspath "${JAVA_HOME}/jre/lib/rt.jar"构建规则

中添加了$JAVAC