Skip to Content
Kotlin Multiplatform📚 Học KMPCài đặt môi trường

Cài đặt môi trường KMP

Bài này hướng dẫn bạn cài đặt tất cả công cụ cần thiết để phát triển ứng dụng Kotlin Multiplatform.

Yêu cầu hệ thống

Phát triển cho Android + iOS (macOS)

  • macOS 12.0 trở lên (bắt buộc cho iOS)
  • RAM: 16GB+ (khuyến nghị)
  • Disk: 50GB+ trống

Phát triển chỉ cho Android (Windows/Linux/macOS)

  • Windows 10/11, Ubuntu 20.04+, hoặc macOS
  • RAM: 8GB+ (16GB khuyến nghị)
  • Disk: 30GB+ trống

Bước 1: Cài đặt JDK

KMP yêu cầu JDK 17 trở lên.

macOS

# Dùng Homebrew brew install openjdk@17 # Thêm vào PATH echo 'export PATH="/opt/homebrew/opt/openjdk@17/bin:$PATH"' >> ~/.zshrc source ~/.zshrc # Kiểm tra java -version

Windows

  1. Tải JDK từ Adoptium 
  2. Chạy installer và cài đặt
  3. Kiểm tra: java -version trong Command Prompt

Bước 2: Cài đặt Android Studio

Tải và cài đặt

  1. Tải Android Studio  phiên bản mới nhất
  2. Chạy installer
  3. Mở Android Studio và hoàn thành setup wizard

Cài đặt Kotlin Multiplatform Plugin

  1. Mở Android Studio > Settings (hoặc Preferences trên macOS)
  2. Vào Plugins > Marketplace
  3. Tìm “Kotlin Multiplatform”
  4. Click Install và restart Android Studio

Cấu hình Android SDK

Android Studio sẽ tự động cài đặt Android SDK. Kiểm tra:

  1. Vào Settings > Languages & Frameworks > Android SDK
  2. Đảm bảo có ít nhất Android 14 (API 34) hoặc mới hơn

Bước 3: Cài đặt Xcode (chỉ macOS)

Để build và chạy app iOS, bạn cần Xcode:

  1. Mở App Store trên Mac
  2. Tìm và cài đặt Xcode (phiên bản 15.0+)
  3. Mở Xcode lần đầu để hoàn thành cài đặt
  4. Chấp nhận license agreement

Cài Command Line Tools

xcode-select --install

Cài CocoaPods (cho iOS dependencies)

# Cài Ruby (nếu chưa có) brew install ruby # Cài CocoaPods sudo gem install cocoapods pod --version

Bước 4: Cài KDoctor - Kiểm tra môi trường

KDoctor là công cụ chính thức để kiểm tra môi trường KMP:

# Cài đặt KDoctor brew install kdoctor # Chạy kiểm tra kdoctor

Output mẫu khi mọi thứ OK:

Environment diagnose (to see all details, use -v option): [✓] Operating System [✓] Java [✓] Android Studio [✓] Xcode [✓] CocoaPods Conclusion: ✓ Your operation system is ready for Kotlin Multiplatform Mobile Development!

Xử lý lỗi thường gặp

LỗiGiải pháp
Java not foundCài JDK 17+ và thêm vào PATH
Android Studio not foundĐảm bảo đã cài và mở ít nhất 1 lần
Xcode not foundCài Xcode từ App Store
CocoaPods not foundChạy sudo gem install cocoapods
Xcode licenseMở Xcode và chấp nhận license

Bước 5: Tạo dự án thử nghiệm

Cách 1: Dùng Wizard online (Khuyến nghị)

  1. Truy cập Kotlin Multiplatform Wizard 
  2. Chọn platforms: Android, iOS
  3. Chọn options cần thiết
  4. Click Download và giải nén
  5. Mở project bằng Android Studio

Cách 2: Từ Android Studio

  1. Mở Android Studio
  2. File > New > Project
  3. Chọn Kotlin Multiplatform App
  4. Điền thông tin và Finish

Chạy thử

# Chạy trên Android Emulator ./gradlew :composeApp:installDebug # Chạy trên iOS Simulator (macOS only) ./gradlew :composeApp:iosSimulatorArm64Test

Cấu trúc thư mục sau cài đặt

~/ ├── Library/ │ └── Android/ │ └── sdk/ # Android SDK ├── Applications/ │ ├── Android Studio.app │ └── Xcode.app └── .gradle/ # Gradle cache

📝 Checklist cài đặt

  • JDK 17+ đã cài và có trong PATH
  • Android Studio đã cài với Kotlin Multiplatform plugin
  • Android SDK đã cài
  • Xcode đã cài (nếu cần iOS)
  • CocoaPods đã cài (nếu cần iOS)
  • KDoctor báo tất cả ✓

Tiếp theo

Sau khi cài đặt xong, hãy tiếp tục với Dự án KMP đầu tiên.

Last updated on