เริ่มต้นใช้งาน NDK
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Native Development Kit (NDK) คือชุดเครื่องมือที่ช่วยให้คุณใช้ C
และโค้ด C++ กับ Android และมีไลบรารีแพลตฟอร์มที่คุณนำไปใช้จัดการได้
กิจกรรมที่มีอยู่แต่เดิม และเข้าถึงส่วนประกอบของอุปกรณ์ทางกายภาพ เช่น เซ็นเซอร์และ
การป้อนข้อมูลด้วยการสัมผัส NDK อาจไม่เหมาะสมสำหรับ Android มือใหม่ส่วนใหญ่
โปรแกรมเมอร์ที่จำเป็นต้องใช้โค้ด Java และ API เฟรมเวิร์กเท่านั้นในการพัฒนา
แอปของตนได้ อย่างไรก็ตาม NDK อาจมีประโยชน์ในกรณีที่คุณต้องการ
ต้องเป็นค่าใดค่าหนึ่งต่อไปนี้
- บีบประสิทธิภาพเพิ่มเติมออกจากอุปกรณ์เพื่อให้เวลาในการตอบสนองต่ำหรือทำงาน
แอปพลิเคชันที่เน้นการคำนวณมาก เช่น เกมหรือการจำลองฟิสิกส์
- ใช้ของคุณเองหรือของนักพัฒนาซอฟต์แวร์คนอื่นๆ ซ้ำ C หรือ C++
เมื่อใช้ Android Studio 2.2 ขึ้นไป คุณจะทำสิ่งต่อไปนี้ได้
ใช้ NDK เพื่อรวบรวมโค้ด C และ C++ ลงในไลบรารีเนทีฟและจัดแพ็กเกจโค้ดนั้น
ลงใน APK โดยใช้ Gradle ซึ่งเป็นระบบบิลด์ที่ผสานรวมของ IDE รหัส Java ของคุณ
จะสามารถเรียกใช้ฟังก์ชันในคลังแบบเนทีฟผ่านเฟรมเวิร์ก Java Native Interface (JNI) เพื่อเรียนรู้
ข้อมูลเพิ่มเติมเกี่ยวกับ Gradle และระบบบิลด์ของ Android ที่หัวข้อกำหนดค่าบิลด์ของคุณ
เครื่องมือสร้างเริ่มต้นของ Android Studio สำหรับคอมไพล์ไลบรารีเนทีฟคือ CMake นอกจากนี้ Android Studio ยัง
สนับสนุน ndk-build เนื่องจาก
จำนวนโปรเจ็กต์ที่มีอยู่ซึ่งใช้ชุดเครื่องมือบิลด์ แต่ถ้าคุณ
การสร้างไลบรารีเนทีฟใหม่ คุณควรใช้ CMake
คู่มือนี้จะให้ข้อมูลที่คุณจําเป็นในการเริ่มต้นใช้งาน
NDK ใน Android Studio หากยังไม่มี Android เวอร์ชันล่าสุด
Studio ดาวน์โหลดและติดตั้งเลย
ดึงดูดความสนใจของผู้ใช้ Gradle เวอร์ชันทดลอง: โปรดพิจารณา
การเปลี่ยนไปใช้ปลั๊กอินเวอร์ชัน 2.2.0 ขึ้นไปและใช้ CMake หรือ ndk-build
เพื่อสร้างไลบรารีเนทีฟหากเป็นไปตามเงื่อนไขใดๆ ต่อไปนี้:
โปรเจ็กต์แบบเนทีฟใช้ CMake หรือ ndk-build อยู่แล้ว ควรใช้เวอร์ชันเสถียร
เวอร์ชันของระบบบิลด์ Gradle หรือต้องการการรองรับเครื่องมือเสริม
เช่น CCache
มิฉะนั้น คุณสามารถ ใช้
เวอร์ชันทดลองของ Gradle และปลั๊กอิน Android
ดาวน์โหลด NDK และเครื่องมือ
หากต้องการคอมไพล์และแก้ไขข้อบกพร่องของโค้ดเนทีฟสำหรับแอป คุณต้องมีสิ่งต่อไปนี้
คอมโพเนนต์
- Android Native Development Kit (NDK): ชุดเครื่องมือที่ช่วยให้คุณสามารถ
เพื่อใช้โค้ด C และ C++ กับ Android
- CMake: เครื่องมือสร้างภายนอกที่ทำงานร่วมกับ Gradle เพื่อสร้าง
ไลบรารีแบบเนทีฟ คุณไม่จำเป็นต้องใช้คอมโพเนนต์นี้หากวางแผนที่จะใช้เท่านั้น
ndk-build
-
LLDB: โปรแกรมแก้ไขข้อบกพร่องที่ Android Studio ใช้เพื่อแก้ไขข้อบกพร่องของโค้ดแบบเนทีฟ
โปรดดูข้อมูลเกี่ยวกับการติดตั้งคอมโพเนนต์เหล่านี้ที่หัวข้อติดตั้งและกำหนดค่า NDK และ CMake
สร้างหรือนำเข้าโปรเจ็กต์เนทีฟ
เมื่อตั้งค่า Android Studio แล้ว เพียงสร้างโปรเจ็กต์ใหม่ด้วย
การรองรับ C/C++ อย่างไรก็ตามหากคุณต้องการเพิ่มหรือนำเข้าโค้ดเนทีฟไปยังไฟล์
โปรเจ็กต์ Android Studio ที่มีอยู่ คุณต้องทำตามขั้นตอนพื้นฐานต่อไปนี้
-
สร้างใหม่
ไฟล์ต้นฉบับดั้งเดิม แล้วเพิ่มลงในโปรเจ็กต์ Android Studio ของคุณ
- คุณสามารถข้ามขั้นตอนนี้ได้หากมีโค้ดแบบเนทีฟอยู่แล้วหรือต้องการที่จะ
นำเข้าไลบรารีเนทีฟที่สร้างไว้ล่วงหน้า
-
สร้าง
สคริปต์บิลด์ CMake เพื่อบอก CMake ถึงวิธีสร้างแหล่งที่มาเนทีฟ
ลงในไลบรารี คุณยังต้องใช้สคริปต์บิลด์นี้หากกำลังนำเข้าและ
การลิงก์กับไลบรารีแพลตฟอร์มที่สร้างไว้ล่วงหน้า
- คุณสามารถข้ามขั้นตอนนี้ได้หากไลบรารีเนทีฟที่มีอยู่
CMakeLists.txt
สคริปต์บิลด์ หรือใช้ ndk-build และรวม
Android.mk
สคริปต์บิลด์
-
ลิงก์ Gradle กับ
ไลบรารีเนทีฟ โดยการระบุเส้นทางไปยัง CMake หรือ ndk-build ของคุณ
ไฟล์สคริปต์ Gradle ใช้สคริปต์บิลด์เพื่อนำเข้าซอร์สโค้ดไปยัง
โปรเจ็กต์ Android Studio และจัดแพ็กเกจไลบรารีเนทีฟ (ไฟล์ SO) ลงในไฟล์
APK
หมายเหตุ: หากโปรเจ็กต์ที่มีอยู่ของคุณใช้เวอร์ชัน
ndkCompile
คุณควรเปิด
build.properties
ไฟล์และนำบรรทัดโค้ดต่อไปนี้ออก
ก่อนที่จะกำหนดค่า Gradle ให้ใช้ CMake หรือ ndk-build:
// Remove this line
android.useDeprecatedNdk = true
-
สร้างและเรียกใช้แอปโดยคลิก
เรียกใช้
เพิ่ม Gradle
กระบวนการ CMake หรือ ndk-build ของคุณเป็นทรัพยากร Dependency ในการคอมไพล์ สร้าง และ
สร้างแพ็กเกจไลบรารีเนทีฟด้วย APK
เมื่อแอปของคุณกำลังทำงานบนอุปกรณ์จริงหรือโปรแกรมจำลอง คุณสามารถใช้
Android Studio เพื่อแก้ไขข้อบกพร่องของแอป
หรือหากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับ NDK และคอมโพเนนต์ต่างๆ ให้อ่านหน้า แนวคิด
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[],[],null,["# Get started with the NDK\n\nThe Native Development Kit (NDK) is a set of tools that allows you to use C\nand C++ code with Android, and provides [platform libraries](/ndk/guides/stable_apis) you can use to manage\nnative activities and access physical device components, such as sensors and\ntouch input. The NDK may not be appropriate for most novice Android\nprogrammers who need to use only Java code and framework APIs to develop\ntheir apps. However, the NDK can be useful for cases in which you need to do\none or more of the following:\n\n- Squeeze extra performance out of a device to achieve low latency or run computationally intensive applications, such as games or physics simulations.\n- Reuse your own or other developers' C or C++ libraries.\n\n\nUsing [Android Studio 2.2 and higher](/studio), you can\nuse the NDK to compile C and C++ code into a native library and package it\ninto your APK using Gradle, the IDE's integrated build system. Your Java code\ncan then call functions in your native library through the [Java Native Interface (JNI)](http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html) framework. To learn\nmore about Gradle and the Android build system, read [Configure Your Build](/studio/build).\n\n\nAndroid Studio's default build tool to compile native libraries is [CMake](https://cmake.org/). Android Studio also\nsupports [ndk-build](/ndk/guides/ndk-build) due to the large\nnumber of existing projects that use the build toolkit. However, if you are\ncreating a new native library, you should use CMake.\n\n\nThis guide gives you the information you need to get up and running with the\nNDK on Android Studio. If you don't have the latest version of Android\nStudio, [download and install it now](/studio).\n\n\n**Attention experimental Gradle users:** Consider [migrating to plugin version 2.2.0 or higher](http://tools.android.com/tech-docs/new-build-system/gradle-experimental/migrate-to-stable), and using CMake or ndk-build\nto build your native libraries if any of the following apply to you: Your\nnative project already uses CMake or ndk-build; you would rather use a stable\nversion of the Gradle build system; or you want support for add-on tools,\nsuch as [CCache](https://ccache.samba.org/).\nOtherwise, you can continue to [use\nthe experimental version of Gradle and the Android plugin](http://tools.android.com/tech-docs/new-build-system/gradle-experimental).\n\nDownload the NDK and tools\n--------------------------\n\n\nTo compile and debug native code for your app, you need the following\ncomponents:\n\n- The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android.\n- CMake: an external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build.\n- *LLDB*: the debugger Android Studio uses to debug native code.\n\n\nFor information on installing these components, see [Install and configure the NDK and CMake](/studio/projects/install-ndk).\n\nCreate or import a native project\n---------------------------------\n\n\nOnce you set up Android Studio, you can simply [Create a New Project with\nC/C++ Support](/studio/projects/add-native-code#new-project). However, if you want to add or import native code to an\nexisting Android Studio project, you need to follow this basic process:\n\n1. [Create new\n native source files](/studio/projects/add-native-code#create-sources) and add them to your Android Studio project.\n - You can skip this step if you already have native code or want to import a prebuilt native library.\n2. [Create\n a CMake build script](/studio/projects/configure-cmake#create_script) to tell CMake how to build your native sources into a library. You also require this build script if you are importing and linking against prebuilt or platform libraries.\n - You can skip this step if your existing native library already has a `CMakeLists.txt` build script, or uses ndk-build and includes an [`Android.mk`](/ndk/guides/android_mk) build script.\n3. [Link Gradle to\n your native library](/studio/projects/gradle-external-native-builds) by providing a path to your CMake or ndk-build script file. Gradle uses the build script to import source code into your Android Studio project and package your native library (the SO file) into the APK.\n\n\n **Note:** If your existing project uses the deprecated\n `ndkCompile` tool, you should open your\n `build.properties` file and remove the following line of code\n before configuring Gradle to use CMake or ndk-build: \n\n ```\n // Remove this line\n android.useDeprecatedNdk = true\n ```\n4. [Build and run your app](/studio/run) by clicking **Run** . Gradle adds your CMake or ndk-build process as a dependency to compile, build, and package your native library with your APK.\n\n\nOnce your app is running on a physical device or the emulator, you can use\nAndroid Studio to [Debug your app](/studio/debug).\nOtherwise, to learn more about the NDK and its components, read the [Concepts](/ndk/guides/concepts) page."]]