How to build Android and iOS App with commands in Flutter project.

Grassroot Engineer
3 min readJul 18, 2021

--

Command line

ปกติ เวลาเราจะ build Android จะไม่มีอะไรยุ่งยาก เมื่อเราอยู่ใน path เดียวกับ pubspec.yaml ให้รันคำสั่งต่อไปนี้

flutter clean
flutter pub cache repair
flutter pub get
flutter build apk --release

3 บรรทัดบน เมื่อต้องการ clean, clear cache และ get package ใหม่ก่อน

flutter build apk --flavor dev lib/main-dev.dart --release

ในกรณีที่เราต้องการ build ใน project ที่ได้แบ่ง env ไว้ด้วย flavor ซึ่งจะได้ output ดังภาพด้านล่างนี้ ในการ build ทั้ง apk หรือ appbundle

flutter build apk — flavor dev lib/main-dev.dart — release
flutter build appbundle— flavor dev lib/main-dev.dart — release

แต่สำหรับ iOS จะมีขั้นตอนเพิ่มเติมดังนี้คับ

  1. Install pods and build ios from Flutter
cd ios
pod install
cd ..
flutter build ios --release
cd ios

2. จากนั้นทำการ Archive
สามารถดูรายละเอียดคำสั่ง xcodebuild ได้ที่ man xcodebuild or xcodebuild -help

# Archive
echo "########## Archive ##########"
xcodebuild -workspace $WORKSPACE_PATH -scheme $SCHEME clean archive -configuration release -sdk iphoneos -archivePath $ARCHIVE_PATH

ซึ่งจากคำสั่งข้างบนจะมีตัวแปร 3 ตัวที่เราต้องกำหนดคือ

  • SCHEME =โดยปกติจะเป็น Runner
  • WORKSPACE_PATH = path ของ Runner.xcworkspace
  • ARCHIVE_PATH = path ที่เราระบุเองได้เลยว่าจะให้ archive file ไปเก็บไว้ที่ไหนและให้ชื่อว่าอะไร โดยผมกำหนดให้ไปเก็บไว้ที่ grassroot_scripted_build และใช้ชื่อว่า grassroot.xcarchive ในทุกๆครั้งที่ archive
SCHEME=RunnerWORKSPACE_PATH=/Users/atthana/Desktop/Private_Q/Flutter/nextflow_local_notification/ios/Runner.xcworkspaceARCHIVE_PATH=/Users/atthana/Library/Developer/Xcode/Archives/grassroot_scripted_build/grassroot.xcarchive

ในขั้นตอนการ Archive ด้วย command เราจะต้อง ensure ว่า configure ใน Xcode เราถูกต้องหมดแล้ว

  • Team
  • Bundle Identifier
  • Provision Profile
  • Signing Certificate
Ensure that all configurations in xcode are correct.

ในครั้งแรกของการ Archive ผ่าน command ถ้าติด error เราจะต้อง ensure โดยทำใน xcode ก่อนที่ Xcode > Product > Archive > เมื่อผ่านแล้วจะเจอ Archives dialog แบบนี้

When archive has succeeded.

เมื่อ Archive ด้วย command ผ่านจะได้ return ใน command ว่า
** ARCHIVE SUCCEEDED ** แบบนี้ครับ

ARCHIVE must be successed like this.

4. จากนั้นทำการ build ipa โดยในการ build ipa file จะเป็นการ build จาก archive file ที่เราได้สร้างไว้ก่อนหน้านี้ ฉะนั้นจะต้องระบุ variables เพิ่มเติม พร้อมกับระบุ path สำหรับ export ipa ออกไป และที่สำคัญคือ ต้องระบุ exportOptionPlist ด้วยครับ ผมสรุปแบบนี้นะ

echo "########## Export ipa ##########"xcodebuild -exportArchive -archivePath $ARCHIVE_PATH -exportOptionsPlist $PLIST_PATH -exportPath $IPA_EXPORT_PATH -allowProvisioningUpdates
  • ARCHIVE_PATH = path ก่อนหน้าที่ระบุไว้ว่าหลังจาก archive เสร็จแล้ว ให้ไปเก็บไว้ที่ไหน และชื่ออะไร
  • IPA_EXPORT_PATH = path ของ ipa file ที่จะให้ไปเก็บไว้
  • PLIST_PATH = เป็นไฟล์ที่เอาไว้ระบุ options ในการสร้าง ipa file ซึ่งปกติเราจะต้องเลือกจากหน้า UI ของ Xcode แต่เราสามารถกำหนดผ่าน ExportOptions.plist ได้เลย

ซึ่งผมจะกำหนดค่าในตัวแปรไว้แบบนี้คับ

# Create current datetime to be folder name.
now="$(date)"
now="$(date +'%Y%m%d-%H%M%S')"
ARCHIVE_PATH=/Users/atthana/Library/Developer/Xcode/Archives/grassroot_scripted_build/grassroot.xcarchiveIPA_EXPORT_PATH=/Users/atthana/Desktop/Private_Q/Flutter/nextflow_local_notification/exported_apps/ios-$nowPLIST_PATH=/Users/atthana/Desktop/Private_Q/Flutter/nextflow_local_notification/ExportOptions.plist

เรื่องของ ExportOptions.plist ต้องอธิบายเพิ่มแบบนี้คับ ไฟล์นี้เราสร้างเองได้เลย โดยใช้ format แบบนี้ (และแก้ไขค่า teamID ให้ตรง)

กรณี Automatic Signing
กรณีใช้ Manual Signing.

เมื่อ run command ผ่านจะได้ ipa file ใน specificed folder ที่เราได้ระบุไว้

ipa file after done export via command.

เมื่อนำทั้งหมดมาเขียนเป็น bash script ในเบื้องต้นเพื่อ build app ทั้ง 2 เวอร์ชั่นจึงประกอบด้วย

  1. publish.sh
  2. publish-android.sh
  3. publish-ios.sh

ซึ่งทั้งหมดจากวางไว้ใน level เดียวกับ pubspec.yaml เพื่อให้ง่ายในการเรียกใช้

publish.sh
For building Android.
For building iOS.
After finished building will get both of Apps (Android and iOS)

เรียบร้อยแล้ว ได้ App สำหรับมาทดสอบล่ะ

RECAP xcodebuild commands:

จริงๆแล้ว command xcodebuild มี option ต่างๆที่เรายังสามารถใช้งานได้ เช่น

1. Clean Project และ Workspace with scheme

xcodebuild clean -workspace $WORKSPACE_PATH -scheme $SCHEME
xcodebuild clean -project $PROJECT_PATH -scheme $SCHEME

2. Build Workspace with scheme

xcodebuild build -workspace $WORKSPACE_PATH -scheme $SCHEME

3. Archive Workshpace

xcodebuild archive -workspace $WORKSPACE_PATH -scheme $SCHEME -archivePath $ARCHIVE_PATH

4. Create an ipa

xcodebuild -exportArchive -archivePath $ARCHIVE_PATH -exportOptionsPlist $PLIST_PATH -exportPath $IPA_EXPORT_PATH -allowProvisioningUpdates

5. List Project or Workspace Targets.

xcodebuild -list

6. Read xcodebuild action lists.

man xcodebuild
xcodebuild -help

แล้วพบกันใหม่คับ

References:
https://medium.com/@marksiu/how-to-build-ios-project-with-command-82f20fda5ec5

--

--

Grassroot Engineer
Grassroot Engineer

Written by Grassroot Engineer

ATM engineer who is interested in CODING and believe in EFFORT. — https://grassrootengineer.com

No responses yet