Bitcode คือ Intermediate Representation (IR) ของโค้ด ที่ไม่ใช่ Binary สุดท้าย ไม่สามารถรันบนอุปกรณ์ได้โดยตรง
กระบวนการทำงาน
- Source Code → Compiler (Clang) → Bitcode (LLVM IR)
- Upload Bitcode ไป App Store Connect
- Apple ใช้ LLVM Compile Bitcode → Binary สำหรับแต่ละ Architecture (arm64, armv7, x86_64 สำหรับ Simulator)
- App Thinning: ผู้ใช้ดาวน์โหลดเฉพาะ Binary ที่ตรงกับอุปกรณ์ของตน
ENABLE_BITCODE ใน Build Settings
| Setting | ค่า | ผลลัพธ์ |
|---|---|---|
| Yes | Bitcode Enabled | Compile → Bitcode → Upload App Store → Apple Compile → Binary |
| No | Bitcode Disabled | Compile → Binary ตรงๆ → Upload Binary ตรงๆ |
ข้อดีของ Bitcode
- App Thinning: ขนาด App ลดลง ผู้ใช้โหลดเฉพาะ Architecture ของตัวเอง
- Future-proof: Apple สามารถ Re-compile ให้ Architecture ใหม่ (เช่น arm64e) โดยไม่ต้อง Submit ใหม่
- Optimization: Apple ปรับ Optimization ให้เหมาะสมกับ Hardware แต่ละรุ่น
ข้อจำกัด/ข้อควรระวัง
- Build Time เพิ่มขึ้น (Compile 2 รอบ: Local + Apple)
- Third-party Libraries ทุกตัวต้อง Enable Bitcode ด้วย (หรือจะ Build Fail)
- Debug Symbols (dSYM) ต้องเก็บไว้เองสำหรับ Crash Reporting
- Xcode 14+ / iOS 16+: Bitcode Deprecated และจะถูกนำออกในอนาคต
สถานะปัจจุบัน (2024+)
Apple ประกาศ Deprecate Bitcode ตั้งแต่ Xcode 14 ควรตั้ง ENABLE_BITCODE = NO สำหรับโปรเจกต์ใหม่
การตรวจสอบ
# Terminal - เช็คว่า Binary มี Bitcode หรือไม่
otool -l YourApp.app/YourApp | grep -A 3 __LLVM
# ถ้ามี __LLVM segment = มี Bitcode