build_app.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/bash
  2. # Build Script for ZenTap
  3. # Builds APK and AAB files in debug and release modes
  4. set -e # Exit on any error
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. PURPLE='\033[0;35m'
  11. NC='\033[0m' # No Color
  12. # Build output directory
  13. BUILD_DIR="build/outputs"
  14. # App information
  15. APP_NAME="ZenTap"
  16. PACKAGE_NAME="hu.fsociety.zentap"
  17. echo -e "${BLUE}$APP_NAME Build Script${NC}"
  18. echo "======================="
  19. # Function to check if Flutter is installed
  20. check_flutter() {
  21. if ! command -v flutter &> /dev/null; then
  22. echo -e "${RED}Error: Flutter is not installed or not in PATH${NC}"
  23. echo "Please install Flutter: https://flutter.dev/docs/get-started/install"
  24. exit 1
  25. fi
  26. echo -e "${GREEN}✓ Flutter found: $(flutter --version --machine | grep -o '"flutterVersion":"[^"]*' | cut -d'"' -f4)${NC}"
  27. }
  28. # Function to check Flutter doctor
  29. check_flutter_doctor() {
  30. echo -e "${YELLOW}Running Flutter doctor...${NC}"
  31. flutter doctor --android-licenses > /dev/null 2>&1 || true
  32. # Check if Android toolchain is available
  33. if ! flutter doctor | grep -q "Android toolchain"; then
  34. echo -e "${RED}Warning: Android toolchain not properly configured${NC}"
  35. echo "Run 'flutter doctor' to see detailed issues"
  36. else
  37. echo -e "${GREEN}✓ Android toolchain available${NC}"
  38. fi
  39. }
  40. # Function to clean build
  41. clean_build() {
  42. echo -e "${YELLOW}Cleaning previous builds...${NC}"
  43. flutter clean
  44. flutter pub get
  45. echo -e "${GREEN}✓ Build cleaned${NC}"
  46. }
  47. # Function to build APK
  48. build_apk() {
  49. local mode=$1
  50. echo -e "${BLUE}Building $mode APK...${NC}"
  51. if [ "$mode" = "debug" ]; then
  52. flutter build apk --debug
  53. local apk_path="build/app/outputs/flutter-apk/app-debug.apk"
  54. local output_name="${APP_NAME}_debug_$(date +%Y%m%d_%H%M%S).apk"
  55. else
  56. flutter build apk --release
  57. local apk_path="build/app/outputs/flutter-apk/app-release.apk"
  58. local output_name="${APP_NAME}_release_$(date +%Y%m%d_%H%M%S).apk"
  59. fi
  60. if [ -f "$apk_path" ]; then
  61. # Create output directory if it doesn't exist
  62. mkdir -p "$BUILD_DIR/apk"
  63. # Copy APK to output directory with timestamp
  64. cp "$apk_path" "$BUILD_DIR/apk/$output_name"
  65. # Get file size
  66. local size=$(du -h "$apk_path" | cut -f1)
  67. echo -e "${GREEN}✓ $mode APK built successfully${NC}"
  68. echo -e "${PURPLE} File: $BUILD_DIR/apk/$output_name${NC}"
  69. echo -e "${PURPLE} Size: $size${NC}"
  70. return 0
  71. else
  72. echo -e "${RED}✗ Failed to build $mode APK${NC}"
  73. return 1
  74. fi
  75. }
  76. # Function to build AAB (Android App Bundle)
  77. build_aab() {
  78. local mode=$1
  79. echo -e "${BLUE}Building $mode AAB...${NC}"
  80. if [ "$mode" = "debug" ]; then
  81. flutter build appbundle --debug
  82. local aab_path="build/app/outputs/bundle/debug/app-debug.aab"
  83. local output_name="${APP_NAME}_debug_$(date +%Y%m%d_%H%M%S).aab"
  84. else
  85. flutter build appbundle --release
  86. local aab_path="build/app/outputs/bundle/release/app-release.aab"
  87. local output_name="${APP_NAME}_release_$(date +%Y%m%d_%H%M%S).aab"
  88. fi
  89. if [ -f "$aab_path" ]; then
  90. # Create output directory if it doesn't exist
  91. mkdir -p "$BUILD_DIR/bundle"
  92. # Copy AAB to output directory with timestamp
  93. cp "$aab_path" "$BUILD_DIR/bundle/$output_name"
  94. # Get file size
  95. local size=$(du -h "$aab_path" | cut -f1)
  96. echo -e "${GREEN}✓ $mode AAB built successfully${NC}"
  97. echo -e "${PURPLE} File: $BUILD_DIR/bundle/$output_name${NC}"
  98. echo -e "${PURPLE} Size: $size${NC}"
  99. return 0
  100. else
  101. echo -e "${RED}✗ Failed to build $mode AAB${NC}"
  102. return 1
  103. fi
  104. }
  105. # Function to show usage
  106. show_usage() {
  107. echo "Usage: $0 [OPTIONS]"
  108. echo ""
  109. echo "Options:"
  110. echo " -h, --help Show this help message"
  111. echo " -c, --clean Clean build before building"
  112. echo " -d, --debug Build debug versions only"
  113. echo " -r, --release Build release versions only"
  114. echo " --apk-only Build APK files only"
  115. echo " --aab-only Build AAB files only"
  116. echo " --all Build all variants (default)"
  117. echo ""
  118. echo "Examples:"
  119. echo " $0 # Build all variants"
  120. echo " $0 --debug # Build debug APK and AAB"
  121. echo " $0 --release --apk-only # Build release APK only"
  122. echo " $0 --clean --all # Clean and build all variants"
  123. }
  124. # Parse command line arguments
  125. BUILD_DEBUG=true
  126. BUILD_RELEASE=true
  127. BUILD_APK=true
  128. BUILD_AAB=true
  129. CLEAN_BUILD=false
  130. while [[ $# -gt 0 ]]; do
  131. case $1 in
  132. -h|--help)
  133. show_usage
  134. exit 0
  135. ;;
  136. -c|--clean)
  137. CLEAN_BUILD=true
  138. shift
  139. ;;
  140. -d|--debug)
  141. BUILD_DEBUG=true
  142. BUILD_RELEASE=false
  143. shift
  144. ;;
  145. -r|--release)
  146. BUILD_DEBUG=false
  147. BUILD_RELEASE=true
  148. shift
  149. ;;
  150. --apk-only)
  151. BUILD_APK=true
  152. BUILD_AAB=false
  153. shift
  154. ;;
  155. --aab-only)
  156. BUILD_APK=false
  157. BUILD_AAB=true
  158. shift
  159. ;;
  160. --all)
  161. BUILD_DEBUG=true
  162. BUILD_RELEASE=true
  163. BUILD_APK=true
  164. BUILD_AAB=true
  165. shift
  166. ;;
  167. *)
  168. echo -e "${RED}Unknown option: $1${NC}"
  169. show_usage
  170. exit 1
  171. ;;
  172. esac
  173. done
  174. # Main execution
  175. echo -e "${YELLOW}Build Configuration:${NC}"
  176. echo " Debug: $BUILD_DEBUG"
  177. echo " Release: $BUILD_RELEASE"
  178. echo " APK: $BUILD_APK"
  179. echo " AAB: $BUILD_AAB"
  180. echo " Clean: $CLEAN_BUILD"
  181. echo ""
  182. # Check prerequisites
  183. check_flutter
  184. check_flutter_doctor
  185. # Clean if requested
  186. if [ "$CLEAN_BUILD" = true ]; then
  187. clean_build
  188. echo ""
  189. fi
  190. # Build variants
  191. BUILD_SUCCESS=true
  192. if [ "$BUILD_DEBUG" = true ]; then
  193. if [ "$BUILD_APK" = true ]; then
  194. build_apk "debug" || BUILD_SUCCESS=false
  195. echo ""
  196. fi
  197. if [ "$BUILD_AAB" = true ]; then
  198. build_aab "debug" || BUILD_SUCCESS=false
  199. echo ""
  200. fi
  201. fi
  202. if [ "$BUILD_RELEASE" = true ]; then
  203. if [ "$BUILD_APK" = true ]; then
  204. build_apk "release" || BUILD_SUCCESS=false
  205. echo ""
  206. fi
  207. if [ "$BUILD_AAB" = true ]; then
  208. build_aab "release" || BUILD_SUCCESS=false
  209. echo ""
  210. fi
  211. fi
  212. # Summary
  213. echo -e "${BLUE}Build Summary${NC}"
  214. echo "============="
  215. if [ "$BUILD_SUCCESS" = true ]; then
  216. echo -e "${GREEN}✓ All builds completed successfully!${NC}"
  217. echo -e "${YELLOW}Output files location:${NC}"
  218. if [ -d "$BUILD_DIR" ]; then
  219. find "$BUILD_DIR" -name "*.apk" -o -name "*.aab" | while read -r file; do
  220. size=$(du -h "$file" | cut -f1)
  221. echo -e "${PURPLE} $file ($size)${NC}"
  222. done
  223. fi
  224. echo ""
  225. echo -e "${BLUE}Installation Instructions:${NC}"
  226. echo " APK: adb install <apk_file>"
  227. echo " AAB: Upload to Google Play Console"
  228. else
  229. echo -e "${RED}✗ Some builds failed. Check the output above for details.${NC}"
  230. exit 1
  231. fi