Shell 构建flutter + Android 生成Apk
#!/bin/bash
# 设置flutter和Android SDK的路径
FLUTTER_ROOT=/path/to/flutter
export PATH="$PATH:$FLUTTER_ROOT/bin"
ANDROID_SDK_ROOT=/path/to/android/sdk
export ANDROID_HOME=$ANDROID_SDK_ROOT
export PATH="$PATH:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools"
# 检查flutter和gradle命令是否可用
if ! command -v flutter &> /dev/null
then
echo "flutter命令未找到,请检查FLUTTER_ROOT路径是否正确"
exit 1
fi
if ! command -v gradle &> /dev/null
then
echo "gradle命令未找到,请确保已安装Gradle"
exit 1
fi
# 获取输入参数
RELEASE_MODE=$1
# 构建flutter应用
echo "开始构建flutter应用..."
if [ "$RELEASE_MODE" = "release" ]; then
flutter build apk --release
else
flutter build apk
fi
# 检查构建结果
if [ $? -ne 0 ]; then
echo "flutter构建失败"
exit 1
fi
echo "flutter构建成功"
# 获取apk路径
APK_PATH=$(find . -name "app-release.apk" | head -n 1)
# 检查apk是否生成
if [ ! -f "$APK_PATH" ]; then
echo "未找到生成的apk文件"
exit 1
fi
echo "APK路径: $APK_PATH"
# 执行后续的Android打包或其他脚本
# ...
# 结束
echo "构建APK完成"
这段代码首先设置了Flutter和Android SDK的环境变量,然后检查这些工具是否可用。接着,它接受一个参数来确定是否以发布模式构建Flutter应用,并且如果构建成功,它会打印出apk的路径。最后,它提供了一个框架来执行后续的Android打包或其他脚本。
评论已关闭