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 -versionWindows
- Tải JDK từ Adoptium
- Chạy installer và cài đặt
- Kiểm tra:
java -versiontrong Command Prompt
Bước 2: Cài đặt Android Studio
Tải và cài đặt
- Tải Android Studio phiên bản mới nhất
- Chạy installer
- Mở Android Studio và hoàn thành setup wizard
Cài đặt Kotlin Multiplatform Plugin
- Mở Android Studio > Settings (hoặc Preferences trên macOS)
- Vào Plugins > Marketplace
- Tìm “Kotlin Multiplatform”
- 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:
- Vào Settings > Languages & Frameworks > Android SDK
- Đả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:
- Mở App Store trên Mac
- Tìm và cài đặt Xcode (phiên bản 15.0+)
- Mở Xcode lần đầu để hoàn thành cài đặt
- Chấp nhận license agreement
Cài Command Line Tools
xcode-select --installCài CocoaPods (cho iOS dependencies)
# Cài Ruby (nếu chưa có)
brew install ruby
# Cài CocoaPods
sudo gem install cocoapods
pod --versionBướ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
kdoctorOutput 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ỗi | Giải pháp |
|---|---|
| Java not found | Cà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 found | Cài Xcode từ App Store |
| CocoaPods not found | Chạy sudo gem install cocoapods |
| Xcode license | Mở 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ị)
- Truy cập Kotlin Multiplatform Wizard
- Chọn platforms: Android, iOS
- Chọn options cần thiết
- Click Download và giải nén
- Mở project bằng Android Studio
Cách 2: Từ Android Studio
- Mở Android Studio
- File > New > Project
- Chọn Kotlin Multiplatform App
- Đ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:iosSimulatorArm64TestCấ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