Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> iOS

Embedded Bitcode คืออะไร และ ENABLE_BITCODE ใน Xcode ทำหน้าที่อะไร

Bitcode คือ Intermediate Representation (IR) ของโค้ด ที่ไม่ใช่ Binary สุดท้าย ไม่สามารถรันบนอุปกรณ์ได้โดยตรง

กระบวนการทำงาน

  1. Source Code → Compiler (Clang) → Bitcode (LLVM IR)
  2. Upload Bitcode ไป App Store Connect
  3. Apple ใช้ LLVM Compile Bitcode → Binary สำหรับแต่ละ Architecture (arm64, armv7, x86_64 สำหรับ Simulator)
  4. App Thinning: ผู้ใช้ดาวน์โหลดเฉพาะ Binary ที่ตรงกับอุปกรณ์ของตน

ENABLE_BITCODE ใน Build Settings

Settingค่าผลลัพธ์
YesBitcode EnabledCompile → Bitcode → Upload App Store → Apple Compile → Binary
NoBitcode DisabledCompile → 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