flutter_build.sh 3.64 KB
#!/bin/bash

build_ios=false
build_android=false
build_release=false
build_debug=false
build_ios_simulator=false

updatePodfile(){
    if [ "$build_ios" == true ] && [ -d ".ios" ]; then
        # 修改Podfile platform版本
        sed -i '' "1s/.*/platform :ios, \"17.0\"/" .ios/Podfile
		modifyPodfileConfigPermissionHandler
    fi
}

# 修改podfile文件: 解决permission_handler在iOS方授权的问题
modifyPodfileConfigPermissionHandler() {
    # 定义文件路径
    PODFILE=".ios/Podfile"
    PERMISSION_HANDLER_CONFIG="permission_handler_config"

	# 检查是否已经包含 `target.name == "permission_handler_apple"`
	if grep -q 'target.name == "permission_handler_apple"' "$PODFILE"; then
  		echo "Podfile already contains 'target.name == \"permission_handler_apple\"'. No insertion needed."
  		return 0
	fi

    # 查找插入位置
    LINE_NUMBER=$(grep -n 'installer.pods_project.targets.each do |target|' "$PODFILE" | cut -d: -f1)
    
    # 如果找到匹配行,则插入内容
    if [ ! -z "$LINE_NUMBER" ]; then
        # 将匹配行号加1,作为插入行号
        INSERT_LINE=$((LINE_NUMBER + 1))
    
        # 在指定行之后插入内容
        sed -i '' "${INSERT_LINE}r ${PERMISSION_HANDLER_CONFIG}" "$PODFILE"
    else
        echo "未找到匹配的行。"
    fi
}

checkSDKDir() {
	if [ "$build_ios" == true ] && [ -d "./SDK/iOS_SDK" ]; then
		rm -rf ./SDK/iOS_SDK/*
	else
		mkdir -p ./SDK/iOS_SDK
	fi

	if [ "$build_android" == true ] && [ -d "./SDK/Android_SDK" ]; then
		rm -rf ./SDK/Android_SDK/*
	else
		mkdir -p ./SDK/Android_SDK
	fi
}

buildIOSSDk() {
	echo "\033[33mBegin Build iOS SDK\033[0m"
	ios_framework_arch_options="--xcframework --no-universal"
	if [[ "$build_ios_simulator" == true ]]; then
		# XCFramework 同时包含真机与 ios-arm64_x86_64-simulator 架构。
		ios_framework_arch_options="--xcframework"
	fi
	if [[ "$build_release" == true ]]; then
		flutter build ios-framework --no-debug --no-profile $ios_framework_arch_options --output=./
		mv ./Release/* ./SDK/iOS_SDK
		rm -rf ./Release
	else
		flutter build ios-framework --no-release --no-profile $ios_framework_arch_options --output=./
		mv ./Debug/* ./SDK/iOS_SDK
		rm -rf ./Debug
	fi
	echo "\033[33miOS SDK Done\033[0m"
}

buildAndroidSDK() {
	echo "\033[33mBegin Build Android SDK\033[0m"
	if [[ "$build_release" == true ]]; then
		echo "\033[33mRelease Version\033[0m"
		flutter build aar --no-profile --no-debug
	elif [[ "$build_debug" == true ]]; then
		echo "\033[33mDebug Version\033[0m"
		flutter build aar --no-profile --no-release
	else
		echo "\033[33mRelease&Debug Version\033[0m"
		flutter build aar --no-profile
	fi
	mv ./build/host/outputs/repo/* ./SDK/Android_SDK
	rm -rf ./build
	echo "\033[33mAndroid SDK Done\033[0m"
}

while getopts ":iards" optname; do
	case "$optname" in
	"i")
		build_ios=true
		;;
	"a")
		build_android=true
		;;
	"r")
		build_release=true
		;;
	"d")
		build_debug=true
		;;
	"s")
		build_ios_simulator=true
		;;
	":")
		echo "\033[31mNo argument value for option $OPTARG\033[0m"
		;;
	"?")
		echo "\033[31mUnknown option $OPTARG\033[0m"
		;;
	*)
		echo "\033[31mUnknown error while processing options\033[0m"
		;;
	esac
	#echo "option index is $OPTIND"
done

all_platform=false

if [ "$build_ios" == false ] && [ "$build_android" == false ]; then
	all_platform=true
fi

echo """\033[36m
Build iOS / Android: ($build_ios / $build_android)
Build All Platform: $all_platform
Build Release: $build_release
Include iOS Simulator: $build_ios_simulator\033[0m
"""

checkSDKDir
if [[ "$build_ios" == true ]]; then
    updatePodfile
	buildIOSSDk
elif [[ "$build_android" == true ]]; then
	buildAndroidSDK
else
    updatePodfile
	buildIOSSDk
	buildAndroidSDK
fi