بدء استخدام NDK
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
حزمة تطوير البرامج (NDK) هي مجموعة من الأدوات التي تتيح لك استخدام لغة البرمجة C
ورمز C++ مع Android، كما توفر مكتبات الأنظمة الأساسية التي يمكنك استخدامها لإدارة
الأنشطة الأصلية والوصول إلى مكونات الأجهزة المادية، مثل أجهزة الاستشعار
الإدخال باللمس. قد لا يكون NDK مناسبًا لمعظم أجهزة Android المبتدئين.
يحتاج المبرمجون الذين يحتاجون إلى استخدام رمز Java وواجهات برمجة التطبيقات لإطار العمل فقط إلى تطوير
تطبيقاتهم. ومع ذلك، يمكن أن يكون NDK مفيدًا في الحالات التي يتعين عليك فيها
واحد أو أكثر مما يلي:
- اضغط على مستوى الأداء الإضافي خارج الجهاز لتحقيق وقت استجابة سريع أو للتشغيل.
التطبيقات المكثفة من الناحية الحاسوبية، مثل الألعاب أو محاكاة الفيزياء.
- أعد استخدام بياناتك أو تطبيقات المطوِّرين الآخرين مكتبات C أو C++.
باستخدام الإصدار 2.2 من "استوديو Android" والإصدارات الأحدث، يمكنك:
استخدام NDK لتجميع التعليمة البرمجية C وC++ في مكتبة أصلية وتجميعها
إلى ملف APK باستخدام Gradle، نظام التصميم المدمج في بيئة التطوير المتكاملة (IDE). رمز Java
يمكنك بعد ذلك استدعاء الدوال في مكتبتك الأصلية من خلال إطار عمل واجهة Java الأصلية (JNI). للتعلّم
لمعرفة مزيد من المعلومات حول Gradle ونظام تصميم Android، يُرجى الاطّلاع على مقالة Configure Your Build (ضبط التصميم الخاص بك).
إنّ أداة التصميم التلقائية في "استوديو Android" لتجميع المكتبات الأصلية هي CMake. و"استوديو Android" أيضًا
تدعم ndk-build نظرًا
عدد المشروعات الحالية التي تستخدم مجموعة أدوات الإنشاء. ومع ذلك، إذا كنت
إنشاء مكتبة أصلية جديدة، يجب عليك استخدام CMake.
يمنحك هذا الدليل المعلومات التي تحتاجها لبدء استخدام
NDK على "استوديو Android". في حال عدم تثبيت أحدث إصدار من Android
استوديو، يمكنك تنزيله وتثبيته الآن.
الانتباه إلى مستخدمي Gradle التجريبيين: يمكنك
الانتقال إلى الإصدار 2.2.0 أو إصدار أحدث من المكوّن الإضافي واستخدام CMake أو ndk-build
لإنشاء مكتبات أصلية إذا كان أي مما يلي ينطبق عليك:
استخدام CMake أو ndk-build بالفعل في المشروع الأصلي فأنت تفضل استخدام دالة ثابتة
من نظام إنشاء Gradle؛ أو تريد دعمًا للأدوات الإضافية،
مثل Ccache.
بخلاف ذلك، يمكنك مواصلة استخدام
الإصدار التجريبي من أداة Gradle ومكوّن Android الإضافي.
تنزيل NDK والأدوات
لتجميع الرمز الأصلي لتطبيقك وتصحيح الأخطاء فيه، تحتاج إلى ما يلي:
المكونات:
- مجموعة أدوات تطوير البرامج (NDK) الأصلية لنظام التشغيل Android: مجموعة من الأدوات التي تتيح لك
لاستخدام رموز C وC++ مع Android.
- CMake: أداة تصميم خارجية تعمل جنبًا إلى جنب مع Gradle لإنشاء
مكتبة أصلية. ولست بحاجة إلى هذا المكون إذا كنت تخطط لاستخدام هذا المكون فقط.
ndk-build.
-
LLDB: برنامج تصحيح الأخطاء الذي يستخدمه "استوديو Android" لتصحيح أخطاء الرمز البرمجي الأصلي.
لمزيد من المعلومات عن تثبيت هذه المكوّنات، يُرجى الاطّلاع على تثبيت وتكوين NDK وCMake.
إنشاء مشروع مدمج مع المحتوى أو استيراده
بعد إعداد "استوديو Android"، يمكنك ببساطة إنشاء مشروع جديد باستخدام
دعم C/C++ مع ذلك، إذا كنت تريد إضافة رمز أصلي أو استيراده إلى
مشروع Android Studio الحالي، عليك اتباع العملية الأساسية التالية:
-
إنشاء تصنيف جديد
ملفات المصدر الأصلية وإضافتها إلى مشروعك على "استوديو Android".
- ويمكنك تخطي هذه الخطوة إذا كان لديك رمز أصلي أو تريد
استيراد مكتبة مدمجة مسبقًا.
-
إنشاء
إنشاء نص برمجي لـ CMakesلإخبار CMake بكيفية إنشاء المصادر الأصلية.
في مكتبة. تحتاج أيضًا إلى نص الإصدار البرمجي هذا إذا كنت تقوم باستيراد ملف
الربط بمكتبات النظام الأساسي أو تم إنشاؤها مسبقًا.
- يمكنك تخطّي هذه الخطوة إذا كانت مكتبتك الحالية المدمجة مع المحتوى
يعتمد
CMakeLists.txt
على إنشاء نص برمجي، أو استخدام ndk-build وتضمينه
جهاز Android.mk
النص البرمجي للإصدار.
-
ربط Gradle بـ
مكتبتك الأصلية من خلال توفير مسار إلى CMake أو ndk-build
ملف البرنامج النصي. يستخدم Gradle النص البرمجي للإنشاء لاستيراد رمز المصدر إلى
مشروع استوديو Android وحزم المكتبة الأصلية (ملف SO) في
APK.
ملاحظة: إذا كان مشروعك الحالي يستخدم واجهة برمجة التطبيقات متوقّفة نهائيًا
ndkCompile
، يجب فتح
build.properties
وإزالة سطر الرمز التالي
قبل تهيئة Gradle لاستخدام CMake أو ndk-build:
// Remove this line
android.useDeprecatedNdk = true
-
يمكنك إنشاء تطبيقك وتشغيله بالنقر على
تشغيل
عمليات إضافة Gradle
عملية CMake أو ndk-build كتبعية لتجميع البيانات
إضافة مكتبة التطبيقات المدمجة مع المحتوى إلى حزمة APK.
بعد تشغيل تطبيقك على جهاز فعلي أو المحاكي، يمكنك استخدام
"استوديو Android" لتصحيح أخطاء تطبيقك
أما إذا كنت تريد الاطّلاع على مزيد من المعلومات عن NDK ومكوناتها، فاقرأ صفحة المفاهيم.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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."]]