תחילת העבודה עם NDK
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
ערכת הפיתוח נייטיב (NDK) היא קבוצת כלים שמאפשרים להשתמש ב-C
ואת קוד C++ ב-Android, ומספק ספריות פלטפורמה שבהן אפשר לנהל
פעילויות מקוריות וגישה לרכיבי מכשירים פיזיים, כמו חיישנים
קלט מגע. NDK לא מתאים לרוב Android מתחילים
מתכנתים שצריכים להשתמש רק בממשקי API של קוד Java וב-framework כדי לפתח
של האפליקציות שלהם. עם זאת, ה-NDK יכול להיות שימושי במקרים שבהם צריך לעשות
אחד או יותר מהאפשרויות הבאות:
- דחיפת ביצועים נוספים מהמכשיר כדי להשיג זמן אחזור נמוך או לפעול
יישומים עתירי חישוב, כגון משחקים או סימולציות בפיזיקה.
- שימוש חוזר שלכם או של מפתחים אחרים ספריות C או C++.
ב-Android Studio 2.2 ואילך ניתן
להשתמש ב-NDK כדי להדר את קוד C ו-C++ בספריית נייטיב ולארוז אותו
ל-APK באמצעות Gradle, מערכת ה-build המשולבת של סביבת הפיתוח המשולבת (IDE). קוד Java שלך
לאחר מכן לקרוא לפונקציות בספריית המקור באמצעות מסגרת Java Native Interface (JNI). למידה
מידע נוסף על Gradle ועל מערכת ה-build של Android, זמין במאמר הגדרת ה-Build.
הכלי שמשמש כברירת מחדל ליצירת ספריות נייטיב ב-Android Studio הוא CMake. גם Android Studio
שתומך ב-ndk-build בגלל
מספר הפרויקטים הקיימים שמשתמשים בערכת הכלים ל-build. אבל אם
ביצירת ספרייה מותאמת חדשה, עליכם להשתמש ב-CMake.
המדריך הזה מספק את המידע הנדרש כדי להתחיל בעבודה עם
NDK ב-Android Studio. אם לא מותקנת במכשיר הגרסה האחרונה של Android
Studio, צריך להוריד ולהתקין אותו עכשיו.
לתשומת לב המשתמשים הניסיוניים ב-Gradle: כדאי לשקול את
מעבר לגרסת הפלאגין בגרסה 2.2.0 ואילך ושימוש ב-CMake או ב-ndk-build.
כדי ליצור את ספריות המקור, אם אחד מהמצבים הבאים רלוונטי לגביכם:
פרויקט מקורי כבר משתמש ב-CMake או ב-ndk-build, אתם מעדיפים להשתמש במערך
גרסה של מערכת ה-build של Gradle, או אם אתם רוצים תמיכה בכלים להוספה,
כמו CCache.
אחרת, אפשר להמשיך להשתמש
את הגרסה הניסיונית של Gradle והפלאגין ל-Android.
הורדת ה-NDK והכלים
כדי להדר ולנפות באגים בקוד המקורי של האפליקציה, צריך את הדברים הבאים
רכיבים:
- Android Native Development Kit (NDK): קבוצת כלים
כדי להשתמש בקוד C וקוד C++ ב-Android.
- CMake: כלי build חיצוני שפועל לצד Gradle כדי ליצור
של ה-AI. אין צורך ברכיב הזה אם בכוונתך להשתמש רק
ndk-build.
-
LLDB: הכלי לניפוי באגים ב-Android Studio שמשמש לניפוי באגים בקוד מקורי.
למידע על התקנת הרכיבים האלה, ראו התקנה והגדרה של NDK ו-CMake.
יצירה או ייבוא של פרויקט Native
אחרי שמגדירים את Android Studio, אפשר פשוט ליצור פרויקט חדש עם
תמיכה ב-C/C++. עם זאת, אם רוצים להוסיף או לייבא קוד נייטיב
בפרויקט Android Studio הקיים, צריך לבצע את התהליך הבסיסי הבא:
-
חדש
קובצי מקור מותאמים ומוסיפים אותם לפרויקט Android Studio.
- אפשר לדלג על השלב הזה אם כבר יש לכם קוד מותאם או אם אתם רוצים
לייבא ספרייה מותאמת שנוצרה מראש.
-
יצירה
סקריפט build של CMake כדי להנחות את CMake
לספרייה. נדרש גם סקריפט ה-build הזה אם מתבצע ייבוא
קישור לספריות מוכנות מראש או ספריות פלטפורמה.
- אפשר לדלג על השלב הזה אם ספריית הנייטיב הקיימת כבר כוללת
סקריפט build מסוג
CMakeLists.txt
או משתמש ב-ndk-build וכולל
Android.mk
סקריפט build.
-
קישור של Gradle אל
את הספרייה המקורית,
באמצעות נתיב ליצירת תוכן
סקריפט. Gradle משתמשת בסקריפט ה-build כדי לייבא את קוד המקור
פרויקט Android Studio ואריזתם את ספריית הנייטיב שלכם (קובץ ה-SO) בתוך
ה-APK.
הערה: אם בפרויקט הקיים שלכם נעשה שימוש במודלים של
ndkCompile
, עליך לפתוח את
קובץ build.properties
והסר את שורת הקוד הבאה
לפני שמגדירים את Gradle לשימוש ב-CMake או ב-ndk-build:
// Remove this line
android.useDeprecatedNdk = true
-
כדי ליצור ולהפעיל את האפליקציה, לוחצים על הסמל
מפעילים את
. הוספות של Gradle
את תהליך ה-CMake או ndk-build כתלות בהידור, בבנייה
לארוז את ספריית ה-APK שלכם עם ה-APK.
אחרי שהאפליקציה תפעל במכשיר פיזי או באמולטור, אפשר להשתמש
Android Studio לניפוי באגים באפליקציה.
לחלופין, כדי לקבל מידע נוסף על ה-NDK ועל הרכיבים שלו, אפשר לעיין בדף מושגים.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-26 (שעון UTC).
[[["התוכן קל להבנה","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 (שעון 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."]]