Hướng dẫn cài đặt Android NDK theo hệ điều hành
Ngày 28/10/2019 - Hành trình trở thành Lập trình viên SDK Mobile tại VCCorp
Khi bắt đầu công việc tại VCCorp với vai trò Lập trình viên SDK Mobile, một trong những công cụ quan trọng mà tôi phải làm quen là Android NDK (Native Development Kit). NDK cho phép phát triển các phần mềm Android sử dụng ngôn ngữ C/C++, giúp tối ưu hiệu năng hoặc tích hợp các thư viện native.
1. Tải xuống và cài đặt Android NDK
Cách 1: Cài đặt qua Android Studio (Khuyến nghị)
Trên Windows
- Mở Android Studio → File → Settings (hoặc
Ctrl + Alt + S
) - Chọn Appearance & Behavior → System Settings → Android SDK
- Chuyển sang tab SDK Tools, tích chọn NDK (Side by side) và CMake
- Nhấn Apply để cài đặt
Trên macOS
- Mở Android Studio → Android Studio → Preferences (hoặc
Cmd + ,
) - Chọn Appearance & Behavior → System Settings → Android SDK
- Chuyển sang tab SDK Tools, tích chọn NDK (Side by side) và CMake
- Nhấn Apply để cài đặt
Trên Linux (Ubuntu/Debian)
- Mở Android Studio → File → Settings (hoặc
Ctrl + Alt + S
) - Chọn Appearance & Behavior → System Settings → Android SDK
- Chuyển sang tab SDK Tools, tích chọn NDK (Side by side) và CMake
- Nhấn Apply để cài đặt
Cách 2: Tải thủ công từ trang chủ
Trên Windows
- Truy cập Android NDK Downloads
- Tải file
android-ndk-r{version}-windows-x86_64.zip
- Giải nén vào thư mục
C:\Android\ndk\
hoặcC:\Users\{username}\AppData\Local\Android\Sdk\ndk\
- Thêm đường dẫn vào biến môi trường PATH:
- Mở System Properties → Environment Variables
- Thêm
C:\Android\ndk\{version}\
vào PATH
Trên macOS
- Truy cập Android NDK Downloads
- Tải file
android-ndk-r{version}-darwin-x86_64.zip
- Giải nén vào thư mục
/Users/{username}/Library/Android/sdk/ndk/
- Thêm vào
.bash_profile
hoặc.zshrc
:export ANDROID_NDK_HOME=/Users/{username}/Library/Android/sdk/ndk/{version} export PATH=$PATH:$ANDROID_NDK_HOME
- Chạy
source ~/.bash_profile
hoặcsource ~/.zshrc
Trên Linux
- Truy cập Android NDK Downloads
- Tải file
android-ndk-r{version}-linux-x86_64.zip
- Giải nén vào thư mục
/home/{username}/Android/Sdk/ndk/
cd /home/{username}/Android/Sdk/ndk/ unzip android-ndk-r{version}-linux-x86_64.zip
- Thêm vào
.bashrc
hoặc.profile
:export ANDROID_NDK_HOME=/home/{username}/Android/Sdk/ndk/{version} export PATH=$PATH:$ANDROID_NDK_HOME
- Chạy
source ~/.bashrc
2. Cấu hình NDK trong Android Studio
Kiểm tra cài đặt NDK
Trên tất cả các hệ điều hành
- Mở Android Studio → File → Project Structure (Windows/Linux) hoặc Android Studio → Project Structure (macOS)
- Chọn SDK Location
- Kiểm tra đường dẫn Android NDK location
- Nếu chưa có, nhấn Browse và chọn thư mục NDK đã cài đặt
- Nhấn Apply → OK
Kiểm tra từ Terminal/Command Prompt
Windows (Command Prompt)
ndk-build --version
macOS/Linux (Terminal)
ndk-build --version
3. Cấu hình NDK trong dự án Android
Cấu hình local.properties
# Windows
ndk.dir=C\:\\Android\\ndk\\{version}
# macOS
ndk.dir=/Users/{username}/Library/Android/sdk/ndk/{version}
# Linux
ndk.dir=/home/{username}/Android/Sdk/ndk/{version}
Cấu hình build.gradle (Module: app)
android {
compileSdk 34
defaultConfig {
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
4. Tạo file CMakeLists.txt
Tạo file CMakeLists.txt
trong thư mục gốc của module app/
:
cmake_minimum_required(VERSION 3.4.1)
# Tạo thư viện native
add_library(
native-lib
SHARED
src/main/cpp/native-lib.cpp
)
# Tìm thư viện log của Android
find_library(
log-lib
log
)
# Liên kết thư viện
target_link_libraries(
native-lib
${log-lib}
)
5. Tạo code native và kiểm tra hoạt động
Tạo file C++ (src/main/cpp/native-lib.cpp)
#include <jni.h>
#include <string>
#include <android/log.h>
#define LOG_TAG "NativeLib"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapp_MainActivity_stringFromJNI(JNIEnv* env, jobject) {
std::string hello = "Hello from NDK!";
LOGI("NDK function called successfully");
return env->NewStringUTF(hello.c_str());
}
Gọi từ MainActivity (Kotlin)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.sample_text)
textView.text = stringFromJNI()
}
/**
* Native method được implement trong C++
*/
external fun stringFromJNI(): String
companion object {
// Load thư viện native khi class được tải
init {
System.loadLibrary("native-lib")
}
}
}
6. Troubleshooting theo hệ điều hành
Windows
- Lỗi thiếu Visual Studio: Cài đặt Visual Studio Build Tools hoặc Visual Studio Community
- Lỗi PATH: Kiểm tra biến môi trường PATH có chứa đường dẫn NDK
- Lỗi permission: Chạy Android Studio với quyền Administrator
macOS
- Lỗi Xcode Command Line Tools: Chạy
xcode-select --install
- Lỗi permission: Sử dụng
sudo
khi cần thiết hoặc thay đổi ownership - Lỗi M1/M2 chip: Đảm bảo sử dụng version NDK hỗ trợ Apple Silicon
Linux
- Lỗi thiếu dependencies:
sudo apt-get install build-essential sudo apt-get install libc6-dev-i386 # For 32-bit support
- Lỗi permission: Thay đổi quyền thư mục:
chmod -R 755 /path/to/ndk
Kết luận
Việc cài đặt và sử dụng Android NDK giúp mở rộng khả năng phát triển ứng dụng, đặc biệt khi làm việc với các thư viện native hoặc xử lý hiệu năng cao. Mỗi hệ điều hành có những đặc thù riêng trong quá trình cài đặt, nhưng các bước cơ bản đều tương tự nhau.
Lưu ý: Luôn sử dụng version NDK tương thích với Android SDK và Gradle version của dự án để tránh các lỗi không mong muốn.
#ndk #android #android-ndk #windows #macos #linux